开发者

how to clear panel in a simple paint application

I am working on a simple windows forms paint application. I am having problem in clearing the panel. The code i am using to draw is

private void panel1_Paint(object sender, PaintEventArgs e)
{
    Graphics g = Graphics.FromImage(tempDraw);
    Pen myPen = new Pen(foreColor, lineWidth);
    g.DrawLine(myPen, x1, y1, x2, y2);
    myPen.Width = 100;
    myPen.Dispose();
    e.Graphics.DrawImageUnscaled(tempDraw, 0, 0);
    g.Dispose();
 }

How to clear th开发者_StackOverflow中文版e panel?


Are drawing in the paint handler of the Panel instance? If not then calling Invalidate on the panel would do.

But you will probably be persisting the drawing items and so to clear them you would need to delete what has been drawn and then call Invalidate. You could also fill the Panel with a particular color using FillRect but that would be a dirty workaround and not fit your final design.

You should also check out CodeProject.com for examples like this one to give you an idea on what needs to be handled when creating a drawing app like this.

EDIT:

Per the edited answer, you cannot clear the panel with the existing logic. You are painting inside Paint handler of the form which will happen any time it needs to be redrawn. This means that you should change your approach. You need some sort of condition inside the Paint handler which decides whether or not it will paint anything at all. This is where the persistence of drawing objects comes in. If you want to create a drawing program then you will have to handle the mouse Down, Up and Move events over the panel objects and store the data in a points array. (As an example of one type of drawing.) Then in your Paint handler if the Points[] is not empty you draw the points. Otherwise you draw nothing... which ends up in an empty container. Then if you need to clear the drawing you delete the contents of the Points array and call Invalidate on the Panel. That will clear the persisted data and repaint to nothing.


You can use

Panel1.Invalidate();

But there is a problem with this, after you call this function it clears all the graphics from the panel, but it also recalls the function i.e.

private void panel1_Paint(object sender, PaintEventArgs e)
{
    //This function is recalled after Panel1.Invalidate();
}

So solution is to make your paint code in some other function

private void MyDrawing()
{
    Graphics g = Graphics.FromImage(tempDraw);
    // if above line doesn't work you can use the following commented line
    //Graphics g = Graphics.Panel1.CreateGraphics();

    Pen myPen = new Pen(foreColor, lineWidth);
    g.DrawLine(myPen, x1, y1, x2, y2);
    myPen.Width = 100;
    myPen.Dispose();
    Panel1.Graphics.DrawImageUnscaled(tempDraw, 0, 0);
    g.Dispose();
 }


You'll have to draw over the panel again with whatever base colour you're using eg. white\grey with the Graphics.FillRectangle method:

// Create solid brush.
SolidBrush whiteBrush = new SolidBrush(Color.White);
// Create location and size of rectangle.
// Fill rectangle to screen.
e.Graphics.FillRectangle(whiteBrush, panel.Location.X, panel.Location.Y, panel.Width, panel.Height);
this.Invalidate();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜