Skip to content

Resize a Rectangle at runtime

ugp

Asked May 25, 2007, 6:44 AM EDT1 answer
Share
LinkedInX
I need to resize a Rectangle at runtime. I draw the Retangle on the Form1_Paint(), but unable to resize itt.. Graphics graph = this.CreateGraphics(); Pen penCurrent = new Pen(Color.Red); Rect = new Rectangle(300, 50, 150, 75); graph.DrawRectangle(penCurrent, Rect); need some help!

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0
    First of all, you don't want to call CreateGraphics, you should do everything in the Paint event handler. To resize a rectangle, just change the boundaries of the rectangle outside the Paint event.

    for example:

    OnPaint()
     {
       e.Graphics.DrawRectangle(30, 30, x, y);
    }

    Set x and y outside of the event handler and call invalidate on the form region

    OnButtonPressed (object sender, EventArg e)
    {
       x = x+5;
       y = y + 5;
       Invalidate(29, 29, x+1, y+1); // force it to redraw
    }
    0