天天看点

C# 实现窗体抖动

 1.方法一

private void button1_Click(object sender, EventArgs e)
        {
            Random ran = new Random((int)DateTime.Now.Ticks);
            Point point = this.Location;
            for (int i = 0; i < 40; i++)
            {
                this.Location = new Point(point.X + ran.Next(8) - 4, point.Y + ran.Next(8) - 4);
                System.Threading.Thread.Sleep(15);
                this.Location = point;
                System.Threading.Thread.Sleep(15);
            }
        }
           

2.方法二(多线程)

private void button1_Click(object sender, EventArgs e)
{
    Random ran = new Random((int)DateTime.Now.Ticks);
    Point point = this.Location;
    new Thread((ThreadStart)delegate
    {
        for (int i = 0; i < 40; i++)
        {
            this.Invoke((EventHandler)delegate
            {
                this.Location = new Point(point.X + ran.Next(8) - 4, point.Y + ran.Next(8) - 4);
                System.Threading.Thread.Sleep(15);
                this.Location = point;
                System.Threading.Thread.Sleep(15);
            });
        }
    }).Start();
}
           

继续阅读