问题描述
//问题详细描述://我想在父窗体上单击鼠标左键后,弹出一个子窗体,遇到的问题是:现在的代码运行的结果是子窗体无法显示,但如果将父窗//体WndProc函数里WM_PAINT处理中的break去掉(或者直接删去此消息),窗体就能正常显示,这是什么原因?//编译环境:VC6.0#include<windows.h>#include<tchar.h>#defineID_CHILD11constcharg_szClassName[]="PARENT";constcharchldClassName[]="CHILD";LRESULTCALLBACKWndProc(HWNDhwnd,UINTmsg,WPARAMwParam,LPARAMlParam);LRESULTCALLBACKChld1Proc(HWNDhwnd,UINTmsg,WPARAMwParam,LPARAMlParam);intWINAPIWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,LPSTRlpCmdLine,intnCmdShow){WNDCLASSwc;HWNDhwnd;MSGMsg;//父窗口类wc.style=CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;wc.lpfnWndProc=WndProc;wc.cbClsExtra=0;wc.cbWndExtra=0;wc.hInstance=hInstance;wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);wc.hCursor=LoadCursor(NULL,IDC_ARROW);wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);wc.lpszMenuName=NULL;wc.lpszClassName=g_szClassName;//注册父窗口类if(!RegisterClass(&wc)){MessageBox(NULL,"ParentRegisterfailed!","Error!",MB_ICONEXCLAMATION|MB_OK);return0;}//子窗口类wc.style=CS_HREDRAW|CS_VREDRAW;wc.lpfnWndProc=Chld1Proc;wc.cbClsExtra=0;wc.cbWndExtra=0;wc.hInstance=hInstance;wc.hIcon=NULL;wc.hCursor=LoadCursor(NULL,IDC_CROSS);wc.lpszClassName=chldClassName;//注册子窗口类if(!RegisterClass(&wc)){MessageBox(NULL,"chldRegisterfailed!","Error!",MB_ICONEXCLAMATION|MB_OK);return0;}//创建父窗口hwnd=CreateWindow(g_szClassName,"Parentwindow",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);if(hwnd==NULL){MessageBox(NULL,"ParentWindowCreationFailed!","Error!",MB_ICONEXCLAMATION|MB_OK);return0;}//显示父窗口ShowWindow(hwnd,nCmdShow);UpdateWindow(hwnd);//消息循环while(GetMessage(&Msg,NULL,0,0)>0){TranslateMessage(&Msg);DispatchMessage(&Msg);}returnMsg.wParam;}LRESULTCALLBACKWndProc(HWNDhwnd,UINTmsg,WPARAMwParam,LPARAMlParam){staticintcxClient,cyClient;staticHDChdc;PAINTSTRUCTps;staticHWNDhwnd_chld1;switch(msg){caseWM_CREATE:break;caseWM_LBUTTONDOWN:hwnd_chld1=CreateWindow(chldClassName,"Childwindow1",WS_CHILD|WS_CAPTION|WS_VISIBLE,0,0,0,0,hwnd,(HMENU)ID_CHILD1,(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL);if(hwnd_chld1==NULL){MessageBox(NULL,"ChildWindowCreationFailed!","Error!",MB_ICONEXCLAMATION|MB_OK);return0;}MoveWindow(hwnd_chld1,cxClient/8,cyClient/8,cxClient/2,cyClient/4,TRUE);break;caseWM_CLOSE:DestroyWindow(hwnd);break;caseWM_SIZE:cxClient=LOWORD(lParam);cyClient=HIWORD(lParam);break;caseWM_DESTROY:PostQuitMessage(0);break;caseWM_PAINT:break;default:returnDefWindowProc(hwnd,msg,wParam,lParam);}return0;}LRESULTCALLBACKChld1Proc(HWNDhwnd,UINTmsg,WPARAMwParam,LPARAMlParam){switch(msg){caseWM_CREATE:break;caseWM_PAINT:break;caseWM_CLOSE:DestroyWindow(hwnd);break;caseWM_DESTROY:PostQuitMessage(0);break;default:returnDefWindowProc(hwnd,msg,wParam,lParam);}return0;}