C# 在面板中滚动许多图片

C# 在面板中滚动许多图片,c#,.net,scroll,C#,.net,Scroll,我有一个面板,我把图片盒放在面板里。所有的画框都是正方形,大小相同。我把它们分为三列。我想自动滚动它们(向上移动)。当前三张图片(第一行)消失时,它会显示在底部 我使用定时器逐像素向上移动,如果第一行消失,我会更改所有PictureBox的位置。但他们是flickr,我尝试了一些方法,但没有任何效果。请给我一些想法。 这是另一种方法,我使用了FlowLayoutPanel,但存在相同的问题 class PicturesPanel : Panel { private FlowLayoutP

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

我使用定时器逐像素向上移动,如果第一行消失,我会更改所有PictureBox的位置。但他们是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();
        }
    }
}
类图片面板:面板{
私有FlowLayoutPanel flowPanel;
内部定时器;
私有列表图片列表;
私人int top;
公共图像列表图像{
得到;
设置
}
公共图片面板(){
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint,true);
//this.AutoScroll=true;
PicturesList=新列表();
}
私有无效图片面板单击(对象发送者,事件参数e){
loadImages();
}
私有void loadImages(){
如果(this.Images!=null){
int count=this.Images.Images.count;
int estimateHeight=60*(计数/3)-4;
flowPanel=新的FlowLayoutPanel();
flowPanel.Top=0;
flowPanel.Left=0;
flowPanel.宽度=200;
flowPanel.Height=估计高度+50;
flowPanel.FlowDirection=FlowDirection.LeftToRight;
this.Controls.Add(flowPanel);
for(int i=0;i此高度){
定时器=新定时器();
时间间隔=25;
timer.Tick+=新事件处理程序(timer\u Tick);
autoscroll=true;
timer.Start();
}
}
}
}
私有无效计时器(对象发送方、事件参数){
//this.VerticalScroll.Value+=1;

//如果(PicturesList[0].Bottom我不会在FlowLayoutPanel中移动PictureBox以获得此效果,请尝试更改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;
  }
}

好的,您的代码只滚动图片框1次。但是我想循环自动滚动。如果第一行消失,它将移动到底部,依此类推。@SmartGoat然后,不要像我的示例中那样关闭计时器,而是将其更改为
flowPanel.VerticalScroll.Value=0;
以再次将其设置回顶部。我已经尝试过了。但它会导致fli当滚动到顶部时返回ckrsuddenly@SmartGoat看到了。谢谢,我终于解决了这个问题。我画了那些图片框中的所有图像,并用新的位置在计时器上重新绘制。