PC Camera开发日志(十七)------- CWindow::Invalidate()

来源:互联网 发布:mysql limit怎么分页 编辑:程序博客网 时间:2024/04/29 11:30

 先来澄清一下,在MyCam的换肤操作中,多处用到了Invalidate()函数,相信很多利用MFC或者ATL编写UI的人基本都会用到这个函数。

但是这个函数也仅仅能在MFC和ATL的Windows里面调用。

MSDN上有这样的一个帖子:

Invalidate is not a function in normal Windows programming in Visual C++ (using WinMain() and Message Handlers). But if you are using MFC/ATL, there are member function CWnd::Invalidate() in MFC and CWindow::Invalidate() in ATL that can be used to invalidate the client area.

 

接下来转我看到的一则比较详细的解释Invalidate()函数的作用。

=========================================================

InvalidateRect只是增加重绘区域,在下次WM_PAINT的时候才生效

InvalidateRect函数中的参数TRUE表示系统会在你画之前用背景色将所选区域覆盖一次,默认背景色为白色,可以通过设置BRUSH来改变背景色。

Invalidate()之后:
...OnPaint()->OnPrepareDC()->OnDraw()
所以只是刷新在OnPaint()和OnDraw()函数中的绘图语句。其它地方没有影响。

Invalidate标记一个需要重绘的无效区域,并不意味着调用该函数后就立刻进行重绘。类似于PostMessage(WM_PAINT),需要处理到WM_PAINT消息时才真正重绘。以为您Invalidate之后还有其他的语句正在执行,程序没有机会去处理WM_PAINT消息,但当函数执行完毕后,消息处理才得以进行。

 

Invalidate只是放一个WM_PAINT消息在队列里,不做别的,所以只有当当前函数返回后,进入消息循环,取出WM_PAINT,才执行PAINT,所以不管Invalidate放哪里,都是最后的。

 

InvalidateRect(hWnd,&rect,TRUE);向hWnd窗体发出WM_PAINT的消息,强制客户区域重绘制,rect是你指定要刷新的区域,此区域外的客户区域不被重绘,这样防止客户区域的一个局部的改动,而导致整个客户区域重绘而导致闪烁,如果最后的参数为TRUE,则还向窗体发送WM_ERASEBKGND消息,使背景重绘,当然在客户区域重绘之前。


UpdateWindow只向窗体发送WM_PAINT消息,在发送之前判断GetUpdateRect(hWnd,NULL,TRUE)看有无可绘制的客户区域,如果没有,则不发送WM_PAINT

如果希望立即刷新无效区域,可以在调用InvalidateRect之后调用UpdateWindow,如果客户区的任一部分无效,则UpdateWindow将导致Windows用WM_PAINT消息调用窗口过程(如果整个客户区有效,则不调用窗口过程). 这一WM_PAINT消息不进入消息队列,直接由WINDOWS调用窗口过程。窗口过程完成刷新以后立刻退出,WINDOWS将控制返回给程序中UpdateWindow调用之后的语句。(windows程序设计第5版 P98)

 

UpdateData()顺便说下,这个函数不是刷新界面用的。
UpdateData();参数为FALSE时,将界面上控件绑定的变量的数据导到控件内,参数为TRUE时,导入方向则相反。

=========================================================

UpdateWindow() MSDN Reference:

  1. MFC Library Reference  
  2. CWnd::UpdateWindow  
  3. Updates the client area by sending a WM_PAINT message if the update region is not empty.
  4. prototype
  5. void UpdateWindow( );
  6. Remarks
  7. The UpdateWindow member function sends a WM_PAINT message directly, bypassing the application queue. If the update region is empty, WM_PAINT is not sent.
  8. Example
  9.   Copy Code 
  10. // In this example a rectangle is drawn in a view. 
  11. // The OnChangeRect() function changes the dimensions 
  12. // of the rectangle and then calls CWnd::Invalidate() so the 
  13. // client area of the view will be redrawn next time the
  14. // window is updated.  It then calls CWnd::UpdateWindow 
  15. // to force the new rectangle to be painted.
  16. void CTestView::OnChangeRect() 
  17. {
  18.    // Change Rectangle size.
  19.    m_rcBox = CRect(20, 20, 210, 210);
  20.    // Invalidate window so entire client area 
  21.    // is redrawn when UpdateWindow is called.
  22.    Invalidate();   
  23.    // Update Window to cause View to redraw.
  24.    UpdateWindow();
  25. }
  26. // On Draw function draws the rectangle.
  27. void CTestView::OnDraw(CDC* pDC)
  28. {
  29. //  ..  Other draw code here.
  30.    pDC->Draw3dRect(m_rcBox, 0x00FF0000, 0x0000FF00);
  31. }

Invalidate() MSDN Reference:

  1. MFC Library Reference  
  2. CWnd::Invalidate  
  3. Invalidates the entire client area of CWnd.
  4. void Invalidate(
  5.    BOOL bErase = TRUE 
  6. );
  7. Parameters
  8. bErase
  9. Specifies whether the background within the update region is to be erased.
  10. Remarks
  11. The client area is marked for painting when the next WM_PAINT message occurs. The region can also be validated before a WM_PAINT message occurs by the ValidateRect or ValidateRgn member function.
  12. The bErase parameter specifies whether the background within the update area is to be erased when the update region is processed. If bErase is TRUE, the background is erased when the BeginPaint member function is called; if bErase is FALSE, the background remains unchanged. If bErase is TRUE for any part of the update region, the background in the entire region, not just in the given part, is erased. 
  13. Windows sends a WM_PAINT message whenever the CWnd update region is not empty and there are no other messages in the application queue for that window.

 

 

原创粉丝点击