当前位置: 移动技术网 > IT编程>开发语言>c# > C#模拟MSN窗体抖动的实现代码

C#模拟MSN窗体抖动的实现代码

2019年07月18日  | 移动技术网IT编程  | 我要评论

基于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;
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网