r/>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[i].nNumFonts = 0;
rc = EnumFontFamilies ((HDC)hdc, ffs[i].szFontFamily,
EnumSingleFontFamily,
(LPARAM)(PFONTFAMSTRUCT)&ffs[i]);
}
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[i].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[i].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;
}
当应用程序在OnCreateMain中处理WM_CREATE消息时,首先枚举不同的字体。此处调用EnumFontFamilies函数,其中FontFamily域设为NULL表示每个字系都将被枚举。在回调函数FontFamilyCallback里,字系的名字被复制进一个字符串数组里。
在处理WM_PAINT消息的过程里完成剩余的工作。OnPaint函数以惯常的标准方式开头,依然是先获取客户区域大小并调用BeginPaint函数,该函数返回窗口的设备环境句柄。接下来调用GetTextMetrics来计算默认字体的行高。之后进入循环,为OnCreateMain中枚举出的每个字系调用EnumFontFamilies函数。用于这个回调序列的回调过程是迄今为止比较复杂的代码。
用于枚举单个字体的回调过程PaintSingleFontFamily使用lParam参数来获得一个指向PAINTFONTINFO结构的指针。该结构包含当前垂直绘制坐标以及设备环境的句柄。通过使用lParam指针,FontList可以避免声明全局变量来和回调过程通信。
接下来回调过程使用传进来的LOGFONT指针创建字体。新字体随后被选进设备环境,之前被选择的字体句柄被保存在hOldFont中。使用本章前面提到的转换公式来计算被枚举的字体的磅值。接下来输出一行文本,显示字系名称以及该字体的磅值。没有使用DrawText,回调过程用的是ExtTextOut来绘制字符串的。