Solved

How to delete rectangle drew with GDI

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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 gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

22 Experts available now in Live!

Get 1:1 Help Now