C # simulates MSN form jitter
Form jitter is an interesting thing, let's take a look at how it works.
It is actually generating a random number and then changing the coordinates of the upper-left corner of the form.
I use the loop to make, actually can use the timer to control.
I split the jitter into two types of jitter:
1. Generate random numbers, change the upper-left coordinate of the form, and immediately restore the form's sitting-up corner coordinates to continue the loop.
2. Generate random numbers, change the upper-left corner of the form, and then immediately restore the form's upper-corner coordinates after the loop is complete.
The core code is as follows:
The first type of jitter
private void Button1_Click (object sender, EventArgs e)
{
int RECORDX = this. Left; Save the X-coordinate of the upper-left corner of the original form
int Recordy = this. Top; Save the y-coordinate of the upper-left corner of the original form
Random random = new random ();
for (int i = 0; i <; 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; Restores the x-coordinate of the upper-left corner of the original form
This. Top = Recordy; Restores the y-coordinate of the upper-left corner of the original form
}
}
The second type of 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 <; 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;
}