程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#模擬MSN窗體抖動

C#模擬MSN窗體抖動

編輯:C#入門知識

C#模擬MSN窗體抖動

窗體抖動是件很有意思的事情,就讓我們看看一起來看看它的原理吧。 
其實是生成隨機數,然後改變Form的左上角的坐標。
我用的是循環來弄得,其實可以用timer來控制.

 我把抖動分成了兩種抖動:
1。 生成隨機數,改變窗體左上角坐標,然後立即把窗體的坐上角坐標還原,繼續循環。
2。 生成隨機數,改變窗體左上角坐標,循環完畢之後,然後立即把窗體的坐上角坐標還原。

核心代碼如下:


//第一種抖動
        private void button1_Click(object sender, EventArgs e)
        {
            int recordx = this.Left;            //保存原來窗體的左上角的x坐標
            int recordy = this.Top;             //保存原來窗體的左上角的y坐標

            Random random = new Random();      

            for (int i = 0; i < 100; i++)
            {
                int x = random.Next(rand);
                int y = random.Next(rand);
                if (x % 2 == 0)
                {
                    this.Left = this.Left + x;
                }
                else
                {
                    this.Left = this.Left - x;
                }
                if (y % 2 == 0)
                {
                    this.Top = this.Top + y;
                }
                else
                {
                    this.Top = this.Top - y;
                }

                this.Left = recordx;            //還原原始窗體的左上角的x坐標
                this.Top = recordy;             //還原原始窗體的左上角的y坐標
            }
           
        }


        //第二種抖動
        private void button2_Click(object sender, EventArgs e)
        {
            int recordx = this.Left;
            int recordy = this.Top;
            Random random = new Random();
            for (int i = 0; i < 50; i++)
            {
                int x = random.Next(rand);
                int y = random.Next(rand);
                if (x % 2 == 0)
                {
                    this.Left = this.Left + x;
                }
                else
                {
                    this.Left = this.Left - x;
                }
                if (y % 2 == 0)
                {
                    this.Top = this.Top + y;
                }
                else
                {
                    this.Top = this.Top - y;
                }
                System.Threading.Thread.Sleep(1);
            }
            this.Left = recordx;
            this.Top = recordy;
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved