Chinaunix首页 | 论坛 | 博客
  • 博客访问: 101098
  • 博文数量: 34
  • 博客积分: 30
  • 博客等级: 民兵
  • 技术积分: 217
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-10 23:36
文章分类
文章存档

2013年(34)

我的朋友

分类: C/C++

2013-03-21 16:31:01

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

message is sent when the or 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  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各子类中该函数的定义都不一样。
阅读(1294) | 评论(0) | 转发(0) |
0

上一篇:工具栏悬浮停靠

下一篇:CButtonST

给主人留下些什么吧!~~