我们知道,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)
}