Solved

How to delete rectangle drew with GDI

Posted on 2004-08-17
4
1,677 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

937 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now