首页 > 代码库 > C#模拟MSN窗体抖动

C#模拟MSN窗体抖动

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

<iframe id="google_ads_frame2" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1399652758&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fgc2013%2Fp%2F3719339.html&dt=1399652761285&shv=r20140506&cbv=r20140417&saldr=sb&prev_slotnames=4356862740&correlator=1399652761139&frm=20&ga_vid=429972749.1397695120&ga_sid=1399648611&ga_hid=2059708938&ga_fc=1&u_tz=480&u_his=127&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=12&adx=0&ady=50358&biw=314&bih=74&eid=317150304%2C36098801&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=2&xpc=ik73bBcOTg&p=http%3A//www.cnblogs.com&dtd=101" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame2" marginWidth="0" scrolling="no" hspace="0"></iframe><iframe id="google_ads_frame3" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1399652758&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fgc2013%2Fp%2F3719339.html&dt=1399652761399&shv=r20140506&cbv=r20140417&saldr=sb&prev_slotnames=4356862740%2C8660799060&correlator=1399652761139&frm=20&ga_vid=429972749.1397695120&ga_sid=1399648611&ga_hid=2059708938&ga_fc=1&u_tz=480&u_his=127&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=12&adx=304&ady=50608&biw=314&bih=74&eid=317150304%2C36098801&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=3&xpc=wB4a2PiY2E&p=http%3A//www.cnblogs.com&dtd=42" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame3" marginWidth="0" scrolling="no" hspace="0"></iframe>