MFC OnDraw 与 OnPaint
我想知道MFC中的OnDraw()
和onpaint()
之间的区别。
搜索互联网一段时间后,我发现了一个有用的文章。总而言之,
wm_paint
将触发onpaint()
,它调用ondraw()
并传递cdc*
:void cview :: onpaint() { //标准涂料例程 CPAINTDC DC(this); OnPreparedC(& dc); OnDraw(& dc); }
另一个文章 还通过传递打印机DC调用
on Draw()
。因此,通过覆盖on Draw()
,您可以在一个功能中获得屏幕绘画和打印,这很方便。
我试图将我的语句放入ondraw()
和onpaint()
中。两者都可以正常工作。 OnDraw()
有点容易一些,因为它已经获得了指针pdc
。
I am wondering about the difference between OnDraw()
and OnPaint()
in MFC.
After searching the Internet for a while, I found a useful article. In summary,
WM_PAINT
will triggerOnPaint()
, which callsOnDraw()
and passes aCDC*
:void CView::OnPaint() { // standard paint routine CPaintDC dc(this); OnPrepareDC(&dc); OnDraw(&dc); }
Another article mentions that when printing a document,
OnPrint()
also callsOnDraw()
by passing a printer DC. Therefore, by overridingOnDraw()
, you get screen painting and printing both in one function, which is convenient.
I tried to put my statements for drawing in either OnDraw()
and OnPaint()
. Either can work well. OnDraw()
is a little easier because it has already gotten a pointer pDC
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设备上下文是一个古老的抽象概念。早在 1982 年,《计算机图形学:原理与实践》第一版中就对它们进行了描述。 >(可能更早)至今仍让人们感到困惑。
设备上下文的主要目的是抽象渲染设备(例如显示器、打印机、内存位图等)的特性并提供一致的接口。渲染到设备上下文中的代码通常不需要知道哪个设备最终使用渲染命令。
标题为在视图中绘图的文档条目接下来解释系统如何工作:简而言之,所有绘制都应该在接收设备上下文的
OnDraw
覆盖中执行。然后,系统提供的OnPaint
实现构造一个CPaintDC
并调用OnDraw
。到目前为止,这似乎是一种过于复杂的呈现窗口内容的方法。当您实现打印支持等功能时,事情就开始变得有意义了。现在您所要做的就是设置打印设备上下文并调用
OnDraw
。OnDraw
实现中没有任何内容需要更改。Device contexts are an ancient abstraction. They have been described as early as 1982 in the first edition of Computer Graphics: Principles and Practice (probably even earlier) and seem to confuse people to this day.
The primary purpose of a device context is to abstract peculiarities of render devices (such as displays, printers, in-memory bitmaps, etc.) and provide a coherent interface. Code that's rendering into a device context generally does not need to know, which device is ultimately consuming the render commands.
The documentation entry titled Drawing in a View goes on to explain how the system is intended to work: In short, all painting should be performed in an
OnDraw
override that receives a device context. The system-providedOnPaint
implementation then constructs aCPaintDC
and callsOnDraw
.Up to this point this seems to be just an overly complex way to render the contents of a window. Things start to make sense when you implement, say, printing support. Now all you have to do is set up a printing device context and call
OnDraw
. Nothing in yourOnDraw
implementation needs to change.