WinForms 绘制周期文档?

发布于 2024-11-03 04:09:22 字数 2296 浏览 1 评论 0原文

WinForms 中有关于绘制周期的文档吗?

当我在 Windows 中编程时,绘制周期通常采用以下形式:

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()
}

这全部记录在 MSDN 上: Windows Development\Graphics and Multimedia\Windows GDI\Painting and Drawing\About Painting and Drawing

我找不到任何有关 WinForms 及其绘制周期的此类文档。我可以随机找到名称为paint的方法和事件:

  • OnPaint(受保护方法“引发 Paint 事件”。)
  • OnPrint(受保护方法“引发 Paint 事件”。)
  • InvokePaint(受保护方法“引发 Paint 事件”指定的控件。")
  • 画图(公共事件)
  • < code>InvokePaintBackground(受保护方法“引发指定控件的 PaintBackground 事件。”)
  • OnPaintBackground(受保护的方法“绘制控件的背景。”)

注意:忽略没有 PaintBackground 事件的事实

是否有文档描述这些实体之间的设计关系? WinForms 中有关于绘制周期的文档吗?

Is there 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橪书 2024-11-10 04:09:22

它与本机 Windows 绘制周期没有本质区别,.NET 事件由相应的 Windows 消息引发。从底部开始,消息是通过窗口管理器或应用程序本身调用 InvalidateRect() 生成的。 .NET 版本是 Control.Invalidate()。 Windows 跟踪窗口的更新区域,决定是否传递 WM_PAINT、WM_NCPAINT 和 WM_ERASEBKGND 消息。

当 ControlStyles.UserPaint 样式打开时,Control.WndProc() 可以识别 WM_PAINT 和 WM_ERASEBKGND 消息。它调用虚拟 OnPaint() 和 OnPaintBackground() 方法。派生控件可以重写这些方法以根据需要自定义绘制。并且必须调用基方法。最终到达 Control.OnPaint/Background 方法,该方法触发 Paint 和 PaintBackground 事件以允许其他代码自定义绘画。

唯一的其他问题是双缓冲,由 DoubleBuffered 属性启用。 Winforms 为控件创建一个位图缓冲区,并运行 OnPaintBackground() 和 OnPaint(),传递从该位图创建的 Graphics 对象。然后将位图传输到屏幕上。

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.

左秋 2024-11-10 04:09:22

这是您要找的吗?

MSDN:自定义控件绘制和渲染


OP 编辑​​:当微软实施下一轮链接中断时,文档的位置是:

  • MSDN 库
    • 开发工具和语言
      • Visual Studio 2010
        • 视觉工作室
          • 创建基于 Windows 的应用程序
            • Windows 窗体
              • Windows 窗体入门
                • Windows 窗体控件
                  • 使用 .NET Framework 开发自定义 Windows 窗体控件
                    • 自定义控件绘制和渲染

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文