c++ - 使用 EnumFontFamiliesEx 函数枚举时字体过多

标签 c++ c windows winapi fonts

我正在尝试创建一个字体列表供用户选择。我正在使用 EnumFontFamiliesEx 函数,但不幸的是,返回的字体列表太长了。有许多额外的字体看起来很无聊、重复、用于不同的语言,或者以其他方式不希望向用户显示。我的屏幕截图最能说明我试图过滤掉的垃圾。

我的调用代码 EnumFontFamiliesEx看起来像这样:

LOGFONT lf;
memset(&lf, 0, sizeof(lf));
lf.lfCharSet = DEFAULT_CHARSET;
// screenDC is result of CreateCompatibleDC(NULL)
EnumFontFamiliesEx(screenDC, &lf, GetFontsCallback, NULL, 0);

在按字母顺序排序并删除具有重复面孔名称的字体之后,结果列表如下所示:

enter image description here

如您所见,ChooseFont字体通用对话框显示了一个非常合理的字体列表,用户友好且有意义。另一方面,我的代码显示了一长串额外字体:以“@”开头的字体(为什么?它们甚至是为了什么?),Arial 字体的 3 种额外变体,以及其他几种用途不明的字体,如 Aheroni,安达卢斯、新悦椿、悦椿UPC等。太疯狂了。

如何过滤EnumFontFamiliesEx 返回的字体列表以便它完全匹配 ChooseFont 中显示的列表对话?

最佳答案

感谢 Jesse Good,我现在了解到 Windows 7 团队做出的一些疯狂不幸的设计决策。我还不会接受我自己的答案,因为如果其他人想出一种在 Windows 7 中使用此隐藏字体功能的方法,即使注册表项尚不存在(例如,可能通过使用未记录的 API 或其他一些诡计)并且他们的答案有效,我会接受它。

此过滤是通过在 Windows 7 控制面板中实际“隐藏”字体来完成的。默认情况下,其他语言环境的字体是隐藏的,但它们可以由用户显示。至少,这是这个想法。这是讨论此功能的 MSDN 页面:International Font Management .

以下是此页面和 MSDN 中其他附近页面的一些关键摘录(另请参阅 Windows 7 兼容性手册中的 http://msdn.microsoft.com/en-us/library/windows/desktop/dd371704(v=vs.85).aspx):

Starting with Windows 7, the font management infrastructure supports the hiding of fonts which are not appropriate for a user's font selection lists. ... This feature means users need no longer be faced with long lists of inappropriate fonts.

In Windows 7, there are no APIs for directly querying which fonts are hidden, or for setting fonts to be hidden. [emphasis mine] If you use the Windows ChooseFont API (Font common dialog) to enable font selection today, you will get the new behavior for free. The new Windows Scenic Ribbon (Font Controls) introduced in Windows 7 also supports this behavior and provides another reason to "Ribbonize" your applications.

When a font is selected into a device context, there is no effect on drawing due to the font being hidden. The EnumFontFamiliesEx function continues to enumerate fonts that are set to hidden. [emphasis mine; there is apparently no way to differentiate hidden and visible fonts with EnumFontFamiliesEx]

Note that charsets are a legacy notion corresponding to pre-Unicode character sets. [emphasis mine]

ChooseFont will only list the shown fonts and filter out the hidden fonts while displaying fonts in the list box. The additional flag (CF_INACTIVEFONTS) in the flags member of the CHOOSEFONT structure is added to allow you to display all the installed fonts in the font list, the same as ChooseFont behaved before Windows 7.

也就是说,除非您使用 ChooseFont 通用对话框或官方的 Windows 功能区控件(仅在 Windows Vista/7 上可用),否则您根本没有支持过滤隐藏字体的方法。互联网上的许多用户都在提示在 Windows 7 控制面板中隐藏字体似乎没有效果,这有什么奇怪或奇怪的吗?!? (我之前错误地发布了 MS Word 2010 过滤掉隐藏字体。它似乎没有,因为它们使用自己的自定义功能区控件,而不是 Windows 内置的功能区。有趣的是,Windows 7 字体控制面板,按设计,与 Microsoft 的一款旗舰产品不兼容,如果不将更强大的功能区转储到 Office 中,就无法使其兼容。)

根据 Jesse Good 发布的链接,我了解到隐藏字体存储在未记录的注册表项中。通过这个链接,以及对 Process Monitor 的一些实验和分析(查看堆栈跟踪和注册表访问),我了解到以下内容:

  • 功能区控件调用 FMS.DLL(字体管理服务)中名为 FmsGetFilteredFontList 的未记录函数。它的目的似乎很明显。真遗憾,他们懒得公开记录和维护它。
  • 这些设置存储在一个未记录的注册表项中,FMS.DLL 可以访问该注册表项。
  • 如果注册表项被删除,它会使用 FmsGetFilteredFontList 的默认设置重新创建,用于隐藏与当前输入语言无关的字体​​。
  • 在全新安装的 Windows 上创建的全新用户配置文件不包含与应隐藏哪些字体相关的任何注册表项。

因此,Jesse Good 发布的链接可能适用于许多/大多数情况,但不是 100% 的时间。如果它们不存在,您需要一种方法来可靠地重新创建这些注册表项(或至少假设默认值)。默认行为仍然是隐藏一些字体,即使注册表项已经消失(例如在新的用户配置文件中)。

关于c++ - 使用 EnumFontFamiliesEx 函数枚举时字体过多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11253827/

相关文章:

带有默认函数的 Windows 静态库

c# - 如何从 C# 调用 C++ DLL 导出函数

python - 多重继承/虚函数

C++ 带括号的取消引用(带迭代器)

c++ - 从构造函数初始化列表中调用重载的构造函数

C:全局定义多维char数组

c - 这个 fabs() 的实现是如何工作的?

c++ - address-of 运算符是否返回变量引用的值的地址

python - 32 位 Windows 10 上的 Tensorflow : is not a supported wheel on this platform; package missing in current win-32 channels, 没有匹配的发行版

c++ - 说明乘以 2 个常量整数时的 decltype 输出