C#中WPF窗体抖动

窗体抖动效果

具体思路:重新设置窗体的位置,还原窗体位置,利用Button按钮的点击事件调动

设置窗体简单的样式:

C#中WPF窗体抖动_第1张图片

//按钮的点击事件

private void Btn_Click(object sender, RoutedEventArgs e)

{

//方法一:

  #region 窗体抖动代码

            //Random ran = new Random();

            ////保存当前窗体位置

            //Point oldPoint = this.Location;

            //for (int i = 0; i < 35; i++)

            //{

            //    //随机生成新的位置

            //    Point newPoint = new Point(oldPoint.X + ran.Next(-80, 80), oldPoint.Y + ran.Next(-80, 80));

            //    //将位置设置给窗体

            //    windoo.Location = newPoint;

////Sleep方法需要导入命名空间System.Threading

            //    Thread.Sleep(50);

            //    windoo.Location = oldPoint;

            //    //休息50毫秒

            //    Thread.Sleep(50);

            //}

#endregion

//方法二:

            #region 窗体抖动代码

            // x位抖动速度,单位是像素,y为时间间隔,单位是毫秒

//如果X=10的话,窗体在抖动时会向下移动,原因不清楚

            int x = 5;

            int y = 10;

            //死循环实现一直抖动

            //for (; ; )

                for (int i = 0; i < 35; i++)

                {

                     //设置窗体X轴的新位置

                    this.Left += x; 

//Sleep方法需要导入命名空间System.Threading

//休息y(10)毫秒            

                    Thread.Sleep(y);

                    this.Top += x;

                    Thread.Sleep(y);

                    this.Left -= x;

                    Thread.Sleep(y);

                    this.Top -= x;

                    Thread.Sleep(y);

                }

            #endregion

        }

 

效果图:

C#中WPF窗体抖动_第2张图片

 

 

你可能感兴趣的:(C#中WPF窗体抖动)