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
|
#include <Windows.h>
// Store handles to the main window and application
// instance globally.
HWND ghMainWnd = 0;
HINSTANCE ghAppInst = 0;
// Step 1: Define and implement the window procedure.
LRESULT CALLBACK
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch( msg )
{
//Handle left mouse button click message.
case WM_LBUTTONDOWN:
MessageBoxA(0, "WM_LBUTTONDOWN message", "Msg", MB_OK);
return 0;
// Handle key down message.
case WM_KEYDOWN:
if( wParam == VK_ESCAPE )
if(MessageBoxA(0, "Do you want to exit?", "Msg", MB_YESNO) == IDYES)
{
DestroyWindow(ghMainWnd);
return 0;
}
// Handle destroy window message.
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}// end switch
// Forward any other messages we diddn;t handle to the
// defualt window procedure.
return DefWindowProc(hWnd, msg, wParam, lParam);
}// end WindProc
// WinMain: Entry point for a Windows Application.
int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR cmdLine, int showCmd)
{
// Save a handle to application instance.
ghAppInst = hInstance;
// Step 2: Fill out a WNDCLASS instance.
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = ghAppInst;
wc.hIcon = ::LoadIcon(0, IDI_APPLICATION);
wc.hCursor = ::LoadCursor(0, IDC_ARROW);
wc.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = L"MyWndClassName";
// Step 3: Register the WNDCLASS instance with Windows.
RegisterClass( &wc );
// Step 4: Create the window, and save handle in globle
// window handle variable ghMainWnd
ghMainWnd = ::CreateWindowA("MyWndClassName", "MyWindow",
WS_VSCROLL | WS_OVERLAPPEDWINDOW | WS_HSCROLL, 0, 0, 500, 500, 0, 0,
ghAppInst, 0);
if(ghMainWnd == 0)
{
::MessageBoxA(0, "CreateWindow - Failed", 0, 0);
return false;
}
// Step 5: Show and update the window
ShowWindow(ghMainWnd, showCmd);
UpdateWindow(ghMainWnd);
//Step 6: Enter the message loop and don't quit until
// a WM_QUIT message is received.
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
while( GetMessage(&msg, 0, 0, 0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Return exit code back to operating system.
return (int)msg.wParam;
}// End WinMain
|