PC Camera开发日志(十七)------- CWindow::Invalidate()
2008-08-08 15:48
447 查看
先来澄清一下,在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:
MFC Library Reference
CWnd::UpdateWindow
Updates the client area by sending a WM_PAINT message if the update region is not empty.
prototype
void UpdateWindow( );
Remarks
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.
Example
Copy Code
// In this example a rectangle is drawn in a view.
// The OnChangeRect() function changes the dimensions
// of the rectangle and then calls CWnd::Invalidate() so the
// client area of the view will be redrawn next time the
// window is updated. It then calls CWnd::UpdateWindow
// to force the new rectangle to be painted.
void CTestView::OnChangeRect()
{
// Change Rectangle size.
m_rcBox = CRect(20, 20, 210, 210);
// Invalidate window so entire client area
// is redrawn when UpdateWindow is called.
Invalidate();
// Update Window to cause View to redraw.
UpdateWindow();
}
// On Draw function draws the rectangle.
void CTestView::OnDraw(CDC* pDC)
{
// .. Other draw code here.
pDC->Draw3dRect(m_rcBox, 0x00FF0000, 0x0000FF00);
}
Invalidate() MSDN Reference:
MFC Library Reference
CWnd::Invalidate
Invalidates the entire client area of CWnd.
void Invalidate(
BOOL bErase = TRUE
);
Parameters
bErase
Specifies whether the background within the update region is to be erased.
Remarks
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.
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.
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.
但是这个函数也仅仅能在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:
MFC Library Reference
CWnd::UpdateWindow
Updates the client area by sending a WM_PAINT message if the update region is not empty.
prototype
void UpdateWindow( );
Remarks
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.
Example
Copy Code
// In this example a rectangle is drawn in a view.
// The OnChangeRect() function changes the dimensions
// of the rectangle and then calls CWnd::Invalidate() so the
// client area of the view will be redrawn next time the
// window is updated. It then calls CWnd::UpdateWindow
// to force the new rectangle to be painted.
void CTestView::OnChangeRect()
{
// Change Rectangle size.
m_rcBox = CRect(20, 20, 210, 210);
// Invalidate window so entire client area
// is redrawn when UpdateWindow is called.
Invalidate();
// Update Window to cause View to redraw.
UpdateWindow();
}
// On Draw function draws the rectangle.
void CTestView::OnDraw(CDC* pDC)
{
// .. Other draw code here.
pDC->Draw3dRect(m_rcBox, 0x00FF0000, 0x0000FF00);
}
Invalidate() MSDN Reference:
MFC Library Reference
CWnd::Invalidate
Invalidates the entire client area of CWnd.
void Invalidate(
BOOL bErase = TRUE
);
Parameters
bErase
Specifies whether the background within the update region is to be erased.
Remarks
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.
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.
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.
相关文章推荐
- PC Camera开发日志(十一)------ TTPLAYER UI 分析
- PC Camera开发日志(二十二)------ 系统中控件默认的长宽值
- PC Camera 开发日志(二十三)------ WND的Z-Order
- PC Camera 开发日志(四)------ 菜鸟眼中安装包要做些什么
- PC Camera开发日志(一)-------- My work on ECam Project's Summary
- PC Camera开发日志(十三)------ 窗口贴图闪烁问题
- PC Camera 开发日志(八)------ 通用多属性页对话框制作(General Setting Dialog)
- PC Camera 开发日志(七)------ 利用Picture(Static)控件在Dialog上帖图
- PC Camera 开发日志(二十四)------ 网上免费图标下载地址
- PC Camera 开发日志(六)------ 一种可换皮肤的对话框实现
- PC Camera开发日志(二十一)---- 窗口透明化处理
- PC Camera 开发日志(九)------ Camera Preview和Record原理
- PC Camera 开发日志(五)------ 应用程序最小化系统托盘参考
- PC Camera开发日志(二)------ 流行Camera APP比较分析
- PC Camera开发日志(十四)---- 用IPicture在窗口中显示一幅 JPG 图象
- PC Camera开发日志(十八)------- 非模态对话框
- PC Camera开发日志(二十六)--- INI文件中某段SECTION删除
- PC Camera 开发日志(十九)------- 各种流行皮肤控件库
- PC Camera 开发日志(三)------ 从安装文件后文件反向分析APP
- PC端开发-学习日志