C#類比MSN表單抖動

來源:互聯網
上載者:User

標籤:style   blog   java   tar   ext   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;
        }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.