C # simulate MSN form Jitter

Source: Internet
Author: User

C # simulate MSN form Jitter

Form jitter is a very interesting thing. Let's take a look at its principles.
In fact, it is to generate a random number and then change the coordinates in the upper left corner of the Form.
I use a loop to get it. In fact, I can use timer to control it.

I split the jitter into two types:
1. Generate a random number, change the coordinates in the upper left corner of the form, and immediately restore the sitting coordinate of the form to continue the loop.
2. Generate a random number and change the coordinates in the upper left corner of the Form. After the loop is completed, immediately restore the sitting coordinate of the form.

The core code is as follows:


// First Jitter
Private void button#click (object sender, EventArgs e)
{
Int recordx = this. Left; // Save the x coordinate in the upper Left corner of the original form.
Int recordy = this. Top; // Save the y coordinate in the upper left corner of the original form

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; // restore the x coordinate in the upper Left corner of the original form.
This. Top = recordy; // restore the y coordinate in the upper left corner of the original form.
}

}


// Second Jitter
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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.