C++获取系统字体MFC

调用函数

#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <afx.h>
#include <fstream>
using namespace std;
vector<CString> g_vSysFonts;

INT CALLBACK NEnumFontNameProc(LOGFONT* plf, TEXTMETRIC* /*ptm*/, INT /*nFontType*/, LPARAM lParam/**/)
{
    //同步调用的回调
    vector<CString>* sysFonts = (vector<CString>*)lParam;
    CString strVal(L"");
    if (sysFonts != NULL)
    {
        strVal = plf->lfFaceName;
        if (strVal.Left(1) != L"@")
        {
            sysFonts->push_back(strVal);
        }
    }
    return TRUE; //EnumFontFamilies 返回值由此回调返回决定
}

void GetSystemFont(HWND& hwnd)
{
    vector<CString> vFont;
    HDC hdc = ::GetDC(hwnd);
    //    int nRet = ::EnumFontFamilies(hdc, (LPTSTR)NULL, (FONTENUMPROC)NEnumFontNameProc, (LPARAM) & (g_vSysFonts));
    int nRet = ::EnumFonts(hdc, (LPTSTR)NULL, (FONTENUMPROC)NEnumFontNameProc, (LPARAM) & (g_vSysFonts));
    ::ReleaseDC(hwnd, hdc);
}

 

 

 

调用方式

std::ofstream out("D://output1.txt", std::ios::app);
HWND m_hWnd = GetActiveWindow();
int i = 0;
GetSystemFont(m_hWnd);
for (vector<CString>::iterator it = g_vSysFonts.begin(); it != g_vSysFonts.end(); it++)
{
    i++;
    CString css = *it;
    CT2CA pszName(css);
    std::string m_NameStd(pszName);
    out << i << ": " << m_NameStd << endl;
}
out.close();

 

新建个MFC项目   基于对话框的 扔进去就ok

结果如下

 

posted @ 2021-10-15 09:39  冰糖葫芦很乖  阅读(568)  评论(1编辑  收藏  举报