Why does PaintEventArgs.Graphics behave differently from Control.CreateGraphics?

后端 未结 2 1770
醉梦人生
醉梦人生 2021-01-27 17:26

I have written an event handler method and attached it to the Paint event of a Form (just the main window). This event sends a PaintEventArgs

相关标签:
2条回答
  • 2021-01-27 18:14

    You .NET WinForms kids really should learn the Win32 API. Pick up a copy of Petzold already!

    Why does g1 redraw the image in the whole window, while g2 only draws the new portion, even if I call g2.Clear() before drawing?

    Presumably g2 is a wrapper around a device context received from BeginPaint. I imagine WinForms wraps PAINTSTRUCT::rcPaint for you - this variable describes the area to be painted. This is expected behaviour - rather than burning CPU cycles redrawing your entire window every single time another window overlaps it by just one pixel, you can redraw ... just the one pixel!

    g2.Clear is presumably limited by rcPaint.

    g1 is probably a GetDC for the window - which gives you the entire surface to draw on.

    Why, with either Graphics object, is the image only redrawn when the window increases in size, and not when it is made smaller?

    The underlying window class probably doesn't have CS_HREDRAW or CS_VREDRAW window styles. Without these, there's no reason for the default behaviour to request you redraw the window when it gets smaller: Windows knows what the entire window looks like, it can clip the unwanted bits away. This is unlike when the window gets bigger, it doesn't know what to draw in the new area.

    If PaintEventArgs.Graphics can (or should) not be used for drawing, what is it used for? I would imagine it just prevents you from having to create a new Graphics instance if the form doesn't need to be redrawn; is there more to it that I'm missing?

    It's used for drawing. Unless you have some complex drawing requirements, you can use the PaintEventArgs.Graphics minimize the amount of painting that goes on to your window. (As above - this is a huge saver of CPU cycles and it's probably a simple wrapper over BeginPaint and EndPaint - which is how drawing to the client area is meant to be done.)

    0 讨论(0)
    1. Why does g1 redraw the image in the whole window, while g2 only draws the new portion, even if I call g2.Clear() before drawing?
      I can't understand this point, You are drawing full form in both cases. So both should draw the whole form, perhaps you had this behavior in a different code

    2. Why, with either Graphics object, is the image only redrawn when the window increases in size, and not when it is made smaller?
      Because you should call Invalidate() in the Form.Resize event to initiate the redraw

    3. If PaintEventArgs.Graphics can (or should) not be used for drawing, what is it used for? I would imagine it just prevents you from having to create a new Graphics instance if the form doesn't need to be redrawn; is there more to it that I'm missing?
      They are different, CreateGraphics is a temporary object that should be disposed immediately after use (or used within using block), Its used for simple tasks that does not require drawing, Such as MeasureString or to get Context information. For Drawing you always should use PaintEventArgs.Graphics. A good scenario where the difference will be obvious is when using double buffering, If you get the Graphics object using CreateGraphics you will get the Graphics object for the control, While getting it from PaintEventArgs will get you the buffering Bitmap.
    0 讨论(0)
提交回复
热议问题