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
|
// INCLUDES ====================================================================
#include <windows.h>
// FUNCTION PROTOTYPES =========================================================
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
// entry point for a Windows application =======================================
int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ PWSTR szCmdLine, _In_ int nWinMode)
{
// define the window class name
static const WCHAR szAppName[ ] = L"MinimalWindowsApp";
// create an instance of the window class structure
WNDCLASSEXW wc;
wc.cbSize = sizeof(WNDCLASSEXW);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = (HICON) LoadImage(NULL, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
wc.hIconSm = (HICON) LoadImage(NULL, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
wc.hCursor = (HCURSOR) LoadImage(NULL, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppName;
if (0 == RegisterClassExW(&wc))
{
MessageBoxW(NULL, L"Can't Register the Window Class!", szAppName, MB_OK | MB_ICONERROR);
return E_FAIL;
}
// define the application title
static const WCHAR szAppTitle[ ] = L"Win32 API Skeletal Application";
// create the window
HWND hwnd = CreateWindowW(szAppName, szAppTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
// check if the window was created, exit if fail
if (NULL == hwnd)
{
MessageBoxW(NULL, L"Unable to Create the Main Window!", szAppName, MB_OK | MB_ICONERROR);
return E_FAIL;
}
// show and update the window
ShowWindow(hwnd, nWinMode);
UpdateWindow(hwnd);
static BOOL bRet;
static MSG msg;
// enter the main message loop
while ((bRet = GetMessageW(&msg, NULL, 0, 0)) != 0) // 0 = WM_QUIT
{
// check for error
if (-1 == bRet)
{
// handle the error and possibly exit
// for this app simply report error and exit
MessageBoxW(NULL, L"Unable to Continue!", szAppName, MB_OK | MB_ICONERROR);
return E_FAIL;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
// the app is done, exit normally, return control to Windows
return (int) msg.wParam;
}
// processes the messages that Windows sends to the application ================
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// choose which Windows messages you want to use
switch (message)
{
case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
// draw some text centered in the client area
RECT rect;
GetClientRect(hwnd, &rect);
DrawTextW(hdc, L"Hello, Windows!", -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint(hwnd, &ps);
return S_OK;
case WM_DESTROY:
// exit the application
PostQuitMessage(0);
return S_OK;
}
// let Windows process any unhandled messages
return DefWindowProcW(hwnd, message, wParam, lParam);
}
|