WinForms 绘制周期文档?

标签 winforms paint

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 ( protected 方法“引发 Paint 事件。”)
  • OnPrint ( protected 方法“引发 Paint 事件。”)
  • InvokePaint ( protected 方法“引发指定控件的 Paint 事件。”)
  • Paint (公共(public)事件)
  • InvokePaintBackground ( protected 方法“引发指定控件的 PaintBackground 事件。”)
  • OnPaintBackground ( protected 方法“绘制控件的背景。”)

Note: Ignoring the fact that there is no PaintBackground event

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

最佳答案

它与 native 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 对象。然后将位图传输到屏幕。

关于WinForms 绘制周期文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5764508/

相关文章:

flutter - 如何在flutter中使用CustomPainter绘制圆环?

java - 组件 Paint 方法未绘制到 JPanel 的中间?

c# - Winforms IoC 容器 - 如何使用 Presenter 工厂处理具体类型

c# - 简单注入(inject)器 - Windows 窗体示例损坏

c# - 当我希望它显示为 1000 时,DataGridView 值显示为 1,000

java - 绘制用自己的 Painter 创建的 JPanel

delphi - 用颜色填充自定义区域

c# - 如何在 MVP 中将 EventArgs 从 View 传递给 Presenter?

c# - 如何动态添加(未知类型)控件到表单?

java - 重写paint()方法