1 #include<windows.h> 2 LRESULT CALLBACK WndProc( 3 HWND hwnd, // handle to window 4 UINT uMsg, // message identifier 5 WPARAM wParam, // first message parameter 6 LPARAM lParam // second message parameter 7 ); 8 HINSTANCE hInstanc; 9 int WINAPI WinMain(HINSTANCE hInstance, 10 HINSTANCE hPrevInstance, 11 LPSTR lpCmdLine, 12 int nShowCmd ) 13 { 14 WNDCLASS wndclass; 15 wndclass.style=CS_HREDRAW|CS_VREDRAW; 16 wndclass.cbClsExtra=0; 17 wndclass.cbWndExtra=0; 18 wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); 19 wndclass.hCursor=LoadCursor(NULL,"IDC_ARROW"); 20 wndclass.hIcon=LoadIcon(NULL,"END"); 21 wndclass.hInstance=hInstance; 22 wndclass.lpfnWndProc=WndProc; 23 wndclass.lpszClassName="yqh"; 24 wndclass.lpszMenuName=NULL; 25 RegisterClass(&wndclass); 26 HWND hWnd; 27 hWnd=CreateWindow("yqh","xianshichengxu",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL); 28 ShowWindow(hWnd,SW_SHOWNORMAL); 29 UpdateWindow(hWnd); 30 MSG message; 31 while(GetMessage(&message,0,0,0)) 32 { 33 TranslateMessage(&message); 34 DispatchMessage(&message); 35 } 36 return 0; 37 } 38 LRESULT CALLBACK WndProc( 39 HWND hwnd, // handle to window 40 UINT uMsg, // message identifier 41 WPARAM wParam, // first message parameter 42 LPARAM lParam // second message parameter 43 ) 44 { 45 static HWND hwndradio1,hwndradio2,hwndradio3; 46 static BOOL flag=false; 47 switch (uMsg) 48 { 49 case WM_CREATE: 50 hwndradio1 = CreateWindow("button","yigeanniu",BS_RADIOBUTTON|WS_CHILD|WS_VISIBLE,10,10,240,25,hwnd,(HMENU)1,hInstanc,NULL); 51 break; 52 case WM_COMMAND: 53 switch (LOWORD(wParam)) 54 { 55 case 1: 56 if(flag==false){ 57 SendMessage(hwndradio1,BM_SETCHECK,1,0); 58 flag=true;} 59 else if(flag=true) 60 { 61 SendMessage(hwndradio1,BM_SETCHECK,0,0); 62 flag=false; 63 } 64 break; 65 } 66 break; 67 //问题在这两行 68 case WM_PAINT: 69 break; 70 case WM_CLOSE: 71 DestroyWindow(hwnd); 72 break; 73 case WM_DESTROY: 74 PostQuitMessage(0); 75 break; 76 default: 77 return DefWindowProc(hwnd,uMsg,wParam,lParam); 78 } 79 return 0; 80 }
67行下面这两行的WM_PAINT消息去掉后运行,直接就会显示按钮,要是有这两行,运行代码后总是要移动一下窗口才会显示,到底为什么?
我查了好多WM_PAINT消息的解释,没怎么看懂,希望各位帮个忙看看。
你加了68、69两行就表示你自己处理WM_PAINT,却什么都没干就直接返回了,当然不会显示
而去掉后就是使用DefWindowProc(hwnd,uMsg,wParam,lParam);来处理了,系统本来就有对应的方式来显示,也就会直接出现
楼上正解,如果你不需要自绘窗口的话一般就不需要处理WM_PAINT消息。