【发布时间】:2011-08-27 15:17:57
【问题描述】:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace d3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pictureBox1.Image = Image.FromFile("C:\\Users\\ocean\\Desktop\\deneme.png");
pictureBox1.Location = new Point(0, 0);
pictureBox2.Image = Image.FromFile("C:\\Users\\ocean\\Desktop\\pul1.png");
}
protected override void OnMouseClick(MouseEventArgs e)
{
Graphics theGraphics = Graphics.FromHwnd(this.Handle);
for (int i = 0; i < 200; i += 5)
{
pictureBox2.Location = new Point(i, 100);
theGraphics.Flush();
System.Threading.Thread.Sleep(50);
pictureBox2.Invalidate();
}
}
}
}
在此代码中,picturebox2 移动正常,但之前的位置图像保持 ON THE 直到循环结束。当循环完成时,旧部分将被删除。我不希望循环内的以前的画我只想在pictureBox1 上移动。我是 C# 新手,所以请帮助我:) 在 J2Me 中,我使用的是flushgraphics,但在这里我尝试过,但没有成功,如果你能举个例子,我会很高兴。
【问题讨论】:
-
不要在带有 thread.sleep 的 for 循环中执行此操作,而是使用每 50 ms 将图像移动 5 个像素然后停止的计时器执行此操作。这将允许更改正确地绘制到屏幕上
-
此代码不允许窗体的 PaintBackground 方法运行,需要替换像素。像这样的循环从来都不是问题,如果消息循环不运行,东西就会停止正常工作。改用定时器,this.Update() 是创可贴。