Windows编程InvalidateRect()函数,该怎么解决
Windows编程InvalidateRect()函数
新手,我在处理某个消息,如WM_XXX时代码如下:
case WM_XXX:
1.InvalidateRect(某窗口句柄,假设为hwnd,NULL,TRUE);
2.UpdateWindow(hwnd);
3.然后在hwnd所指示的窗口下绘图。
break;
以上代码执行良好。
但是当我去掉UpdateWindow(hwnd)时,图形显示不出来,我觉得是在WM_PAINT消息被处理之前先执行了第三句绘图指令,而此时矩形是无效的,所以无法绘制(如有错误,求高手指正)。
然后,我把UpdateWindow(hwnd) 用for(int i=0;i<=10000;i++);代替(这里的10000可以用很大的数代替),旨在等待WM_PAINT消息被处理之后将矩形有效然后进入第三句话进行画图,可这样也不行,为什么?
------解决方案--------------------
InvalidateRect(某窗口句柄,假设为hwnd,NULL,TRUE);
2.UpdateWindow(hwnd);
这两句应该放在 3的最后,一般是绘制完图形然后进行刷新显示。
InvalidateRect函数只是置窗口区域为无效,UpdateWindow才真正发送wm_pain
1,2可用redrawwindow代替,更直接
------解决方案--------------------
InvalidateRect
The InvalidateRect function adds a rectangle to the specified window's update region. The update region represents the portion of the window's client area that must be redrawn.
BOOL InvalidateRect(
HWND hWnd, // handle of window with changed update region
CONST RECT *lpRect,
// address of rectangle coordinates
BOOL bErase // erase-background flag
);
Parameters
hWnd
Handle to the window whose update region has changed. If this parameter is NULL, the system invalidates and redraws all windows, and sends theWM_ERASEBKGND and WM_NCPAINT messages to the window procedure before the function returns.
lpRect
Pointer to a RECT structure that contains the client coordinates of the rectangle to be added to the update region. If this parameter is NULL, the entire client area is added to the update region.
bErase
Specifies whether the background within the update region is to be erased when the update region is processed. If this parameter is TRUE, the background is erased when the BeginPaint function is called. If this parameter is FALSE, the background remains unchanged.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero.
Windows NT: To get extended error information, callGetLastError.
Remarks
The invalidated areas accumulate in the update region until the region is processed when the next WM_PAINT message occurs or until the region is validated by using the ValidateRect or ValidateRgn function.
The system sends a WM_PAINT message to a window whenever its update region is not empty and there are no other messages in the application queue for that window.
If the bErase parameter is TRUE for any part of the update region, the background is erased in the entire region, not just in the given part.
Windows CE: The hWnd parameter cannot be NULL.
QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winuser.h.
Import Library: Use user32.lib.
See Also
Painting and Drawing Overview, Painting and Drawing Functions, BeginPaint, InvalidateRgn, RECT, ValidateRect, ValidateRgn,WM_ERASEBKGND, WM_NCPAINT, WM_PAINT
------解决方案--------------------
查了下MSDN的InvalidateRect,重点如下
Remarks
The invalidated areas accumulate in the update region until the region is processed when the next WM_PAINT message occurs or until the region is validated by using the ValidateRect or ValidateRgn function.
新手,我在处理某个消息,如WM_XXX时代码如下:
case WM_XXX:
1.InvalidateRect(某窗口句柄,假设为hwnd,NULL,TRUE);
2.UpdateWindow(hwnd);
3.然后在hwnd所指示的窗口下绘图。
break;
以上代码执行良好。
但是当我去掉UpdateWindow(hwnd)时,图形显示不出来,我觉得是在WM_PAINT消息被处理之前先执行了第三句绘图指令,而此时矩形是无效的,所以无法绘制(如有错误,求高手指正)。
然后,我把UpdateWindow(hwnd) 用for(int i=0;i<=10000;i++);代替(这里的10000可以用很大的数代替),旨在等待WM_PAINT消息被处理之后将矩形有效然后进入第三句话进行画图,可这样也不行,为什么?
windows编程
消息处理
InvalidateRect()函数
------解决方案--------------------
InvalidateRect(某窗口句柄,假设为hwnd,NULL,TRUE);
2.UpdateWindow(hwnd);
这两句应该放在 3的最后,一般是绘制完图形然后进行刷新显示。
InvalidateRect函数只是置窗口区域为无效,UpdateWindow才真正发送wm_pain
1,2可用redrawwindow代替,更直接
------解决方案--------------------
InvalidateRect
The InvalidateRect function adds a rectangle to the specified window's update region. The update region represents the portion of the window's client area that must be redrawn.
BOOL InvalidateRect(
HWND hWnd, // handle of window with changed update region
CONST RECT *lpRect,
// address of rectangle coordinates
BOOL bErase // erase-background flag
);
Parameters
hWnd
Handle to the window whose update region has changed. If this parameter is NULL, the system invalidates and redraws all windows, and sends theWM_ERASEBKGND and WM_NCPAINT messages to the window procedure before the function returns.
lpRect
Pointer to a RECT structure that contains the client coordinates of the rectangle to be added to the update region. If this parameter is NULL, the entire client area is added to the update region.
bErase
Specifies whether the background within the update region is to be erased when the update region is processed. If this parameter is TRUE, the background is erased when the BeginPaint function is called. If this parameter is FALSE, the background remains unchanged.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero.
Windows NT: To get extended error information, callGetLastError.
Remarks
The invalidated areas accumulate in the update region until the region is processed when the next WM_PAINT message occurs or until the region is validated by using the ValidateRect or ValidateRgn function.
The system sends a WM_PAINT message to a window whenever its update region is not empty and there are no other messages in the application queue for that window.
If the bErase parameter is TRUE for any part of the update region, the background is erased in the entire region, not just in the given part.
Windows CE: The hWnd parameter cannot be NULL.
QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winuser.h.
Import Library: Use user32.lib.
See Also
Painting and Drawing Overview, Painting and Drawing Functions, BeginPaint, InvalidateRgn, RECT, ValidateRect, ValidateRgn,WM_ERASEBKGND, WM_NCPAINT, WM_PAINT
------解决方案--------------------
查了下MSDN的InvalidateRect,重点如下
Remarks
The invalidated areas accumulate in the update region until the region is processed when the next WM_PAINT message occurs or until the region is validated by using the ValidateRect or ValidateRgn function.