Click to See Complete Forum and Search --> : Rectangle on screen


phirestalker
March 13th, 2003, 04:53 PM
I'm drawing a rectangle on a graphics surface using Graphics.DrawRectangle method. I want to be able to change the size of it several times, programmatically. What is the best way to get rid of the old size on the graphics object, before drawing it's new size???

Please help

pareshgh
March 13th, 2003, 05:37 PM
store it in Array or member var.

phirestalker
March 13th, 2003, 06:18 PM
what I mean is I draw a rectangle on the screen, and then I need to resize it on the screen. if I call the DrawRectangle again with the new size, it will then have 2 rectangles on the screen, I do not want the first rectangle when I draw the second one with the new size. What's the best way to delete the first one from the screen (when there is other stuff on the screen behind it)??

I know of Graphics.Save and Graphics.Restore, but I was hoping for a different way

pareshgh
March 14th, 2003, 01:40 PM
you can use ArrayList class
and store the previous or history of rectangles and go from there.

Paresh

SimonVega
March 14th, 2003, 04:30 PM
You can call the 'Invalidate()' method, which will cause the form to become dirty and force a re-paint.

pareshgh
March 14th, 2003, 04:32 PM
yes, but before even calling Invalidate or refresh u need to have history of rectangles ? isn't it !!!

Paresh

SimonVega
March 14th, 2003, 04:46 PM
If there is only one rectangle ever drawn on the form, store that rect in a class variable.

pareshgh
March 14th, 2003, 04:49 PM
and that's why I pointed to store in Array List to store if History of rectangles are required !!!

Paresh

SimonVega
March 14th, 2003, 04:51 PM
What's the best way to delete the first one from the screen (when there is other stuff on the screen behind it)??

I answered his direct question...

pareshgh
March 14th, 2003, 04:59 PM
ok,
never mind..
:D

SimonVega
March 14th, 2003, 05:00 PM
no harm, no foul...;)

phirestalker
March 14th, 2003, 11:54 PM
ok I thought of that, but if I am going to be doing this a couple times per second, won't calling that cause a flicker effect? I am looking through the help, and I am trying to find a way to store what is where I'm going to draw the rectangle and save it before I draw it, and then when I go to draw a new rectangle I do the same thing, but first replace what was behind the first rectangle and then save behind the new one again and so on. The reason I need to do this, is I am writing a class SelectionBox to draw the equivelent of what a lot of windows programs have, I want this class to keep track of itself as an OBJECT like all classes should, so that only ONE rectangle is on the screen at a time for each instance, and each instance can be moved individualy :)

pareshgh
March 17th, 2003, 04:33 PM
you could specify Graphics path and use Region and do the clipping for rectangles.

Paresh

phirestalker
March 18th, 2003, 06:32 PM
ok guys the code is attatched to this post, for some reason it is not erasing the old rectangles, could you tell me what I'm doin wrong?

pareshgh
March 19th, 2003, 12:32 PM
this is the basic class, could you copy the code for where this class is called so that it can be debugged atleast.

Paresh

phirestalker
March 19th, 2003, 02:37 PM
here's the form that uses the SelectionBox class

Scott MacMaster
March 20th, 2003, 03:34 AM
Hmm, seems overly complicated. I'd suggest dropping the SelectionBox class unless you need to store more information about the selection then a rectangle.

Add these lines to the constructor of your form.

this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint , true);
this.SetStyle(ControlStyles.UserPaint, true);

Then, just draw what you need to draw in your paint event handler. Probably just change the call to SelectionBox.Draw() to Graphics.DrawRectangle(). No need to worry about flicker. The lines I said to add to the constructor will tell the system to take care of take. You could read about double buffering to understand what it's doing but you don't need to know how it works to use it.

phirestalker
March 20th, 2003, 07:01 PM
Thank you Scott, u were right :)