2013-04-21 36 views
0

我有一个面板,我把面板放在面板中。所有的照片盒都是正方形和相同的尺寸。我把它们放在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

我不动你的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; 
    } 
} 
+0

好了,你的代码srolls的pictureboxes只有1次。但我想循环自动滚动。如果第一行消失,它将移动到底部,依此类推。 – SmartGoat 2013-04-21 14:56:53

+0

@SmartGoat然后,不要像我的例子那样关闭计时器,而是将其更改为'flowPanel.VerticalScroll.Value = 0;'将其重新设置回顶部。 – LarsTech 2013-04-21 15:02:28

+0

我已经试过了。但是,当它突然滚动到顶部时会导致flickr – SmartGoat 2013-04-21 15:31:04