【示例C语言控制台程序:判断一系列的字体是否存在】 #include <conio.h> #include <locale.h> #include <tchar.h> #include <Windows.h>
int CALLBACK FontExistsProc(const LOGFONT *lpelfe, const TEXTMETRIC *lpntme, DWORD FontType, LPARAM lParam) { return 0; // 直接终止EnumFontFamiliesEx函数的运行 }
// 判断字体是否存在的函数(1) BOOL FontExists(LPLOGFONT lpFont) { HDC hdc = GetDC(NULL); BOOL bResult = (EnumFontFamiliesEx(hdc, lpFont, FontExistsProc, (LPARAM)NULL, (DWORD)NULL) == 0); ReleaseDC(NULL, hdc); return bResult; }
// 判断字体是否存在的函数(2) BOOL FontNameExists(LPTSTR szFontName) { LOGFONT logfont; ZeroMemory(&logfont, sizeof(logfont)); logfont.lfCharSet = DEFAULT_CHARSET; lstrcpy(logfont.lfFaceName, szFontName); return FontExists(&logfont); }
int main(void) { int i; LPTSTR list[] = {TEXT("黑体"), TEXT("白体"), TEXT("楷体"), TEXT("Times New Roman"), TEXT("楷体_GB2312"), TEXT("Consolas")}; int n = sizeof(list) / sizeof(list[0]); // 字符串个数
setlocale(LC_ALL, "chs"); for (i = 0; i < n; i++) { if (FontNameExists(list[i])) _tprintf(TEXT("字体“%s”存在\n"), list[i]); else _tprintf(TEXT("字体“%s”不存在\n"), list[i]); }
_getch(); return 0; }
|