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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| #include <windows.h>
//The main window handle
HWND MainWindowHandle = 0;
//Necessary to initialise a Windows application
bool InitWindowApp(HINSTANCE instanceHandle, int show);
//Wraps the code to initialize a Windows application
int Run();
//The window procedure, handles events our window receives
LRESULT CALLBACK WndProc(HWND hWnd,
UINT msg,
WPARAM wParam,
LPARAM lParam);
//Windows equivalant to main()
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR pCmdLine,
int nShowCmd)
{
//First we create and initialize our Windows application
if(!InitWindowApp(hInstance, nShowCmd))
{
::MessageBox(0, L"Init - Failed", L"Error", MB_OK);
return 0;
}
//Once application has been created and initialized,
//we enter the message loop. We stay in the message
//loop until a WM_QUIT message is received,
//indicating the application should be terminated.
return Run(); //enter message loop
}
bool InitWindowApp(HINSTANCE instanceHandle, int show)
{
//Describe the window's characteristics by filling out a WNDCLASS structure
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 = static_cast<HBRUSH>(::GetStockObject(WHITE_BRUSH));
wc.lpszMenuName = 0;
wc.lpszClassName = L"Hello";
//Register the window class description
if(!::RegisterClass(&wc))
{
::MessageBox(0, L"RegisterClass - Failed", 0, 0);
return false;
}
//Create a window with the CreateWindow function
//Note, this function returns a HWND to the created window
//which we save in MainWindowHandle. Through MainWindowHandle
//we can reference this particular window we are creating
MainWindowHandle = ::CreateWindow(
L"Hello",
L"Hello",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
instanceHandle,
0);
if(MainWindowHandle == 0)
{
::MessageBox(0, L"CreateWindow - Failed", 0, 0);
return false;
}
::ShowWindow(MainWindowHandle, show);
::UpdateWindow(MainWindowHandle);
return true;
}
int Run()
{
MSG msg;
::ZeroMemory(&msg, sizeof(MSG));
//The function GetMessage will return 0 (false)
//when a WM_QUIT message is received.
while(::GetMessage(&msg, 0, 0, 0))
{
//Translate the message and then dispatch it
//to the appropriate window procedure.
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND windowHandle,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
switch(msg)
{
//In the case the left mouse button was pressed
//then display a messge box
case WM_LBUTTONDOWN:
::MessageBox(0, L"Hello World", L"Hello", MB_OK);
return 0;
//In the case the escape key was pressed
//then destroy the main application window,
//which is identified by MainWindowHandle.
case WM_KEYDOWN:
if(wParam == VK_ESCAPE)
{
::DestroyWindow(windowHandle);
return 0;
}
case WM_DESTROY:
::PostQuitMessage(0);
return 0;
}
return ::DefWindowProc(MainWindowHandle,
msg,
wParam,
lParam);
} |
Partager