简体   繁体   English

使用 System.Drawing.Font.FromHfont(IntPtr hfont) 从 SYSTEM_FONT Stock 对象创建 System.Drawing.Font

[英]System.Drawing.Font Creation From SYSTEM_FONT Stock Object using System.Drawing.Font.FromHfont(IntPtr hfont)

I'm trying to create an instance of System.Drawing.Font using the SYSTEM_FONT stock object using System.Drawing.Font.FromHfont(IntPtr hfont) , like so:我正在尝试使用System.Drawing.Font.FromHfont(IntPtr hfont)使用SYSTEM_FONT库存对象创建 System.Drawing.Font 的实例,如下所示:

      static Font GetStockFont(StockObjects index)
      {
         Font returnFont = Font.FromHfont(GetStockObject(index));
         return returnFont;
      }

      [DllImport("gdi32.dll")]
      static extern IntPtr GetStockObject(StockObjects fnObject);

      enum StockObjects 
      {
         WHITE_BRUSH = 0,
         LTGRAY_BRUSH = 1,
         GRAY_BRUSH = 2,
         DKGRAY_BRUSH = 3,
         BLACK_BRUSH = 4,
         NULL_BRUSH = 5,
         HOLLOW_BRUSH = NULL_BRUSH,
         WHITE_PEN = 6,
         BLACK_PEN = 7,
         NULL_PEN = 8,
         OEM_FIXED_FONT = 10,
         ANSI_FIXED_FONT = 11,
         ANSI_VAR_FONT = 12,
         SYSTEM_FONT = 13,
         DEVICE_DEFAULT_FONT = 14,
         DEFAULT_PALETTE = 15,
         SYSTEM_FIXED_FONT = 16,
         DEFAULT_GUI_FONT = 17,
         DC_BRUSH = 18,
         DC_PEN = 19
      }

But when I do I'm getting the following System.ArgumentException exception, is there something I'm missing?但是当我这样做时,我得到以下System.ArgumentException异常,是不是我遗漏了什么? I was under the impression that GetStockObject in this case would return a proper HFont:我的印象是,在这种情况下,GetStockObject 会返回正确的 HFont:

Only TrueType fonts are supported. This is not a TrueType font.

What you may ignore:你可能会忽略的:

According to MSDN : System font.根据MSDN :系统字体。 By default, the system uses the system font to draw menus, dialog box controls, and text.默认情况下,系统使用系统字体来绘制菜单、对话框控件和文本。 It is not recommended that you use DEFAULT_GUI_FONT or SYSTEM_FONT to obtain the font used by dialogs and windows;不建议您使用 DEFAULT_GUI_FONT 或 SYSTEM_FONT 来获取对话框和窗口使用的字体; for more information, see the remarks section.有关详细信息,请参阅备注部分。 The default system font is Tahoma.默认系统字体是 Tahoma。

An ancient document:一个古老的文件:

Among the things you can get with the GetStockObject function are two fonts called SYSTEM_FONT and DEFAULT_GUI_FONT.使用 GetStockObject 函数可以获得的内容包括两种字体,分别称为 SYSTEM_FONT 和 DEFAULT_GUI_FONT。 What are they?这些是什么?

They are fonts nobody uses any more.它们是没人再使用的字体。

Back in the old days of Windows 2.0, the font used for dialog boxes was a bitmap font called System.在过去的 Windows 2.0 时代,用于对话框的字体是一种名为 System 的位图字体。 This is the font that SYSTEM_FONT retrieves, and it is still the default dialog box font for compatibility reasons.这是 SYSTEM_FONT 检索的字体,出于兼容性原因,它仍然是默认的对话框字体。 Of course, nobody nowadays would ever use such an ugly font for their dialog boxes.当然,现在没有人会在他们的对话框中使用如此难看的字体。 (Among other things, it's a bitmap font and therefore does not look good at high resolutions, nor can it be anti-aliased.) (除其他外,它是一种位图字体,因此在高分辨率下看起来不太好,也不能消除锯齿。)

DEFAULT_GUI_FONT has an even less illustrious history. DEFAULT_GUI_FONT 的历史更不光彩。 It was created during Windows 95 development in the hopes of becoming the new default GUI font, but by July 1994, Windows itself stopped using it in favor of the various fonts returned by the SystemParametersInfo function.它是在 Windows 95 开发期间创建的,希望成为新的默认 GUI 字体,但到 1994 年 7 月,Windows 本身已停止使用它,转而使用 SystemParametersInfo 函数返回的各种字体。 Its existence is now vestigial.它的存在现在是退化的。

One major gotcha with SYSTEM_FONT and DEFAULT_GUI_FONT is that on a typical US-English machine, they map to bitmap fonts that do not support ClearType. SYSTEM_FONT 和 DEFAULT_GUI_FONT 的一个主要问题是,在典型的美式英语机器上,它们映射到不支持 ClearType 的位图字体。

If you want to use DEFAULT_GUI_FONT , you can refer @RbMm's answer , get NONCLIENTMETRICS by SystemParametersInfo(SPI_GETNONCLIENTMETRICS) and then use it LOGFONT data, for create self font.如果你想使用DEFAULT_GUI_FONT ,你可以参考@RbMm 的答案,通过SystemParametersInfo(SPI_GETNONCLIENTMETRICS)获取NONCLIENTMETRICS然后使用它LOGFONT数据,用于创建自己的字体。 or you can query for SystemParametersInfo(SPI_GETICONTITLELOGFONT) and use it.或者您可以查询SystemParametersInfo(SPI_GETICONTITLELOGFONT)并使用它。

Hope to help you:)希望能帮到你:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM