📄 clientwnd.c
字号:
//======================================================================
// ClientWnd - Client window code for FontList2
//
// Written for the book Programming Windows CE
// Copyright (C) 1998 Douglas Boling
//======================================================================
#include <windows.h> // For all that Windows stuff
#include "FontList2.h" // Program-specific stuff
extern HINSTANCE hInst;
BOOL fFirst = TRUE;
//----------------------------------------------------------------------
// Global data
//
FONTFAMSTRUCT ffs[FAMILYMAX];
INT sFamilyCnt = 0;
INT sVPos = 0;
INT sVMax = 0;
// Message dispatch table for ClientWindowProc
const struct decodeUINT ClientMessages[] = {
WM_CREATE, DoCreateClient,
WM_PAINT, DoPaintClient,
WM_VSCROLL, DoVScrollClient,
};
//----------------------------------------------------------------------
// InitClient - Client window initialization
//
int InitClient (HINSTANCE hInstance) {
WNDCLASS wc;
// Register application client window class.
wc.style = 0; // Window style
wc.lpfnWndProc = ClientWndProc; // Callback function
wc.cbClsExtra = 0; // Extra class data
wc.cbWndExtra = 0; // Extra window data
wc.hInstance = hInstance; // Owner handle
wc.hIcon = NULL, // Application icon
wc.hCursor = NULL; // Default cursor
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = CLIENTWINDOW; // Window class name
if (RegisterClass (&wc) == 0) return 1;
return 0;
}
//----------------------------------------------------------------------
// TermClient - Client window cleanup
//
int TermClient (HINSTANCE hInstance, int nDefRC) {
return nDefRC;
}
//======================================================================
// Font callback functions
//----------------------------------------------------------------------
// FontFamilyCallback - Callback function that enumerates the font
// families.
//
int CALLBACK FontFamilyCallback (CONST LOGFONT *lplf,
CONST TEXTMETRIC *lpntm,
DWORD nFontType, LPARAM lParam) {
int rc = 1;
// Stop enumeration if array filled.
if (sFamilyCnt >= FAMILYMAX)
return 0;
// Copy face name of font.
lstrcpy (ffs[sFamilyCnt++].szFontFamily, lplf->lfFaceName);
return rc;
}
//----------------------------------------------------------------------
// EnumSingleFontFamily - Callback function that enumerates the font
// families
//
int CALLBACK EnumSingleFontFamily (CONST LOGFONT *lplf,
CONST TEXTMETRIC *lpntm,
DWORD nFontType, LPARAM lParam) {
PFONTFAMSTRUCT pffs;
pffs = (PFONTFAMSTRUCT) lParam;
pffs->nNumFonts++; // Increment count of fonts in family.
return 1;
}
//----------------------------------------------------------------------
// PaintSingleFontFamily - Callback function that enumerates the font
// families.
//
int CALLBACK PaintSingleFontFamily (CONST LOGFONT *lplf,
CONST TEXTMETRIC *lpntm,
DWORD nFontType, LPARAM lParam) {
PPAINTFONTINFO ppfi;
TCHAR szOut[256];
INT nFontHeight, nPointSize;
TEXTMETRIC tm;
HFONT hFont, hOldFont;
ppfi = (PPAINTFONTINFO) lParam; // Translate lParam into
// structure pointer.
// Create the font from the LOGFONT structure passed.
hFont = CreateFontIndirect (lplf);
// Select the font into the device context.
hOldFont = SelectObject (ppfi->hdc, hFont);
// Get the height of the default font.
GetTextMetrics (ppfi->hdc, &tm);
nFontHeight = tm.tmHeight + tm.tmExternalLeading;
// Compute font size.
nPointSize = (lplf->lfHeight * 72) /
GetDeviceCaps(ppfi->hdc,LOGPIXELSY);
// Format string and paint on display.
wsprintf (szOut, TEXT ("%s Point:%d"), lplf->lfFaceName,
nPointSize);
ExtTextOut (ppfi->hdc, 25, ppfi->yCurrent, 0, NULL,
szOut, lstrlen (szOut), NULL);
// Update new draw point.
ppfi->yCurrent += nFontHeight;
// Deselect font and delete.
SelectObject (ppfi->hdc, hOldFont);
DeleteObject (hFont);
return 1;
}
//======================================================================
// Message handling procedures for ClientWindow
//----------------------------------------------------------------------
// ClientWndProc - Callback function for application window
//
LRESULT CALLBACK ClientWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
INT i;
//
// Search message list to see if we need to handle this
// message. If in list, call procedure.
//
for (i = 0; i < dim(ClientMessages); i++) {
if (wMsg == ClientMessages[i].Code)
return (*ClientMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoCreateClient - Process WM_CREATE message for window.
//
LRESULT DoCreateClient (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
HDC hdc;
INT i, rc;
//Enumerate the available fonts.
hdc = GetDC (hWnd);
rc = EnumFontFamilies ((HDC)hdc, (LPTSTR)NULL, FontFamilyCallback, 0);
for (i = 0; i < sFamilyCnt; i++) {
ffs[i].nNumFonts = 0;
rc = EnumFontFamilies ((HDC)hdc, ffs[i].szFontFamily,
EnumSingleFontFamily,
(LPARAM)(PFONTFAMSTRUCT)&ffs[i]);
}
ReleaseDC (hWnd, hdc);
return 0;
}
//----------------------------------------------------------------------
// DoPaintClient - Process WM_PAINT message for window.
//
LRESULT DoPaintClient (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PAINTSTRUCT ps;
RECT rect;
HDC hdc;
TEXTMETRIC tm;
INT nFontHeight, i;
TCHAR szOut[256];
PAINTFONTINFO pfi;
SCROLLINFO si;
hdc = BeginPaint (hWnd, &ps);
GetClientRect (hWnd, &rect);
// Get the height of the default font.
GetTextMetrics (hdc, &tm);
nFontHeight = tm.tmHeight + tm.tmExternalLeading;
// Initialize struct that is passed to enumerate function.
pfi.yCurrent = rect.top - sVPos;
pfi.hdc = hdc;
for (i = 0; i < sFamilyCnt; i++) {
// Format output string and paint font family name.
wsprintf (szOut, TEXT ("Family: %s Number of fonts:%d"),
ffs[i].szFontFamily, ffs[i].nNumFonts);
ExtTextOut (hdc, 5, pfi.yCurrent, 0, NULL,
szOut, lstrlen (szOut), NULL);
pfi.yCurrent += nFontHeight;
// Enumerate each family to draw a sample of that font.
EnumFontFamilies ((HDC)hdc, ffs[i].szFontFamily,
PaintSingleFontFamily,
(LPARAM)&pfi);
}
// Compute the total height of the text in the window.
if (fFirst) {
sVPos = 0;
sVMax = (pfi.yCurrent - rect.top) - (rect.bottom - rect.top);
si.cbSize = sizeof (si);
si.nMin = 0;
si.nMax = pfi.yCurrent;
si.nPage = rect.bottom - rect.top;
si.nPos = sVPos;
si.fMask = SIF_ALL;
SetScrollInfo (hWnd, SB_VERT, &si, TRUE);
fFirst = FALSE;
}
EndPaint (hWnd, &ps);
return 0;
}
//----------------------------------------------------------------------
// DoVScrollClient - Process WM_VSCROLL message for window.
//
LRESULT DoVScrollClient (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
RECT rect;
SCROLLINFO si;
INT sOldPos = sVPos;
GetClientRect (hWnd, &rect);
switch (LOWORD (wParam)) {
case SB_LINEUP:
sVPos -= 10;
break;
case SB_LINEDOWN:
sVPos += 10;
break;
case SB_PAGEUP:
sVPos -= rect.bottom - rect.top;
break;
case SB_PAGEDOWN:
sVPos += rect.bottom - rect.top;
break;
case SB_THUMBPOSITION:
sVPos = HIWORD (wParam);
break;
}
// Check range.
if (sVPos < 0)
sVPos = 0;
if (sVPos > sVMax)
sVPos = sVMax;
// If scroll position changed, update scrollbar and
// force redraw of window.
if (sVPos != sOldPos) {
si.cbSize = sizeof (si);
si.nPos = sVPos;
si.fMask = SIF_POS;
SetScrollInfo (hWnd, SB_VERT, &si, TRUE);
InvalidateRect (hWnd, NULL, TRUE);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -