为啥消息都给了父窗口?

time_is_life 2014-01-05 10:09:51
我在做<Windows程序设计>的Checker3例子。


#include <Windows.h>

#define DIVISIONS 5

LRESULT CALLBACK wWinProc( HWND, UINT, WPARAM, LPARAM );
LRESULT CALLBACK wChildWinProc( HWND, UINT, WPARAM, LPARAM );

TCHAR szChildClass[] = TEXT("Checker3_Child");

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCommandLine, int iCmdShow )
{
static TCHAR szAppName[] = TEXT("Checker3_Child");

HWND hWnd;
MSG msg;
WNDCLASS wndclass;

wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = wWinProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wndclass.hCursor = LoadIcon( 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;
}

wndclass.lpfnWndProc = wChildWinProc;
wndclass.cbWndExtra = sizeof( long );
wndclass.hIcon = NULL;
wndclass.lpszClassName = szChildClass;

RegisterClass( &wndclass );

hWnd = CreateWindow( szAppName, //windows class name
TEXT("Checker3 Mouse Hit-Test Demo"), // windows caption.
WS_OVERLAPPEDWINDOW, //windows style.
CW_USEDEFAULT, // initial X positon.
CW_USEDEFAULT, // initial Y positon.
CW_USEDEFAULT, // initial X size.
CW_USEDEFAULT, // initial Y positon.
NULL, //Parent window handle.
NULL, //window menu handle.
hInstance, // program instance handle.
NULL); // creation parameters.

ShowWindow( hWnd, iCmdShow );
UpdateWindow( hWnd );

while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}

return msg.wParam;

}

LRESULT CALLBACK wWinProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
static HWND hwndChild[DIVISIONS][DIVISIONS];
int cxBlock, cyBlock, x,y;

switch (message)
{
case WM_CREATE:
for( x = 0; x < DIVISIONS; x++ )
for( y = 0; y < DIVISIONS ; y++ )
hwndChild[x][y] = CreateWindow( szChildClass, NULL,
WS_CHILDWINDOW | WS_VISIBLE,
0, 0, 0, 0,
hwnd, (HMENU)( y << 8 | x ),
(HINSTANCE)GetWindowLong( hwnd, GWL_HINSTANCE ),
NULL );
return 0;
case WM_SIZE:
cxBlock = LOWORD( lParam ) / DIVISIONS;
cyBlock = HIWORD( lParam ) / DIVISIONS;

for( x = 0; x < DIVISIONS; x++ )
for( y = 0; y < DIVISIONS; y++ )
{
MoveWindow( hwndChild[x][y] ,
x * cxBlock, y * cyBlock,
cxBlock, cyBlock, TRUE );
}
return 0;
case WM_LBUTTONDOWN:
MessageBeep( 0 );
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}

return DefWindowProc( hwnd, message, wParam, lParam );
}



LRESULT CALLBACK wChildWinProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;

switch (message)
{
case WM_CREATE:
SetWindowLong( hwnd, 0, 0 );
return 0;
case WM_LBUTTONDOWN:
SetWindowLong( hwnd, 0, 1 ^ GetWindowLong( hwnd, 0 ));
InvalidateRect( hwnd, NULL, FALSE );
return 0;

case WM_PAINT:
hdc = BeginPaint( hwnd, &ps );

GetClientRect( hwnd, &rect);
Rectangle( hdc, 0, 0, rect.right, rect.bottom );

if( GetWindowLong( hwnd, 0 ) )
{
MoveToEx( hdc, 0, 0, NULL );
LineTo( hdc, rect.right, rect.bottom );
MoveToEx( hdc, 0, rect.bottom, NULL );
LineTo( hdc, rect.right, 0 );
}

EndPaint( hwnd, &ps );

return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}

return DefWindowProc( hwnd, message, wParam, lParam );
}



程序运行不正常,打断点跟踪,发现所有的消息都发送给了wWinProc, wChildWinProc没有收到任何消息。
为什么那?请高手指教,谢谢。
...全文
111 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
bluesky_ql 2014-01-06
  • 打赏
  • 举报
回复
同意一楼二楼,你这个错误实际上调试的时候很好找,但是因为你41行的代码没有判断返回值正确与否,导致后面你不知道发生了什么,所以判断返回值是否正确是个好的编程习惯
schlafenhamster 2014-01-05
  • 打赏
  • 举报
回复
注册失败 static TCHAR szAppName[] = TEXT("Checker3_Main"); static TCHAR szChildClass[] = TEXT("Checker3_Child");
encoderlee 版主 2014-01-05
  • 打赏
  • 举报
回复
把TCHAR szChildClass[] = TEXT("Checker3_Child"); 改成TCHAR szChildClass[] = TEXT("Checker3_Child2"); 就行了。 你两个窗口的类名不能相同,否则子窗口注册失败。
worldy 2014-01-05
  • 打赏
  • 举报
回复
szChildClass的窗口类在哪注册了窗口类?没看到注册

16,472

社区成员

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

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

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