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