上一篇有讲到在QT中从字体名获取文字的路径,这个是MFC版本的:

#QT从字体名获取字库文件路径#include <string>
using namespace std;void wcharTochar(const wchar_t *wchar, char *chr, int length)
{memset(chr,0x00,length * sizeof(char));WideCharToMultiByte( CP_ACP, 0, wchar, -1, chr, length, NULL, NULL );
}void charTowchar(const char *chr, wchar_t *wchar, int size)
{memset(wchar,0x00,size * sizeof(wchar_t));MultiByteToWideChar( CP_ACP, 0, chr, strlen(chr)+1, wchar, size/sizeof(wchar[0]) );
}// Get system font file path
int GetSystemFontFile(const std::string &faceName, char *pszFontPath) {static const char    fontRegistryPath[] = "Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts";HKEY               hKey;LONG               result;std::string          wsFaceName(faceName.begin(), faceName.end());// Open Windows font registry keyresult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, fontRegistryPath, 0, KEY_READ, &hKey);if (result != ERROR_SUCCESS) {return 0;}DWORD maxValueNameSize, maxValueDataSize;result = RegQueryInfoKey(hKey, 0, 0, 0, 0, 0, 0, 0, &maxValueNameSize, &maxValueDataSize, 0, 0);if (result != ERROR_SUCCESS) {return 0;}DWORD valueIndex = 0;char *valueName = new char[maxValueNameSize];LPBYTE valueData = new BYTE[maxValueDataSize];DWORD valueNameSize, valueDataSize, valueType;char    szFontFile[MAX_PATH] = {0x00};// Look for a matching font namedo {memset(szFontFile, 0x00, sizeof(szFontFile));valueDataSize = maxValueDataSize;valueNameSize = maxValueNameSize;result = RegEnumValue(hKey, valueIndex, valueName, &valueNameSize, 0, &valueType, valueData, &valueDataSize);valueIndex++;if (result != ERROR_SUCCESS || valueType != REG_SZ) {continue;}std::string wsValueName(valueName, valueNameSize);// Found a matchif (_tcsncicmp(wsFaceName.c_str(), wsValueName.c_str(), wsFaceName.length()) == 0) {wsprintf(szFontFile, "%s", valueData);break;}}while (result != ERROR_NO_MORE_ITEMS);delete[] valueName;delete[] valueData;RegCloseKey(hKey);if (strlen(szFontFile) == 0){return 0;}// Build full font file pathchar winDir[MAX_PATH] = {0x00};GetWindowsDirectory(winDir, MAX_PATH);wsprintf(pszFontPath, "%s\\Fonts\\%s", winDir, szFontFile);return strlen(pszFontPath);
}#define MAKE_TAG(ch1, ch2, ch3, ch4) (\(((UINT)(ch4)) << 24) | \(((UINT)(ch3)) << 16) | \(((UINT)(ch2)) << 8) | \((UINT)(ch1)) \)static inline USHORT getUShort(const unsigned char *p)
{USHORT val;val = *p++ << 8;val |= *p;return val;
}static char * getEnglishName(const UCHAR *table, UINT bytes)
{static char    szEnName[MAX_PATH] = {0x00};static wchar_t i18n_name[MAX_PATH] = {0x00};enum {NameRecordSize = 12,FamilyId = 1,MS_LangIdEnglish = 0x009};// get the name tableUSHORT count;USHORT string_offset;const unsigned char *names;int microsoft_id = -1;int apple_id = -1;int unicode_id = -1;if(getUShort(table) != 0)goto error;count = getUShort(table+2);string_offset = getUShort(table+4);names = table + 6;if(string_offset >= bytes || 6 + count*NameRecordSize > string_offset)goto error;for(int i = 0; i < count; ++i) {// search for the correct name entryUSHORT platform_id = getUShort(names + i*NameRecordSize);USHORT encoding_id = getUShort(names + 2 + i*NameRecordSize);USHORT language_id = getUShort(names + 4 + i*NameRecordSize);USHORT name_id = getUShort(names + 6 + i*NameRecordSize);if(name_id != FamilyId)continue;enum {PlatformId_Unicode = 0,PlatformId_Apple = 1,PlatformId_Microsoft = 3};USHORT length = getUShort(names + 8 + i*NameRecordSize);USHORT offset = getUShort(names + 10 + i*NameRecordSize);if(DWORD(string_offset + offset + length) >= bytes)continue;if ((platform_id == PlatformId_Microsoft&& (encoding_id == 0 || encoding_id == 1))&& (language_id & 0x3ff) == MS_LangIdEnglish&& microsoft_id == -1)microsoft_id = i;// not sure if encoding id 4 for Unicode is utf16 or ucs4...else if(platform_id == PlatformId_Unicode && encoding_id < 4 && unicode_id == -1)unicode_id = i;else if(platform_id == PlatformId_Apple && encoding_id == 0 && language_id == 0)apple_id = i;}{bool unicode = false;int id = -1;if(microsoft_id != -1) {id = microsoft_id;unicode = true;} else if(apple_id != -1) {id = apple_id;unicode = false;} else if (unicode_id != -1) {id = unicode_id;unicode = true;}if(id != -1) {USHORT length = getUShort(names + 8 + id*NameRecordSize);USHORT offset = getUShort(names + 10 + id*NameRecordSize);if(unicode) {// utf16length /= 2;//i18n_name.resize(length);wchar_t *uc = (wchar_t *) i18n_name;const unsigned char *string = table + string_offset + offset;for(int i = 0; i < length; ++i)uc[i] = getUShort(string + 2*i);memset(szEnName, 0x00, sizeof(szEnName));wcharTochar(i18n_name, szEnName, MAX_PATH);} else {// Apple Romanchar *uc = (char *) i18n_name;const unsigned char *string = table + string_offset + offset;for(int i = 0; i < length; ++i)uc[i] = string[i];memset(szEnName, 0x00, sizeof(szEnName));memcpy(szEnName, uc, strlen(uc));}}}
error://qDebug("got i18n name of '%s' for font '%s'", i18n_name.latin1(), familyName.toLocal8Bit().data());return szEnName;
}char * getEnglishName(const char *familyName)
{char *i18n_name = 0x00;HDC hdc = GetDC( 0 );LOGFONT lf;memset(&lf, 0, sizeof(LOGFONT));memcpy(lf.lfFaceName, familyName, strlen(familyName));lf.lfCharSet = DEFAULT_CHARSET;HFONT hfont = CreateFontIndirect(&lf);if(!hfont) {ReleaseDC(0, hdc);return i18n_name;}HGDIOBJ oldobj = SelectObject( hdc, hfont );const DWORD name_tag = MAKE_TAG( 'n', 'a', 'm', 'e' );// get the name tableunsigned char *table = 0;DWORD bytes = GetFontData( hdc, name_tag, 0, 0, 0 );if ( bytes == GDI_ERROR ) {// ### Unused variable/* int err = GetLastError(); */goto error;}table = new unsigned char[bytes];GetFontData(hdc, name_tag, 0, table, bytes);if ( bytes == GDI_ERROR )goto error;i18n_name = getEnglishName(table, bytes);
error:delete [] table;SelectObject( hdc, oldobj );DeleteObject( hfont );ReleaseDC( 0, hdc );//qDebug("got i18n name of '%s' for font '%s'", i18n_name.latin1(), familyName.toLocal8Bit().data());return i18n_name;
}

遍历系统所有可用字体:

BOOL CALLBACK EnumFontFamEx(LPLOGFONT lplf,LPTEXTMETRIC lptm,int iType,LPARAM lpData)
{// m_cList.AddString(lplf->lfFaceName);return 1;
}void main()
{// 遍历系统所有字体HDC     hdc = ::GetDC(NULL);LOGFONT lf;memset(&lf,0,sizeof(LOGFONT));lf.lfCharSet = DEFAULT_CHARSET;lf.lfFaceName[0] = NULL;m_cList.ResetContent();::EnumFontFamiliesEx(hdc,&lf,(FONTENUMPROC)EnumFontFamEx,0,0);::ReleaseDC(NULL,hdc);
}

根据faceName来获取字体ttc文件名:

void CEnumFontDlg::OnDblclkList1()
{// TODO: 在此添加控件通知处理程序代码char    szName[MAX_PATH] = {0x00};int      nSel = CB_ERR;char szPath[MAX_PATH] = {0x00};char *pPath = NULL;nSel = m_cList.GetCurSel();if(nSel == CB_ERR)return;// 获取到ListBox中双击选中的字体// 例如:宋体、微软雅黑m_cList.GetText(nSel, szName);pPath = getEnglishName(szName);if(pPath){GetSystemFontFile(pPath, szPath);AfxMessageBox(szPath);}
}

运行效果如下:

MFC从字体名获取字库文件路径(从宋体获取到simsun.ttc)相关推荐

  1. java 获取java文件路径_Java怎么获取相对路径下所有的.java文件的信息

    理论上不能,因为java可能动态创建并加载类. 实践上是可能, 假设你不动态创建类, 或动态创建也由你的ClassLoader来处理,或可能忽略那些不是你创建的类 则用Guava反射的方式:Refle ...

  2. java 获取包路径_java获取java文件路径的四种方法

    java获取java文件路径的四种方法 发布时间:2020-04-17 11:03:45 来源:亿速云 阅读:750 作者:小新 今天小编给大家分享的是java获取java文件路径的四种方法,很多人都 ...

  3. ubuntu18.04.4 获取当前文件路径

    ubuntu18.04.4 获取当前文件路径 打开文件夹 右键–>在终端打开 pwd 复制上面地址

  4. python获取某文件路径_Python获取当前文件路径

    一. Python 获取当前文件路径方法 2. sys.path[0] 获取文件当前工作目录路径(绝对路径) sys.argv[0]|获得模块所在的路径(由系统决定是否是全名) 若显示调用python ...

  5. python 当前文件路径获取方式_Python获取当前文件路径

    一. Python 获取当前文件路径方法 2. sys.path[0] 获取文件当前工作目录路径(绝对路径) sys.argv[0]|获得模块所在的路径(由系统决定是否是全名) 若显示调用python ...

  6. html5加js实现本地文件读取和写入并获取本地文件路径

    HTML5提供了一台API可以实现文件的读写,文件读取利用API是FileReader 代码如下: 读取本地文件 <!doctype html> <html lang="e ...

  7. python 如何获取文件路径_Python如何获取文件路径/目录

    一.获取文件路径实现 1.1 获取当前文件路径 import os current_file_path = __file__ print(f"current_file_path: {curr ...

  8. VS2019 WPF制作OTA上位机(二)获取bin文件路径

    OTA升级是通过无线通信远程把bin文件内容传输到单片机,完成升级. 因此上位机需要获取bin文件的路径,读取bin文件内容,将内容分割依次发送(因为单片机的接收缓存不会开得和bin文件一样大(十几K ...

  9. Qt 程序获取各种文件路径方法

    Qt 程序获取程序所在路径.用户目录路径.临时文件夹等特殊路径的方法 经常我们的程序中需要访问一些特殊的路径,比如程序所在的路径.用户目录路径.临时文件夹等.在 Qt 中实现这几个功能所用的方法虽然都 ...

最新文章

  1. 2017可信区块链峰会在京举办 可信区块链标准和测评结果公布
  2. pythonjoin函数所在包_Python中的join()函数
  3. fail-fast(快速失败/报错机制)-ConcurrentModificationException
  4. 【深度学习】一文概览神经网络模型
  5. win10 快捷键 - 采矿篇
  6. 字符串和字符数组里的字符串比较
  7. 苹果商店上架流程_苹果TF签名是什么?
  8. 测试工程方法:判定表驱动法
  9. 《计算机操作系统》(慕课版) 第1章 操作系统引论
  10. Allegro给一个网络赋默认值,取消默认值
  11. opencv 二值化图像详解 一文看懂各种二值化方法
  12. 基于51单片机的电子时钟
  13. 科普 | 到底什么是移动边缘计算?
  14. Unity3D游戏开发入门引导:Unity3D收费方案和版本、下载地址、安装教程
  15. 《遥感原理与应用》总结—遥感传感器及成像原理
  16. oracle怎么启动oem,Oracle 启动OEM
  17. 南师大计算机专硕士复试分数线,南京师范大学2020年硕士研究生复试分数线的通知...
  18. Python-脾气暴躁
  19. uniapp上传(拍照、本地),预览,删除图片
  20. 模式识别-高维空间降维的重要性

热门文章

  1. 学习java随堂练习-20220617
  2. core文件如何查看和调试
  3. 【python】wxpy--微信接口库
  4. 数学史上最重要的女性:埃米·诺特
  5. 定时任务的10种写法,长见识了
  6. 开发工具篇第三讲:Maven从入门到实战
  7. 【疑难杂症】matplotlib绘图是设置中文字体为宋体
  8. 基于C语言设计的俄罗斯方块小游戏(VS2017运行)
  9. mac 版VirtualBox 安装win10方法 全屏
  10. html中微博发布怎么做,js实现微博发布小功能