如何获取给定字体的可用字体样式 [英] How to get available fonts style of a given font

查看:34
本文介绍了如何获取给定字体的可用字体样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建 2 个组合框.第一个组合包含 Windows 中安装的所有字体,第二个组合包含基于第一个组合框中所选字体的可用字体样式.

I want to create 2 combo boxes. First combo with all fonts installed in windows, second one with available font styles based on the selected font in the first combo box.

我已经使用 EnumFontFamiliesEx 函数安装了所有已安装的字体,但我不知道在哪里可以找到给定字体的所有可用字体样式.

I've got all installed fonts using EnumFontFamiliesEx function but I can't figure out where to find all available font styles for a give font.

我不想使用 CFontDialog

有什么想法吗?

推荐答案

我已经有一段时间没有这样做了,所以请考虑一下这是一个有根据的猜测.

It's been a while since I've done this, so consider this an educated guess.

您最初使用 lpszFamily 的空指针调用 EnumFontFamilies(文档现在有时错误地调用 lpLogfont).这为您提供了每个字体系列(例如 Arial)中的一种代表性字体.

You initially call EnumFontFamilies with a null pointer for lpszFamily (which the docs now sometimes incorrectly call lpLogfont). That gives you one representative font from each font family (e.g., Arial).

一旦用户选择了一个系列,您将再次使用 EnumFontFamilies,这次使用该系列的名称.然后应该枚举属于该字体系列的所有字体文件.可以检查传递给枚举回调的 NEWTEXTMETRIC 对象,看它是粗体、斜体等.

Once the user selects a family, you use EnumFontFamilies again, this time with the name of that family. That should then enumerate all of the font files that belong to that font family. The NEWTEXTMETRIC object passed to the enumeration callback can be inspected to see if it's bold, italic, etc.

请注意,对于 NEWTEXTMETRIC,您可能会得到一个仅指向 TEXTMETRIC 的指针.我不确定您应该如何知道额外的字段是否存在.

Be aware that for the NEWTEXTMETRIC, you might get a pointer to just a TEXTMETRIC. I'm not sure how you're supposed to know whether the extra fields are there or not.

您也可以使用 EnumFontFamiliesEx(注意 -Ex 后缀)来执行此操作.文档有一个表格,告诉您将根据您在 LOGFONT 中设置的约束来枚举什么.EnumFontFamiliesEx 的回调还提供了 elfFullName 字段,该字段应提供具有它支持的特定样式的字体名称(例如,Arial Black").请注意,这些名称采用 ANSI 格式,即使您使用的是宽 (Unicode") 版本的 API.

You might be able to do this with EnumFontFamiliesEx (note the -Ex suffix) as well. The docs have a table telling you what will be enumerated depending on what constraints you put in the LOGFONT. The callback for EnumFontFamiliesEx also gives the elfFullName field, which should give the name of the font with the particular style it supports (e.g., "Arial Black"). Note that these names are in ANSI, even if you use the wide ("Unicode") version of the API.

这篇关于如何获取给定字体的可用字体样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆