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
|
// INCLUDES ====================================================================
#include <windows.h>
// FUNCTION PROTOTYPES =========================================================
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
int MessageLoop();
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
// GLOBALS =====================================================================
// (sometimes a global makes things easier than passing as a local parameter, YMMV)
const WCHAR szWinName[] = L"WINMOD";
const WCHAR szAppTitle[] = L"Modular WinAPI Minimal Application";
// Visual Studio gets all warning whingy if WinMain ain't SALed up the wazoo.
//
// SAL: https://docs.microsoft.com/en-us/cpp/code-quality/understanding-sal?view=msvc-170
//
int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
_In_ PWSTR szCmdLine, _In_ int nWinMode)
{
if (InitApplication(hInstance) == FALSE)
{
return E_FAIL;
}
if (InitInstance(hInstance, nWinMode) == FALSE)
{
return E_FAIL;
}
return MessageLoop();
}
// initializes the window class data and registers the main window class
BOOL InitApplication(HINSTANCE hInstance)
{
WNDCLASSW wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = (HICON) LoadImageW(NULL, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_SHARED);
wc.hCursor = (HCURSOR) LoadImageW(NULL, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = szWinName;
if (RegisterClassW(&wc) == 0)
{
MessageBoxW(NULL, L"Can't Register the Window Class!", szWinName, MB_OK | MB_ICONERROR);
return FALSE;
}
else
{
return TRUE;
}
}
// initializes the instance data and creates a new window for each instance
BOOL InitInstance(HINSTANCE hInstance, int nWinMode)
{
HWND hwnd = CreateWindowW(szWinName, szAppTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
if (hwnd == NULL)
{
MessageBoxW(NULL, L"Can't Create the Main Window!", szWinName, MB_OK | MB_ICONERROR);
return FALSE;
}
ShowWindow(hwnd, nWinMode);
UpdateWindow(hwnd);
return TRUE;
}
// starts and runs the application's message loop
int MessageLoop()
{
MSG msg;
while (GetMessageW(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
const WCHAR szAboutLeft[] = L"This is a minimal modular WinAPI application.\nYou've pressed the left mouse button!";
const WCHAR szAboutRight[] = L"This is a minimal modular WinAPI application.\nYou've pressed the right mouse button!";
switch (message)
{
case WM_LBUTTONDOWN:
MessageBeep(MB_ICONEXCLAMATION);
MessageBoxW(hwnd, szAboutLeft, L"About", MB_OK | MB_ICONINFORMATION);
return 0;
case WM_RBUTTONDOWN:
MessageBeep(MB_ICONASTERISK);
MessageBoxW(hwnd, szAboutRight, L"About", MB_OK | MB_ICONINFORMATION);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProcW(hwnd, message, wParam, lParam);
}
|