【发布时间】:2019-12-11 09:38:06
【问题描述】:
我正在使用 Windows 窗体 (.NET Framework) 并试图让图片框在屏幕上移动。 我试过使用定时器和这个while循环,但是在while循环的情况下图像(它应该是一个平面)没有出现,并且使用定时器使得很难删除过去的图片框,所以它们似乎生成了一个序列的飞机。我该如何做到这一点?它与 Sleep() 有关系吗?
private void Button1_Click(object sender, EventArgs e)
{
//airplane land
//drawPlane(ref locx, ref locy);
//timer1.Enabled = true;
while (locx > 300)
{
var picture = new PictureBox
{
Name = "pictureBox",
Size = new Size(30, 30),
Location = new System.Drawing.Point(locx, locy),
Image = Properties.Resources.plane2, //does not appear for some reason
};
this.Controls.Add(picture);
Thread.Sleep(500);
this.Controls.Remove(picture);
picture.Dispose();
locx = locx - 50;
}
【问题讨论】:
-
使用System.Windows.Forms.Timer 移动控件。您只需要一个 PictureBox:重新定义它在
Timer.Tick事件中的位置。不要使用Resources工厂设置 Image 属性:将 Image 分配给 Bitmap 对象,然后在需要时将 Bitmap 分配给 Image 属性,并在不再需要时将其丢弃。
标签: c# visual-studio winforms picturebox