Hi
I am tryintg to draw a moving rectangle and circle on a form using the paint method.
A button should start the process. 2nd pres should end the program.
The once variable is a global boolean set to True on start.
X1 is a global int, set to 10 on start.
Up is a global boolean, set to true on start.
Each iteeration the X1 variable is increased until 100, going down to 10, endlessly.
Two problems:
1- No movement of the drawing on the form
2- No control on the form once the program starts.
- private void Form1_Paint(object sender, PaintEventArgs e)
- {
-
- Pen red = new Pen(Color.Red,3);
- Rectangle rect = new Rectangle(x1, x1, x1, x1);
- Rectangle circle = new Rectangle(x1+10, x1 + 10, x1 + 50, x1 + 50);
-
-
- Graphics g = CreateGraphics();
- g.DrawRectangle(red,rect);
- g.DrawEllipse(red, circle);
-
- red.Dispose();
- g.Dispose();
- }
-
- private void button2_Click(object sender, EventArgs e)
- {
- if (once)
- once = false;
- else
- Environment.Exit(0);
-
- while (true)
- {
- if (up )
- {
- x1 += 10;
- if (x1 > 100)
- up = false;
- }
- else
- {
- x1 -= 10;
- if (x1 <= 10)
- up = true;
- }
- this.Invalidate();
- Thread.Sleep(500);
- }
- }