1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| #include<windows.h> #include<tchar.h>
HWND ghMainWnd = 0;
bool InitWindowsApp(HINSTANCE instanceHandle, int show); int Run(); LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nShowCmd) { if (!InitWindowsApp(hInstance, nShowCmd)) return 0; return Run(); }
bool InitWindowsApp(HINSTANCE instanceHandle, int show) { WNDCLASS wc; wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = instanceHandle; wc.hIcon = LoadIcon(0, IDI_APPLICATION); wc.hCursor = LoadCursor(0, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName = 0; wc.lpszClassName = _T("BasicWndClass");
if (!RegisterClass(&wc)) { MessageBox(0, _T("RegisterClass FAILED"), 0, 0); return false; }
ghMainWnd = CreateWindow( _T("BasicWndClass"), _T("Win32Basic"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, instanceHandle, 0 );
if (ghMainWnd == 0) { MessageBox(0, _T("CreateWindow FAILED"), 0, 0); return false; }
ShowWindow(ghMainWnd, show); UpdateWindow(ghMainWnd);
return true; }
int Run() { MSG msg = { 0 }; while (msg.message != WM_QUIT) { if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { } } return (int)msg.wParam; }
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_LBUTTONDOWN: MessageBox(0, _T("Hello, World"), _T("Hello"), MB_OK); return 0;
case WM_KEYDOWN: if (wParam == VK_ESCAPE) DestroyWindow(ghMainWnd); return 0;
case WM_KEYUP: return 0;
case WM_DESTROY: PostQuitMessage(0); return 0; }
return DefWindowProc(hWnd, msg, wParam, lParam); }
|