CPaintDC及其与BeginUpdate、EndUpdate的前因后果

我们知道,CPaintDC类在其ctor和dtor中调用了BeginUpdate()和EndUpdate(),因此它也只能用在WM_PAINT的消息响应函数中,那为什么会这样呢?这样就要牵涉到BeginUpdate()和EndUpdate()做了那些事

 MSDN上说到:

The BeginPaint function automatically sets the clipping region of the device context to exclude any area outside the update region. The update region is set by the InvalidateRect or InvalidateRgn function and by the system after sizing, moving, creating, scrolling, or any other operation that affects the client area. If the update region is marked for erasing, BeginPaint sends a WM_ERASEBKGND message to the window.

An application should not call BeginPaint except in response to a WM_PAINT message. Each call to BeginPaint must have a corresponding call to the EndPaint function.

If the caret is in the area to be painted, BeginPaint automatically hides the caret to prevent it from being erased.

If the window's class has a background brush, BeginPaint uses that brush to erase the background of the update region before returning

 另外,最重要的一点是,EndUpdate(应该是它吧,我猜的)会validate所有的invalidate clipping area,且从消息队列中移除WM_PAINT消息!

而且,BeginUpdate()返回的是无效区域的dc句柄,所以,不能缓存该dc句柄!

 也因此,在MFC中重载OnPaint()时,wizard会自动提示你不要调用父类的OnPaint(),因为在子类的OnPaint()中wizard已经自动声明过一个局部CPaintDC对象了

就像这样:

void CHideSplitterWnd::OnPaint() 

{

    //注意临时变量声明的顺序,因为BeginPaint/EndPaint是不支持嵌套的

    //CPaintDC dc(this); // device context for painting,wizard自动给我们加上的

    

    // TODO: Add your message handler code here

    

    // Do not call CSplitterWnd::OnPaint() for painting messages(by wizard)

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值