17.4 字体枚举
17.4.1 枚举函数
(1)EnumFontFamiliesEx函数
参数 |
含义 |
HDC hdc |
handle to DC |
LPLOGFONT lpLogfont |
传入LOGFONT结构的指针 注意:如果lfCharset=DEFAULT_CHARSET; lf.lfFaceName[0]=NULL,则枚举所有字体 |
FONTENUMPROC lpEnumFontFamExProc |
枚举回调函数 |
LPARAM lParam |
可以指定附加数据,会传到枚举的回调函数中 |
DWORD dwFlags |
没用,必须为0 |
(2)EnumFontFamExProc回调函数(在每找到一种字体,该函数就被调用一次)
参数 |
含义 |
ENUMLOGFONTEX *lpelfe |
指向含有字体的逻辑属性的ENUMLOGFONTEX结构的指针 |
NEWTEXTMETRICEX *lpntme |
指向含有字体物理属性的结构的指针 |
DWORD FontType |
指定字体类型,此参数可为下列值的组合。 DEVICE_FONTTYPE, RASTER_FONTTYPE, TRUETYPE_FONTTYPE |
LPARAM lParam |
由EnumFontFamiliesEx中lParam指定的数据 |
【EnumFont程序】枚举计算机上的所有字体
/*------------------------------------------------------------ ENUMFONT.C -- EnumFontFamiliesEx函数 (c) Charles Petzold, 1998 ------------------------------------------------------------*/ #include <windows.h> #define ID_LIST 1 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int CALLBACK EnumFontFamExProc(ENUMLOGFONTEX*, NEWTEXTMETRICEX*, DWORD, LPARAM); int DisplayFonts(HWND hwnd, HDC hdc); typedef struct { HWND hwndList; int iCount; } EnumData; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT("EnumFont"); HWND hwnd; MSG msg; WNDCLASSEX wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.cbSize = sizeof(WNDCLASSEX); wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(hInstance, szAppName); wndclass.hIconSm = LoadIcon(hInstance, szAppName); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; if (!RegisterClassEx(&wndclass)) { MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppName, // window class name TEXT("利用EnumFontFamiliesEx枚举字体"), // window caption WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // initial x position CW_USEDEFAULT, // initial y position 560, // initial x size 480, // initial y size NULL, // parent window handle NULL, // window menu handle hInstance, // program instance handle NULL); // creation parameters ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; static int cxChar, cyChar; static TEXTMETRIC tm; static HWND hwndList; static int iCount; TCHAR szBuffer[50]; switch (message) { case WM_CREATE: cxChar = LOWORD(GetDialogBaseUnits()); cyChar = HIWORD(GetDialogBaseUnits()); hwndList = CreateWindow(TEXT("listbox"), NULL, WS_CHILD | WS_VISIBLE | LBS_STANDARD, cxChar, cyChar * 3, cxChar * LF_FULLFACESIZE + GetSystemMetrics(SM_CXVSCROLL), cyChar * 25, hwnd, (HMENU)ID_LIST, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL); hdc = GetDC(hwnd); iCount = DisplayFonts(hwndList, hdc); ReleaseDC(hwnd, hdc); return 0; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); TextOut(hdc, 10, 20, szBuffer, wsprintf(szBuffer, TEXT("共有%d种字体"), iCount)); EndPaint(hwnd, &ps); return 0; case WM_SETFOCUS: SetFocus(hwndList); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); } int DisplayFonts(HWND hwnd, HDC hdc) { EnumData ed; LOGFONT lf; //以下两个设置表示枚举所有的字体 lf.lfCharSet = DEFAULT_CHARSET; lf.lfFaceName[0] = '