窗口抖动作为一种提醒消息,也可以说是有点用处的,下面我们可以简单实现一下
一.编程思路
1.需要的控件button当点击时进行运动;
2.运动可以有线程控制和timer控制;
3.运动与位置有关也就是location;
二.代码实现
1.线程实现
线程Threda,当然需要命名空间中加入using System.Threading;否则无法使用线程概念图:从中心园出发围绕圆点运动一周再回到中心圆点
代码片段
private void button1_Click(object sender, EventArgs e)
{
int x = this.Left;//将x赋给左边距
int y = this.Top;//将y赋给上边距
for (int i = 0; i < 5; i++)
{
//围绕运动一周位置改变
this.Location = new Point(x, y);
Thread.Sleep(50);//改变一次位置后间隔,以下以此类推
this.Location = new Point(x + 10, y);
Thread.Sleep(50);
this.Location = new Point(x + 10, y + 10);
Thread.Sleep(50);
this.Location = new Point(x, y + 10);
Thread.Sleep(50);
this.Location = new Point(x - 10, y + 10);
Thread.Sleep(50);
this.Location = new Point(x - 10, y);
Thread.Sleep(50);
this.Location = new Point(x - 10, y - 10);
Thread.Sleep(50);
this.Location = new Point(x, y - 10);
Thread.Sleep(50);
this.Location = new Point(x + 10, y - 10);
Thread.Sleep(50);
this.Location = new Point(x + 10, y);
Thread.Sleep(50);
this.Location = new Point(x, y);
Thread.Sleep(50);
}
}
2.timer实现
timer计时器:按照用户定义的间隔引发的事件的组件,当然也用到了第一步的概念图代码片段:以下语句都写在要进行点击触发抖动事件的按钮里
for (int i = 0; i < 10; i++)
{
this.Location = new Point(x, y);
this.Location = new Point(x - 5, y);
this.Location = new Point(x - 5, y - 5);
this.Location = new Point(x, y - 5);
this.Location = new Point(x + 5, y - 5);
this.Location = new Point(x + 5, y);
this.Location = new Point(x + 5, y + 5);
this.Location = new Point(x, y + 5);
this.Location = new Point(x - 5, y + 5);
this.Location = new Point(x - 5, y);
this.Location = new Point(x, y);
}