Solved

How to delete rectangle drew with GDI

Posted on 2004-08-17
4
1,683 Views
Last Modified: 2007-12-19
Hi,
I have simple question regarding to drawing with GDI.
How can I delete things which I drew with GDI, I mean for instance, a rectangle.
I’m using following code to draw rectangle:

                        Graphics gfx = this.tabControlPanel3.CreateGraphics();
                  Rectangle rc = new Rectangle(10,20,100,200);
                  gfx.SmoothingMode = SmoothingMode.AntiAlias;
                  Pen pen = new Pen(Brushes.Red,3);
                  gfx.DrawRectangle(pen,rc);

I want to delete this rectangle from my Form after pressing a button.

Thanks for your help.
0
Comment
Question by:knobloch
  • 3
4 Comments
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11839281
Well, the short answer is, you can't :-p

You must redraw the form without drawing the rectangle.... either that, or draw a new rectangle (over the old one) that is the same color as the background, as to hide it.

There is no "undo" with GDI.

You can liken your gdi surface to painting with a permanent pen.  The only thing you can do is to paint over it with another pen :-p (I know, bad example, but I'm trying!)

-Eric
0
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11839480
Here is a simple example.  I created 2 buttons, one for draw, the other for, erhm, undraw.

Here's the code:

private void btnDraw_Click(object sender, System.EventArgs e)
{
      System.Drawing.Graphics gfx = this.CreateGraphics();
      System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
      System.Drawing.Pen p = new System.Drawing.Pen(b, 3.0f);
      gfx.DrawRectangle(p, btnDraw.Left - 10, btnDraw.Top - 10, btnDraw.Width + 20, btnDraw.Height + 20);
}

private void button2_Click(object sender, System.EventArgs e)
{
      System.Drawing.Graphics gfx = this.CreateGraphics();
      System.Drawing.Brush b = new System.Drawing.SolidBrush(this.BackColor);
      System.Drawing.Pen p = new System.Drawing.Pen(b, 3.0f);
      gfx.DrawRectangle(p, btnDraw.Left - 10, btnDraw.Top - 10, btnDraw.Width + 20, btnDraw.Height + 20);
}

Should be a start for you.

-Eric
0
 

Author Comment

by:knobloch
ID: 11842555
I can't use color of my Form as a color for brush because my form has gradient background and brush this.BacColor will mess up my background.
Anyway thanks, maybe I will repaint my form, but it seems not good idea if you have to do this many times. I will test it.
0
 
LVL 3

Accepted Solution

by:
bigjim2000 earned 500 total points
ID: 11844021
Well, if you just do it when you click a button, that is nothing, go ahead and repaint your form using similar code as above.  If you are going to be drawing it several times per second, then I would suggest implementing a double buffered system.  This has 2 graphics surfaces.  You draw to one of them, then when you are finished drawing, you copy over the entire thing.  This eliminates (mostly) the flickering you see when you draw a lot at once on the screen.

Another thing to consider, is to paint your form once to a bitmap in memory (your gradient background).  Then when you want to draw your form, you copy the bitmap in memory.  Then you draw on the form a bit, then you can copy the gradient bitmap again, only this time, just copy part of it (where you drew on the form).

-Eric
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question