PaintEvent not overpainting

前端 未结 3 2055
北荒
北荒 2021-01-21 23:37

I have a Windows Forms application in C# with drawing panel and a button - for drawing a line.

When you click the button, you can draw a line for 2 random points.

3条回答
  •  醉酒成梦
    2021-01-22 00:03

    Whenever you want to redraw (in your code) you should call panelinvalidate drawPanel.Invalidate().

    When you finish mouse moving and release the button

    private void drawPanel_MouseUp(object sender, MouseEventArgs e)
    {
                onMouseUpFlag = true;
                drawPanel.Invalidate();
    }
    

    And at the end I would suggest to use this code in your drawPanel_Paint instead of that of yours . You should use Graphics provided by an EventArgs, not create new one.

    private void drawPanel_Paint(object sender, PaintEventArgs e)
    {
                if (onMouseUpFlag)
                {
                      e.Graphics.DrawLine(p, ps, pe); 
                }
    }
    

提交回复
热议问题