×

新导航介绍,点击查看

OnDraw与OnPaint的区别

发表日期:2008-07-03 摄影器材: 点击数: 投票数:

CWnd::OnPaint 

afx_msg void OnPaint( );

Remarks

The framework calls this member function when Windows or an application makes a request to repaint a portion of an application’s window. The

WM_PAINT message is sent when the UpdateWindow or RedrawWindow member function is called.

 

CView::OnDraw

virtual void OnDraw( CDC* pDC ) = 0;

 

Remarks

Called by the framework to render an image of the document.

The framework calls this function to perform screen display, printing, and print preview, and it passes a different device context in each case. There is no default implementation.

You must override this function to display your view of the document.

If you need to distinguish between normal screen display and printing, call the IsPrinting member function of the device context.

区别已经很明显。OnPaint()是CWnd的成员函数,所有CWnd的子类都有一份OnPaint,而OnDraw()则是CView类中声明的纯虚函数。OnPaint()响应WM_PAINT消息,OnDraw不响应任何消息,只是一个虚函数。CWnd的子类分为四大类:FrameWindows, Dialog Boxes, Views, Controls,因此除了View及其子类使用OnDraw()外,其余三类都使用OnPaint()来重画。OnPaint()的工作是'Screen Display',而OnDraw()则可以做'Screen display, printing, print preview'等工作。

再看MFC源代码中如何做:
void CView::OnPaint()
{
    // standard paint routine
    CPaintDC dc(this);
    OnPrepareDC(&dc);
    OnDraw(&dc);
}
OnPaint()是CWnd类的非虚函数,CView中重载了该函数。从代码中可以看出,在CView响应WM_PAINT消息的情况下,在OnPaint中先调用了OnPrepareDC(),然后调用了OnDraw()。查MSDN,可以看到,
OnPrepareDC()的作用是:framework中在OnDraw()之前调用它,或者在OnPrint中调用它,前者是为了准备屏幕显示,后者是为了打印。MFC中OnPrepareDC的源代码是:
void CView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
    ASSERT_VALID(pDC);
    UNUSED(pDC); // unused in release builds

    // Default to one page printing if doc length not known
    if (pInfo != NULL)
        pInfo->m_bContinuePrinting =
            (pInfo->GetMaxPage() != 0xffff || (pInfo->m_nCurPage == 1));
}
这是CView中的定义,OnPrepareDC()是一个虚函数。完全可以改写它。在CView各子类中该函数的定义都不一样。
关键词:C

作者:fccc

《OnDraw与OnPaint的区别》


下一篇:没有了

最 新:
没有其它新的作品了

更多fccc的POCO作品...

评论