《Programming Microsoft Windows CE .NET, Third Edition》by Douglas Boling
在看第二章Drawing on the screen遇到一个问题,示例程序FontList是用来枚举字体之后显示出来。
但是我的问题是它枚举的字体为什么显示的那么大?而且在程序代码中也没有看到修改字体的高度.....
程序代码如下:
Listing 2-2: The FontList program enumerates all fonts in the system.
FontList.h
//================================================================
// Header file
//
// Written for the book Programming Windows CE
// Copyright (C) 2003 Douglas Boling
//======================================================================
// Returns number of elements
#define dim(x) (sizeof(x) / sizeof(x[0]))
//----------------------------------------------------------------------
// Generic defines and data types
//
struct decodeUINT { // Structure associates
UINT Code; // messages
// with a function.
LRESULT (*Fxn)(HWND, UINT, WPARAM, LPARAM);
};
struct decodeCMD { // Structure associates
UINT Code; // menu IDs with a
LRESULT (*Fxn)(HWND, WORD, HWND, WORD); // function.
};
//----------------------------------------------------------------------
// Program-specific structures
//
#define FAMILYMAX 24
typedef struct {
int nNumFonts;
TCHAR szFontFamily[LF_FACESIZE];
} FONTFAMSTRUCT;
typedef FONTFAMSTRUCT *PFONTFAMSTRUCT;
FontList.cpp
//======================================================================
// FontList - Lists the available fonts in the system
//
// Written for the book Programming Windows CE
// Copyright (C) 2003 Douglas Boling
//======================================================================
#include // For all that Windows stuff
#include "FontList.h" // Program-specific stuff
//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT ("FontList");
HINSTANCE hInst; // Program instance handle
FONTFAMSTRUCT ffs[FAMILYMAX];
INT sFamilyCnt = 0;
//======================================================================
// 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 fonts
//
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 draws a font
//
int CALLBACK PaintSingleFontFamily (CONST LOGFONT *lplf,
CONST TEXTMETRIC *lpntm,
DWORD nFontType, LPARAM lParam) {
PPAINTFONTINFO ppfi;
TCHAR szOut[256];
INT nFontHeight, nPointSize;
HFONT hFont, hOldFont;
// 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);
// Compute the height of the default font.
nFontHeight = lpntm->tmHeight + lpntm->tmExternalLeading;
// Update new draw point.
ppfi->yCurrent += nFontHeight;
// Deselect font and delete.
SelectObject (ppfi->hdc, hOldFont);
DeleteObject (hFont);
return 1;
}
//================================================================
// Message handling procedures for MainWindow
//
//----------------------------------------------------------------
// MainWndProc - Callback function for application window
//
LRESULT CALLBACK MainWndProc (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(MainMessages); i++) {
if (wMsg == MainMessages.Code)
return (*MainMessages.Fxn)(hWnd, wMsg, wParam, lParam);
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoCreateMain - Process WM_CREATE message for window.
//
LRESULT DoCreateMain (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.nNumFonts = 0;
rc = EnumFontFamilies ((HDC)hdc, ffs.szFontFamily,
EnumSingleFontFamily,
(LPARAM)(PFONTFAMSTRUCT)&ffs);
}
ReleaseDC (hWnd, hdc);
return 0;
}
//---------------------------------------------------------------
// DoPaintMain - Process WM_PAINT message for window.
//
LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PAINTSTRUCT ps;
RECT rect;
HDC hdc;
TEXTMETRIC tm;
INT nFontHeight, i;
TCHAR szOut[256];
PAINTFONTINFO pfi;
GetClientRect (hWnd, &rect);
hdc = BeginPaint (hWnd, &ps);
// 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;
pfi.hdc = hdc;
for (i = 0; i < sFamilyCnt; i++) {
// Format output string, and paint font family name.
wsprintf (szOut, TEXT("Family: %s "),
ffs.szFontFamily);
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.szFontFamily,
PaintSingleFontFamily,
(LPARAM)&pfi);
}
EndPaint (hWnd, &ps);
return 0;
}
//----------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PostQuitMessage (0);
return 0;
}