使用默认窗口处理函数,源码
1 #include<Windows.h> 2 #include<Windowsx.h> 3 4 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); 5 LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); 6 7 int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow) 8 { 9 WNDCLASS WndClass; 10 TCHAR* ClassName = TEXT("MyClass"); 11 HWND hwnd; 12 MSG msg; 13 HBRUSH hBrush = CreateSolidBrush(RGB(200, 200, 200)); 14 15 WndClass.cbClsExtra = 0; 16 WndClass.cbWndExtra = 0; 17 WndClass.hbrBackground = hBrush; 18 WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); 19 WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 20 WndClass.hInstance = hInst; 21 WndClass.lpfnWndProc = WindProc; 22 WndClass.lpszClassName = ClassName; 23 WndClass.lpszMenuName = NULL; 24 WndClass.style = CS_VREDRAW | CS_HREDRAW; 25 26 if (!RegisterClass(&WndClass)) 27 { 28 MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK); 29 return 0; 30 } 31 32 //CreateWindow返回之前,会发送WM_CREATE消息 33 hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInst, NULL); 34 if (hwnd == NULL) 35 { 36 MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK); 37 return 0; 38 } 39 ShowWindow(hwnd, nShow); 40 UpdateWindow(hwnd); 41 42 while (GetMessage(&msg, NULL, 0, 0)) 43 { 44 TranslateMessage(&msg); 45 DispatchMessage(&msg); 46 } 47 48 return 0; 49 } 50 51 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 52 { 53 PAINTSTRUCT pt; 54 static int cx, cy; 55 HDC hdc; 56 HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 240)); 57 switch (message) 58 { 59 case WM_CREATE: 60 //SetWindowLong(hwnd, GWL_WNDPROC, (LONG)MyProc); 61 return 0; 62 case WM_SIZE: 63 cx = LOWORD(lParam); 64 cy = HIWORD(lParam); 65 return 0; 66 case WM_PAINT: 67 hdc = BeginPaint(hwnd, &pt); 68 HBRUSH hBrush = CreateSolidBrush(RGB(0,255,0)); 69 SelectObject(hdc, hBrush); 70 Rectangle(hdc, 0, 0, cx/2, cy/2); 71 DeleteObject(hBrush); 72 EndPaint(hwnd, &pt); 73 return 0; 74 case WM_DESTROY: 75 PostQuitMessage(0); 76 return 0; 77 default: 78 break; 79 } 80 81 return DefWindowProc(hwnd, message, wParam, lParam); 82 } 83 84 LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 85 { 86 PAINTSTRUCT ps; 87 HDC hdc; 88 static int cx, cy; 89 90 switch (message) 91 { 92 case WM_SIZE: 93 cx = LOWORD(lParam); 94 cy = HIWORD(lParam); 95 return 0; 96 case WM_PAINT: 97 hdc = BeginPaint(hwnd, &ps); 98 99 EndPaint(hwnd, &ps); 100 return 0; 101 default: 102 break; 103 } 104 return DefWindowProc(hwnd, message, wParam, lParam); 105 }