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();
}
}
}
好了,你的代碼srolls的pictureboxes只有1次。但我想循環自動滾動。如果第一行消失,它將移動到底部,依此類推。 – SmartGoat 2013-04-21 14:56:53
@SmartGoat然後,不要像我的例子那樣關閉計時器,而是將其更改爲'flowPanel.VerticalScroll.Value = 0;'將其重新設置回頂部。 – LarsTech 2013-04-21 15:02:28
我已經試過了。但是,當它突然滾動到頂部時會導致flickr – SmartGoat 2013-04-21 15:31:04