开发者

WinForms paint cycle documentation?

Is th开发者_高级运维ere documentation on the paint cycle in WinForms?

When i am programming in Windows the paint cycle is usually of the form:

sent a WM_PAINT message
{
   call BeginPaint(&paintStruct)

      //BeginPaint sends WM_NCPAINT and WM_ERASEBKGND
      sent a WM_ERASEBKGND message
      {
         i can:
           - allow default processing (Windows will fill the area with the default background color (e.g. white)
           - erase and background myself (e.g. a gradient) and prevent default processing
           - do nothing (letting whatever was there before me stay there) and prevent default processing
      }

   perform whatever painting i desire on 
         paintStruct.hDC (Device Context)
         paintStruct.rcPaint (Invalid Rectangle)
   that was populated into paintStruct during BeginPaint

   call EndPaint()
}

This is all documented on MSDN: Windows Development\Graphics and Multimedia\Windows GDI\Painting and Drawing\About Painting and Drawing

i cannot find any such documentation about WinForms and its paint cycle. i can randomly find methods and events that have the name paint in them:

  • OnPaint (protected method "Raises the Paint event.")
  • OnPrint (protected method "Raises the Paint event.")
  • InvokePaint (protected method "Raises the Paint event for the specified control.")
  • Paint (public event)
  • InvokePaintBackground (protected method "Raises the PaintBackground event for the specified control.")
  • OnPaintBackground (protected method "Paints the background of the control.")

Note: Ignoring the fact that there is no PaintBackground event

Is there documentation describing the design relationship between these entities? Is there documentation on the paint cycle in WinForms?


It isn't substantially different from the native Windows paint cycle, the .NET events are raised by the corresponding Windows messages. Starting from the bottom, the messages are generated by a call to InvalidateRect(), either by the window manager or by the app itself. The .NET version is Control.Invalidate(). Windows keeps track of the update region for the window, deciding whether to deliver a WM_PAINT, WM_NCPAINT and WM_ERASEBKGND message.

The WM_PAINT and WM_ERASEBKGND messages are recognized by Control.WndProc() when the ControlStyles.UserPaint style is turned on. It calls the virtual OnPaint() and OnPaintBackground() methods. A derived control can override these methods to customize the painting as necessary. And must call the base method. Eventually that reaches the Control.OnPaint/Background method, that fires the Paint and PaintBackground events to allow other code to customize the painting.

The only other wrinkle is double-buffering, enabled by the DoubleBuffered property. Winforms creates a bitmap buffer for the control and runs OnPaintBackground() and OnPaint(), passing a Graphics object created from that bitmap. Then blits the bitmap to the screen.


Is this what you are looking for?

MSDN: Custom Control Painting and Rendering


OP Edit: For when Microsoft implements their next round of link breaking, the documentation's location is:

  • MSDN Library
    • Development Tools and Languages
      • Visual Studio 2010
        • Visual Studio
          • Creating Windows-Based Applications
            • Windows Forms
              • Getting Started with Windows Forms
                • Windows Forms Controls
                  • Development Custom Windows Forms Controls with the .NET Framework
                    • Custom Control Painting and Rendering

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜