如何设置invalid region的值?

EricChan1986 2011-02-24 03:48:33
一个关于invalid region的小程序,希望其完成以下功能:
1.运行时显示invalid region的初始大小,并validate整个invalid region。
2.单击鼠标左键之后,将invalid region的大小设置为(10, 10, 10, 10),同时程序将这一数值显示出来。
代码如下:

#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("wm_size") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("WM_SIZE"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
static RECT rect, rect1;
TCHAR szFormat[] = TEXT("%4d***%4d***%4d***%4d");
static TCHAR buffer[10];
switch(msg)
{
case WM_LBUTTONDOWN:
SetRect(&rect1, 10, 10, 10, 10);
InvalidateRect(hwnd, &rect, FALSE);
UpdateWindow(hwnd);
return 0;

case WM_PAINT:
GetUpdateRect(hwnd, &rect, FALSE);
hdc = GetDC(hwnd);
TextOut(hdc, 0, 0, buffer, wsprintf(buffer, szFormat, rect.left, rect.top, rect.right, rect.bottom));
ValidateRect(hwnd, NULL);
ReleaseDC(hwnd, hdc);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}


VS2008下能编译成功并运行。 但是出现一个问题:
能满足目标1, 可是当单击鼠标之后,显示的值却并没有发生变化。
有人能帮帮我么?
谢谢。
...全文
276 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
EricChan1986 2011-02-24
  • 打赏
  • 举报
回复
看过几次之后明白了,完整思路应该是:

由于rect被杯具地定义为了null,所以InvalidateRect函数将invalid region定义为了整个client area。这样引起Window发送WM_PAINT消息。而由于rect是null,UpdateWindow并不会发送WM_PAINT消息给WndProc。

感谢部长点拨!
Eleven 2011-02-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ericchan1986 的回复:]
.................还是部长英明
可是为什么不是矩形的话他就不显示呢?
[/Quote]
MSDN里关于UpdateWindow是这样说明的

UpdateWindow
The UpdateWindow function updates the client area of the specified window by sending a WM_PAINT message to the window if the window's update region is not empty. The function sends a WM_PAINT message directly to the window procedure of the specified window, bypassing the application queue. If the update region is empty, no message is sent.
EricChan1986 2011-02-24
  • 打赏
  • 举报
回复
.................还是部长英明
可是为什么不是矩形的话他就不显示呢?
Eleven 2011-02-24
  • 打赏
  • 举报
回复
SetRect(&rect1, 10, 10, 10, 10);
你这个不是个矩形;
改成SetRect(&rect1, 10, 10, 100, 100);
myj870517 2011-02-24
  • 打赏
  • 举报
回复
下面的也改过来了?设个断点看看呢
EricChan1986 2011-02-24
  • 打赏
  • 举报
回复
抱歉,改过来了,可是还是不行
myj870517 2011-02-24
  • 打赏
  • 举报
回复
case WM_LBUTTONDOWN:
SetRect(&rect1, 10, 10, 10, 10);
InvalidateRect(hwnd, &rect, FALSE);
UpdateWindow(hwnd);
return 0;



rect1
rect
搞错了吗?

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧