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;
                    }                 
                }
            }
        }
    }

 

 

posted @ 2024-08-21 20:10  enych  阅读(72)  评论(0编辑  收藏  举报