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后才移动到正确位置。怎么办。做一个坦克行走的功能效果

解决方案 »

  1.   

    起一个线程来执行
    for (int i = 1; i <= 4; i++)
      {
      this.pictureBox1.Location = new Point(p.X + i * 15, p.Y);
      this.pictureBox1.Show();
      Thread.Sleep(250);
      }这段代码就可以了。
    不过应该需要加委托。
      

  2.   

    简单的用法,用timer控件,设置Enabled为true ,Interval设置时间频率,在Tick事件里面写要实现的效果
      

  3.   

    补充一下,WPF可以用DispatcherTimer,其他的Timer是不在UI线程的,更新UI需要委托,相比你的要求,DispatcherTimer使用起来更方便一些
      

  4.   

    将timer的时间设置为250
    也就是250ms调用一次这个时间
    然后双击timer 在这里面写代码
      

  5.   

    using System.Runtime.InteropServices;[StructLayout(LayoutKind.Sequential)]
    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);
    }
      

  6.   

    首先要知道,我们的winform程序界面用的是单线程方式,也是主线程。如果我们要和用户交互就不能sleep这个线程,我们可以另开一个线程处理,但是新的线程不能直接访问我们主线程的控件资源,需要调界面的Invoke方法,Invoke方法内传入真正的要调用的方法。
    还有简单的做法是用Timer控件。定时的处理。
      

  7.   

    用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++;
    }