#include <windows.h>
#include <string.h>
#include <stdio.h>
#include "text.h"
LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
char szWinName[] = "MyWin"; /* name of window class */
char str[255]; /* holds output strings */
int X=0, Y=0; /* current output location */
int maxX, maxY; /* screen dimensions */
HDC memdc; /* store the virtual device handle */
HBITMAP hbit; /* store the virtual bitmap */
HBRUSH hbrush; /* store the brush handle */
HFONT holdf, hnewf; /* store the font handles */
int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst,
LPSTR lpszArgs, int nWinMode)
{
HWND hwnd;
MSG msg;
WNDCLASSEX wcl;
HACCEL hAccel;
/* Define a window class. */
wcl.cbSize = sizeof(WNDCLASSEX);
wcl.hInstance = hThisInst; /* handle to this instance */
wcl.lpszClassName = szWinName; /* window class name */
wcl.lpfnWndProc = WindowFunc; /* window function */
wcl.style = 0; /* default style */
wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION); /* standard icon */
wcl.hIconSm = LoadIcon(NULL, IDI_APPLICATION); /* small icon */
wcl.hCursor = LoadCursor(NULL, IDC_ARROW); /* cursor style */
/* specify name of menu resource */
wcl.lpszMenuName = "FontMenu"; /* main menu */
wcl.cbClsExtra = 0; /* no extra */
wcl.cbWndExtra = 0; /* information needed */
/* Make the window white. */
wcl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
/* Register the window class. */
if(!RegisterClassEx(&wcl)) return 0;
/* Now that a window class has been registered, a window
can be created. */
hwnd = CreateWindow(
szWinName, /* name of window class */
"Using Built-in Fonts", /* title */
WS_OVERLAPPEDWINDOW, /* window style - normal */
CW_USEDEFAULT, /* X coordinate - let Windows decide */
CW_USEDEFAULT, /* Y coordinate - let Windows decide */
CW_USEDEFAULT, /* width - let Windows decide */
CW_USEDEFAULT, /* height - let Windows decide */
HWND_DESKTOP, /* no parent window */
NULL, /* no override of class menu */
hThisInst, /* handle of this instance of the program */
NULL /* no additional arguments */
);
/* Load accelerators. */
hAccel = LoadAccelerators(hThisInst, "FontMenu");
/* Display the window. */
ShowWindow(hwnd, nWinMode);
UpdateWindow(hwnd);
/* Create the message loop. */
while(GetMessage(&msg, NULL, 0, 0))
{
if(!TranslateAccelerator(hwnd, hAccel, &msg))
{
TranslateMessage(&msg); /* translate keyboard messages */
DispatchMessage(&msg); /* return control to Windows 98 */
}
}
return msg.wParam;
}
/* This function is called by Windows 98 and is passed
messages from the message queue.
*/
LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT paintstruct;
static TEXTMETRIC tm;
SIZE size;
static fontswitch = 0;
int response;
switch(message)
{
case WM_CREATE:
/* get screen coordinates */
maxX = GetSystemMetrics(SM_CXSCREEN);
maxY = GetSystemMetrics(SM_CYSCREEN);
/* create a virtual window */
hdc = GetDC(hwnd);
memdc = CreateCompatibleDC(hdc);
hbit = CreateCompatibleBitmap(hdc, maxX, maxY);
SelectObject(memdc, hbit);
hbrush = (HBRUSH) GetStockObject(WHITE_BRUSH);
SelectObject(memdc, hbrush);
PatBlt(memdc, 0, 0, maxX, maxY, PATCOPY);
/* get new font */
hnewf = (HFONT) GetStockObject(ANSI_VAR_FONT);
ReleaseDC(hwnd, hdc);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDM_SHOW:
/* get text metrics */
GetTextMetrics(memdc, &tm);
sprintf(str, "The font is %ld pixels high.",
tm.tmHeight);
TextOut(memdc, X, Y, str, strlen(str));
Y = Y + tm.tmHeight + tm.tmExternalLeading; /* next line */
strcpy(str, "This is on the next line. ");
TextOut(memdc, X, Y, str, strlen(str));
/* compute length of a string */
GetTextExtentPoint32(memdc, str, strlen(str), &size);
sprintf(str, "Previous string is %ld units long",
size.cx);
X = size.cx; /* advance to end of previous string */
TextOut(memdc, X, Y, str, strlen(str));
Y = Y + tm.tmHeight + tm.tmExternalLeading; /* next line */
X = 0; /* reset X */
sprintf(str, "Screen dimensions: %d %d", maxX, maxY);
TextOut(memdc, X, Y, str, strlen(str));
Y = Y + tm.tmHeight + tm.tmExternalLeading; /* next line */
InvalidateRect(hwnd, NULL, 1);
break;
case IDM_RESET:
X = Y = 0;
/* erase by repainting background */
PatBlt(memdc, 0, 0, maxX, maxY, PATCOPY);
InvalidateRect(hwnd, NULL, 1);
break;
case IDM_FONT:
if(!fontswitch)
{ /* switch to new font */
holdf = (HFONT) SelectObject(memdc, hnewf);
fontswitch = 1;
}
else { /* switch to old font */
SelectObject(memdc, holdf);
fontswitch = 0;
}
break;
case IDM_EXIT:
response = MessageBox(hwnd, "Quit the Program?",
"Exit", MB_YESNO);
if(response == IDYES) PostQuitMessage(0);
break;
case IDM_HELP:
MessageBox(hwnd, "F2: Display\nF3: Change Font\nF4:Reset", "Help", MB_OK);
break;
}
break;
case WM_PAINT: /* process a repaint request */
hdc = BeginPaint(hwnd, &paintstruct); /* get DC */
/* copy virtual window onto screen */
BitBlt(hdc, 0, 0, maxX, maxY, memdc, 0, 0, SRCCOPY);
EndPaint(hwnd, &paintstruct); /* release DC */
break;
case WM_DESTROY: /* terminate the program */
DeleteDC(memdc);
PostQuitMessage(0);
break;
default:
/* Let Windows 98 process any messages not specified in
the preceding switch statement. */
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
评论0