C#多方法实现窗体抖动

窗口抖动作为一种提醒消息,也可以说是有点用处的,下面我们可以简单实现一下

一.编程思路

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);
            }
两种简单的方法实现,具体应用考虑实际情况
原创文章 36 获赞 31 访问量 2084

猜你喜欢

转载自blog.csdn.net/qq_45096273/article/details/105601530