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 141 142 143 144 145 146 147 148 149 150 151
|
#include <windows.h>
#include "ids.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
_In_ PWSTR szCmdLine, _In_ int nWinMode)
{
const WCHAR szWinName[] = L"DisplayText";
WNDCLASSW wc;
wc.hInstance = hInstance;
wc.lpszClassName = szWinName;
wc.lpfnWndProc = WndProc;
wc.style = 0;
wc.hIcon = (HICON) LoadImageW(NULL, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
wc.hCursor = (HCURSOR) LoadImageW(NULL, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
if (RegisterClassW(&wc) == 0)
{
MessageBoxW(NULL, L"Couldn't Register the Window Class!", L"ERROR", MB_OK | MB_ICONERROR);
return E_FAIL;
}
const WCHAR szAppTitle[] = L"Demonstrating Text Output";
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"Couldn't Create the Main Window!", L"ERROR", MB_OK | MB_ICONERROR);
return E_FAIL;
}
ShowWindow(hwnd, nWinMode);
UpdateWindow(hwnd);
HACCEL hAccel = LoadAcceleratorsW(hInstance, MAKEINTRESOURCE(IDR_ACCEL));
MSG msg;
while (GetMessageW(&msg, NULL, 0, 0))
{
if (TranslateAcceleratorW(hwnd, hAccel, &msg) == 0)
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static WCHAR str[255];
static int X = 0; // current output location
static int Y = 0; // current output location
static int maxX; // screen dimensions
static int maxY; // screen dimensions
SIZE size;
TEXTMETRIC tm;
UINT response;
HDC hdc;
switch (message)
{
case WM_CREATE:
// get the screen dimensions
maxX = GetSystemMetrics(SM_CXSCREEN);
maxY = GetSystemMetrics(SM_CYSCREEN);
return 0;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_EXIT:
response = MessageBoxW(hwnd, L"Quit the Program?", L"Exit", MB_YESNO);
if (response == IDYES)
{
PostQuitMessage(S_OK);
}
return S_OK;
case IDM_HELP:
MessageBoxW(hwnd, L"F2: Display\nF3: Reset", L"Help", MB_OK);
return S_OK;
case IDM_SHOW:
hdc = GetDC(hwnd);
// set the text color to black
SetTextColor(hdc, RGB(0, 0, 0));
// set the background color to turquoise
SetBkColor(hdc, RGB(0, 255, 255));
// get the text metrics
GetTextMetricsW(hdc, &tm);
wsprintfW(str, L"The font is %ld pixels high.", tm.tmHeight);
TextOutW(hdc, X, Y, str, lstrlenW(str));
Y += tm.tmHeight + tm.tmExternalLeading;
lstrcpyW(str, L"This is on the next line. ");
TextOutW(hdc, X, Y, str, lstrlenW(str));
// compute the length of a string
GetTextExtentPoint32W(hdc, str, lstrlenW(str), &size);
wsprintfW(str, L"Previous string is %ld units long.", size.cx);
X = size.cx;
TextOutW(hdc, X, Y, str, lstrlenW(str));
X = 0;
Y += tm.tmHeight + tm.tmExternalLeading;
wsprintfW(str, L"Screen dimensions: %d x %d", maxX, maxY);
TextOutW(hdc, X, Y, str, lstrlenW(str));
Y += tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hwnd, hdc);
return S_OK;
case IDM_RESET:
X = Y = 0;
InvalidateRect(hwnd, NULL, TRUE);
return S_OK;
}
break;
case WM_DESTROY:
PostQuitMessage(S_OK);
return S_OK;
}
return DefWindowProcW(hwnd, message, wParam, lParam);
}
|