Solved

How to delete rectangle drew with GDI

Posted on 2004-08-17
4
1,686 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…

739 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