MFC OnDraw 与 OnPaint

发布于 2025-01-18 22:36:04 字数 928 浏览 0 评论 0原文

我想知道MFC中的OnDraw()onpaint()之间的区别。

搜索互联网一段时间后,我发现了一个有用的文章。总而言之,

  1. wm_paint将触发onpaint(),它调用ondraw()并传递cdc*

      void cview :: onpaint()
    {
        //标准涂料例程
        CPAINTDC DC(this);
        OnPreparedC(& dc);
        OnDraw(& dc);
    }
     
  2. 另一个文章 还通过传递打印机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,

  1. WM_PAINT will trigger OnPaint(), which calls OnDraw() and passes a CDC*:

    void CView::OnPaint()
    {
        // standard paint routine
        CPaintDC dc(this);
        OnPrepareDC(&dc);
        OnDraw(&dc);
    }
    
  2. Another article mentions that when printing a document, OnPrint() also calls OnDraw() by passing a printer DC. Therefore, by overriding OnDraw(), 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 技术交流群。

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

发布评论

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

评论(1

谁的新欢旧爱 2025-01-25 22:36:05

设备上下文是一个古老的抽象概念。早在 1982 年,《计算机图形学:原理与实践》第一版中就对它们进行了描述。 >(可能更早)至今仍让人们感到困惑。

设备上下文的主要目的是抽象渲染设备(例如显示器、打印机、内存位图等)的特性并提供一致的接口。渲染到设备上下文中的代码通常不需要知道哪个设备最终使用渲染命令。

标题为在视图中绘图的文档条目接下来解释系统如何工作:简而言之,所有绘制都应该在接收设备上下文的 OnDraw 覆盖中执行。然后,系统提供的 OnPaint 实现构造一个 CPaintDC 并调用 OnDraw

到目前为止,这似乎是一种过于复杂的呈现窗口内容的方法。当您实现打印支持等功能时,事情就开始变得有意义了。现在您所要做的就是设置打印设备上下文并调用 OnDrawOnDraw 实现中没有任何内容需要更改。

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-provided OnPaint implementation then constructs a CPaintDC and calls OnDraw.

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 your OnDraw implementation needs to change.

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