C# 获取系统字体,获取系统默认字体
参考 : https://www.cnblogs.com/kuangzhenxia-416/p/11728928.html
//获取所有字体集合 System.Drawing.Text.InstalledFontCollection MyFont = new System.Drawing.Text.InstalledFontCollection(); System.Drawing.FontFamily[] list = MyFont.Families;
#include <windows.h>
#include <uxtheme.h> #include <vssym32.h> #include <stdio.h> #pragma comment(lib, "uxtheme") int main() { LOGFONTW lf = { 0 }; HTHEME hTheme = OpenThemeData(0, VSCLASS_TEXTSTYLE); if (hTheme) { if (SUCCEEDED(GetThemeFont(hTheme, 0, TEXT_BODYTEXT, 0, TMT_FONT, &lf))) { wprintf(L"System default font name is \"%ls\"\n", lf.lfFaceName);;
} CloseThemeData(hTheme); } return 0; }
//// lf.lfFaceName 0x006ffcd0 L"Microsoft YaHei UI" 正确;
转自: https://www.fournoas.com/posts/find-windows-system-default-font-name-and-font-file/
static
void
Main(
string
[] args)
{
// C#获取系统默认字体 System.Drawing.SystemFonts.DefaultFont.Name
System.Drawing.Font font =
new
System.Drawing.Font(System.Drawing.SystemFonts.DefaultFont.Name, System.Drawing.SystemFonts.DefaultFont.Size);
// 显示的是 {Name = "宋体" Size=9} ///能获取到; 但是获取的不正确
}
HandleRef hWndRef = new HandleRef(null, IntPtr.Zero); int TEXT_BODYTEXT = 4; int TMT_FONT = 210; string VSCLASS_TEXTSTYLE = "TEXTSTYLE"; IntPtr hTheme = UxthemeHelper.OpenThemeData(hWndRef, VSCLASS_TEXTSTYLE); UxthemeHelper.LOGFONTW logfont = new UxthemeHelper.LOGFONTW(); UxthemeHelper.GetThemeFont(new HandleRef(null, hTheme), hWndRef, TEXT_BODYTEXT, 0,TMT_FONT, ref logfont); var d = logfont.lfFaceName;
//d = "Microsoft YaHei UI\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
partial class UxthemeHelper { [DllImport(ExternDll.Uxtheme, CharSet = CharSet.Auto)] public static extern IntPtr OpenThemeData(HandleRef hwnd, [MarshalAs(UnmanagedType.LPWStr)] string pszClassList); [DllImport(ExternDll.Uxtheme, CharSet = CharSet.Auto)] public static extern int CloseThemeData(HandleRef hTheme); [DllImport(ExternDll.Uxtheme, CharSet = CharSet.Auto)] public static extern int GetThemeFont(HandleRef hTheme, HandleRef hdc, int iPartId, int iStateId, int iPropId, ref LOGFONTW pFont); }
partial class UxthemeHelper { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public unsafe struct LOGFONTW { private const int LF_FACESIZE = 32; public int lfHeight; public int lfWidth; public int lfEscapement; public int lfOrientation; public int lfWeight; public byte lfItalic; public byte lfUnderline; public byte lfStrikeOut; public byte lfCharSet; public byte lfOutPrecision; public byte lfClipPrecision; public byte lfQuality; public byte lfPitchAndFamily; private fixed char _lfFaceName[LF_FACESIZE]; public string lfFaceName { get { fixed (char* c = _lfFaceName) { string result = Marshal.PtrToStringAuto((IntPtr)c, LF_FACESIZE); return result; } } } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决