1 #include "windows.h" 2 3 ATOM MyRegisterClass(HINSTANCE hInstance); 4 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow); 5 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); 6 7 HINSTANCE hInst; 8 9 //********************************************************* 10 // 主程序 11 //********************************************************* 12 int APIENTRY WinMain(HINSTANCE hInstance, 13 HINSTANCE hPrevInstance, 14 LPSTR lpCmdLine, 15 int nCmdShow) 16 { 17 MSG msg; 18 19 // 注册窗口类 20 MyRegisterClass(hInstance); 21 22 // 初始化 23 if (!InitInstance (hInstance, nCmdShow)) 24 { 25 return FALSE; 26 } 27 28 // 消息循环 29 while (GetMessage(&msg, NULL, 0, 0)) 30 { 31 TranslateMessage(&msg); 32 DispatchMessage(&msg); 33 } 34 35 return msg.wParam; 36 } 37 38 //********************************************************* 39 // 注册窗口类别函数 40 //********************************************************* 41 ATOM MyRegisterClass(HINSTANCE hInstance) 42 { 43 WNDCLASSEX wcex; 44 45 wcex.cbSize = sizeof(WNDCLASSEX); 46 wcex.style = CS_HREDRAW | CS_VREDRAW; 47 wcex.lpfnWndProc = WndProc; 48 wcex.cbClsExtra = 0; 49 wcex.cbWndExtra = 0; 50 wcex.hInstance = hInstance; 51 wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION); 52 wcex.hCursor = LoadCursor(NULL, IDC_ARROW); 53 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 54 wcex.lpszMenuName = NULL; 55 wcex.lpszClassName = TEXT("Test"); 56 wcex.hIconSm = NULL; 57 58 return RegisterClassEx(&wcex); 59 } 60 61 //********************************************************* 62 // 初始化函数 63 //********************************************************* 64 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 65 { 66 HWND hWnd; 67 hInst = hInstance; 68 69 hWnd = CreateWindow(TEXT("Test"), TEXT("Win32 Template") , WS_OVERLAPPEDWINDOW, 70 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 71 if (!hWnd) 72 { 73 return FALSE; 74 } 75 76 ShowWindow(hWnd, nCmdShow); 77 UpdateWindow(hWnd); 78 79 return TRUE; 80 } 81 82 //********************************************************* 83 // 窗口过程函数 84 //********************************************************* 85 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 86 { 87 PAINTSTRUCT ps; 88 HDC hdc; 89 90 switch (message) 91 { 92 case WM_PAINT: 93 hdc = BeginPaint(hWnd, &ps); 94 EndPaint(hWnd, &ps); 95 break; 96 case WM_DESTROY: 97 PostQuitMessage(0); 98 break; 99 default: 100 return DefWindowProc(hWnd, message, wParam, lParam);101 }102 103 return 0;104 }