65.9K
CodeProject 正在变化。 阅读更多。
Home

XFont - 获取字体名称和文件信息

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.73/5 (13投票s)

2003年5月22日

CPOL

2分钟阅读

viewsIcon

115860

downloadIcon

2955

XFont 从注册表中检索字体文件的名称,并从字体文件中检索字体名称。

引言

XFont 包含两个函数,在尝试确定给定字体名称的字体文件名时,以及相反地,在尝试从字体文件查找字体名称时,这两个函数都很有用。 我已经在 之前的文章 中介绍了 GetFontFile()。 本文介绍 GetFontFile() 的一个新版本,不依赖 MFC,以及 GetFontProperties(),它们共同允许您找到字体名称或字体文件的名称。

首先,我要感谢 Philip Patrick 的工作,他研究了字体文件格式,从而提出了 GetFontProperties()。 我采用了他的代码,并删除了 MFC 依赖项,添加了 Unicode 支持,并将 CFile 文件 I/O 转换为内存映射文件。

XFont 函数

  • GetFontFile() - 查找字体文件的名称。
    ///////////////////////////////////////////////////////////////////////////////
    //
    // GetFontFile()
    //
    // Purpose:     Find the name of font file from the font name
    //
    // Parameters:  lpszFontName     - name of font
    //              lpszDisplayName  - pointer to buffer where font display name
    //                                 will be copied
    //              nDisplayNameSize - size of display name buffer in TCHARs
    //              lpszFontFile     - pointer to buffer where font file name
    //                                 will be copied
    //              nFontFileSize    - size of font file buffer in TCHARs
    //
    // Returns:     BOOL - TRUE = success
    //
    // Notes:       This is *not* a foolproof method for finding the name of a
    //              font file. If a font has been installed in a normal manner,
    //              and if it is in the Windows "Font" directory, then this method
    //              will probably work. It will probably work for most screen
    //              fonts and TrueType fonts. However, this method might not work
    //              for fonts that are created or installed dynamically, or that
    //              are specific to a particular device, or that are not installed
    //              into the font directory.
    // 
  • GetFontProperties() - 从字体文件获取字体名称。
    ///////////////////////////////////////////////////////////////////////////////
    //
    // GetFontProperties()
    //
    // Purpose:     Get font name from font file
    //
    // Parameters:  lpszFilePath - file path of font file
    //              lpFontPropsX - pointer to font properties struct
    //
    // Returns:     BOOL - TRUE = success
    // 

如何使用

要将 XFont 集成到您的应用程序中,您首先需要将以下文件添加到您的项目中

  • XFont.cpp
  • XFont.h

如果您在项目中包含使用预编译头文件的 XFont,则必须将 C/C++ 预编译头文件 设置更改为 不使用预编译头文件,用于 XFont.cpp

接下来,在适当的项目文件中包含头文件 XFont.h。 现在您可以开始使用 XFont 了。 XFont 的两个函数在 XFont 演示应用程序中得到了充分的演示。

演示应用

XFontTest.exe 演示程序测试 XFont 中的 API。 以下是一些输出示例

XFont screenshot

修订历史

版本 1.1 - 2003 年 5 月 21 日

  • GetFontProperties 中删除了 MFC 依赖项
  • CFile 文件 I/O 转换为内存映射文件
  • 添加了Unicode支持。
  • 与我的 GetFontFile 例程结合

版本 1.0

  • GetFontFileGetFontProperties 的初始(独立)发布

用法

此软件发布到公共领域。 您可以以任何您喜欢的方式使用它。 如果您修改或扩展它,请考虑在此处发布新代码供大家共享。 此软件按“原样”提供,不提供任何明示或暗示的保证。 我对因本软件造成的任何损害或业务损失不承担任何责任。

© . All rights reserved.