OnDraw与OnPaint的区别

先看MSDN 怎么说:

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各子类中该函数的定义都不一样。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值