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
|
/*-----------------------------------------
WHATSIZE.C -- What Size is the Window?
(c) Charles Petzold, 1998
-----------------------------------------*/
#define STRICT
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ PWSTR pCmdLine,
_In_ int nShowCmd)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(pCmdLine);
static WCHAR szAppName[] = L"WhatSize";
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = (HICON) LoadImageW(NULL, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_SHARED);
wndclass.hCursor = (HCURSOR) LoadImageW(NULL, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED);
wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClassW(&wndclass))
{
MessageBoxW(NULL, L"This program requires Windows NT!", szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindowW(szAppName, L"What Size is the Window?",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
while (GetMessageW(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return (int) msg.wParam;
}
void Show(HWND hwnd, HDC hdc, int xText, int yText, int iMapMode, WCHAR* szMapMode)
{
WCHAR szBuffer[60];
RECT rect;
SaveDC(hdc);
SetMapMode(hdc, iMapMode);
GetClientRect(hwnd, &rect);
DPtoLP(hdc, (PPOINT) &rect, 2);
RestoreDC(hdc, -1);
TextOutW(hdc, xText, yText, szBuffer,
wsprintf(szBuffer, L"%-20s %7d %7d %7d %7d", szMapMode,
rect.left, rect.right, rect.top, rect.bottom));
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static WCHAR szHeading[] = L"Mapping Mode Left Right Top Bottom";
static WCHAR szUndLine[] = L"------------ ---- ----- --- ------";
static int cxChar;
static int cyChar;
HDC hdc;
PAINTSTRUCT ps;
TEXTMETRIC tm;
switch (message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
GetTextMetricsW(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hwnd, hdc);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
SetMapMode(hdc, MM_ANISOTROPIC);
SetWindowExtEx(hdc, 1, 1, NULL);
SetViewportExtEx(hdc, cxChar, cyChar, NULL);
TextOutW(hdc, 1, 1, szHeading, lstrlen(szHeading));
TextOutW(hdc, 1, 2, szUndLine, lstrlen(szUndLine));
Show(hwnd, hdc, 1, 3, MM_TEXT, L"TEXT (pixels)");
Show(hwnd, hdc, 1, 4, MM_LOMETRIC, L"LOMETRIC (.1 mm)");
Show(hwnd, hdc, 1, 5, MM_HIMETRIC, L"HIMETRIC (.01 mm)");
Show(hwnd, hdc, 1, 6, MM_LOENGLISH, L"LOENGLISH (.01 in)");
Show(hwnd, hdc, 1, 7, MM_HIENGLISH, L"HIENGLISH (.001 in)");
Show(hwnd, hdc, 1, 8, MM_TWIPS, L"TWIPS (1/1440 in)");
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProcW(hwnd, message, wParam, lParam);
}
|