Prasanna RN
Prasanna RN

Reputation: 87

How to clear all the previously drawn graphics in vc++ paint?

I have drawn rectangles in picture box using paint event. When i click clear button. i want the graphics to vanish. i call paint event everytime the mouse moves. What should i do?

Code in paint event:

Graphics^ g = e->Graphics;
float PenWidth = 2;
if(msdwnflag!=-1 && count%2==1)
{
    if(selecflag==0)
    {
    g->DrawRectangle( gcnew Pen( Color::Blue,PenWidth ), RcDraw);
}
else
{
    RcDraw.Width = finalMousePos.X- RcDraw.X;
    RcDraw.Height = finalMousePos.Y- RcDraw.Y;
    g->DrawRectangle( gcnew Pen( Color::Red,PenWidth ), RcDraw);
}
}

Upvotes: 2

Views: 3754

Answers (2)

Prasanna RN
Prasanna RN

Reputation: 87

Draw a graphics of the transparent colour. thats wat i finally did. not a gud design but works :)

Upvotes: 0

Subs
Subs

Reputation: 529

If pb is your PictureBox then clear its image to clear all the graphics. Also, You can use a variable (buttonpressed) to check whether it is true (button clear pressed) or false (otherwise)

     buttonpressed=1;
     pb->Image = nullptr;
     pb->Refresh();

In your paint method include all the graphics if Not buttonpressed:

     if (buttonpressed != 1){
         // all your graphics code
     }

When you want the graphics to appear back when you press a button change the buttonpressed value:

    buttonpressed=0;
    pb->Refresh();

Upvotes: 3

Related Questions