C#模仿MSN窗體發抖的完成代碼。本站提示廣大學習愛好者:(C#模仿MSN窗體發抖的完成代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是C#模仿MSN窗體發抖的完成代碼正文
基於C#完成窗體的發抖是件很成心思的工作,道理其實不難,實際上是生成隨機數,然後轉變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; }