for (int i = 1; i <= 4; i++)
{
this.pictureBox1.Location = new Point(p.X + i * 15, p.Y);
this.pictureBox1.Show();
Thread.Sleep(250);
}我想让这个pictureBox每250ms把位置向右移动15px。移动4次的效果。
如果用了Thread.Sleep(250);
他是把整个程序都给Sleep了。
结果是1S后才移动到正确位置。怎么办。做一个坦克行走的功能效果
{
this.pictureBox1.Location = new Point(p.X + i * 15, p.Y);
this.pictureBox1.Show();
Thread.Sleep(250);
}我想让这个pictureBox每250ms把位置向右移动15px。移动4次的效果。
如果用了Thread.Sleep(250);
他是把整个程序都给Sleep了。
结果是1S后才移动到正确位置。怎么办。做一个坦克行走的功能效果
for (int i = 1; i <= 4; i++)
{
this.pictureBox1.Location = new Point(p.X + i * 15, p.Y);
this.pictureBox1.Show();
Thread.Sleep(250);
}这段代码就可以了。
不过应该需要加委托。
也就是250ms调用一次这个时间
然后双击timer 在这里面写代码
struct LASTINPUTINFO
{
[MarshalAs(UnmanagedType.U4)]
public int cbSize;
[MarshalAs(UnmanagedType.U4)]
public uint dwTime;
}
[DllImport("user32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
static long GetLastInputTime()
{
LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
if (!GetLastInputInfo(ref vLastInputInfo)) return 0;
return Environment.TickCount - (long)vLastInputInfo.dwTime;
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
Text = string.Format("用户已经{0}秒没有路过了", GetLastInputTime() / 1000);
}
还有简单的做法是用Timer控件。定时的处理。
放个Timer控件
设置Enabled=true;
设置全局变量 int pcout=1;//移动次数
pLocation=pictureBox1.Location.x;//得到图片的x位置
Interval=250;//多长时间调用事件
在 Tick事件中
if (pcout <=4){
this.pictureBox1.Location = new Point(pLocation + 15, pictureBox1.Location .Y);
pLocation=this.pictureBox1.Location.x;
pcout++;
}