在面板中滚动许多图片

时间:2013-04-21 13:18:43

标签: c# .net scroll

我有一个面板,我把图片放在面板中。所有图片框都是方形和相同的大小。我把它们分成3列。我想自动滚动它们(向上移动)。当前三张图片(第一行)消失时,它会显示在底部。

我使用计时器逐个像素地向上移动,如果第一行消失,我改变所有图片框的位置。但他们flickr,我尝试了一些方法但没有任何作用。请给我一些想法。 这是另一种方式,我使用FlowLayoutPanel,但同样的问题。

class PicturesPanel : Panel {
    private FlowLayoutPanel flowPanel;
    internal Timer timer;
    private List<BorderPictureBox> PicturesList;
    private int top;

    public ImageList Images {
        get;
        set;
    }

    public PicturesPanel() {
        this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);
        //this.AutoScroll = true;
        PicturesList = new List<BorderPictureBox>();
    }

    private void PicturesPanel_Click(object sender, EventArgs e) {
        loadImages();
    }

    private void loadImages() {
        if (this.Images != null) {
            int count = this.Images.Images.Count;
            int estimateHeight = 60 * (count / 3) - 4;

            flowPanel = new FlowLayoutPanel();
            flowPanel.Top = 0;
            flowPanel.Left = 0;
            flowPanel.Width = 200;
            flowPanel.Height = estimateHeight + 50;
            flowPanel.FlowDirection = FlowDirection.LeftToRight;
            this.Controls.Add(flowPanel);

            for (int i = 0; i < count; i++) {
                BorderPictureBox newPic = new BorderPictureBox();
                newPic.Image = this.Images.Images[i];
                newPic.Width = 56;
                newPic.Height = 56;
                newPic.SizeMode = PictureBoxSizeMode.StretchImage;
                newPic.Top = 60 * (i / 3);
                newPic.Left = 60 * (i % 3);
                flowPanel.Controls.Add(newPic);
                PicturesList.Add(newPic);
            }

            if (timer == null) {
                if (estimateHeight > this.Height) {
                    timer = new Timer();
                    timer.Interval = 25;
                    timer.Tick += new EventHandler(timer_Tick);
                    autoscroll = true;
                    timer.Start();
                }
            }
        }
    }

    private void timer_Tick(object sender, EventArgs e) {
        //this.VerticalScroll.Value += 1;
        //if (PicturesList[0].Bottom <= -4) {
        //    PicturesList.Add(PicturesList[0]);
        //    PicturesList.Add(PicturesList[1]);
        //    PicturesList.Add(PicturesList[2]);
        //    PicturesList.RemoveAt(0);
        //    PicturesList.RemoveAt(0);
        //    PicturesList.RemoveAt(0);
        //    this.VerticalScroll.Value = 0;

        //    for (int i = 0; i < PicturesList.Count; ++i) {
        //        PicturesList[i].Top = 60 * (i / 3);
        //        PicturesList[i].Left = 60 * (i % 3);
        //    }

        //}
        flowPanel.Top -= 1;
        if (flowPanel.Top <= -60) {
            flowPanel.SuspendLayout();
            flowPanel.Controls.RemoveAt(0);
            flowPanel.Controls.RemoveAt(0);
            flowPanel.Controls.RemoveAt(0);
            flowPanel.Controls.Add(PicturesList[0]);
            flowPanel.Controls.Add(PicturesList[1]);
            flowPanel.Controls.Add(PicturesList[2]);
            PicturesList.Add(PicturesList[0]);
            PicturesList.Add(PicturesList[1]);
            PicturesList.Add(PicturesList[2]);
            PicturesList.RemoveAt(0);
            PicturesList.RemoveAt(0);
            PicturesList.RemoveAt(0);
            flowPanel.Top = 0;
            flowPanel.ResumeLayout();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我不会在FlowLayoutPanel中移动PictureBoxes来获得此效果,只需更改FlowLayoutPanel滚动条的值:

void timer_Tick(object sender, EventArgs e) {
  flowPanel.VerticalScroll.Value += 1;
  if (flowPanel.VerticalScroll.Value + flowPanel.VerticalScroll.LargeChange > 
                                       flowPanel.VerticalScroll.Maximum) {
    ((Timer)sender).Enabled = false;
  }
}