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

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

我正在尝试使用 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
      }

但是当我这样做时,我得到以下 System.ArgumentException 异常,是不是我遗漏了什么?我的印象是 GetStockObject 在这种情况下会 return 一个合适的 HFont:

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

你可能忽略的:

According to MSDN: System font. 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; for more information, see the remarks section. The default system font is Tahoma.

An ancient document:

Among the things you can get with the GetStockObject function are two fonts called SYSTEM_FONT and 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. This is the font that SYSTEM_FONT retrieves, and it is still the default dialog box font for compatibility reasons. 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. 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. 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.

如果要使用DEFAULT_GUI_FONT,可以参考,通过SystemParametersInfo(SPI_GETNONCLIENTMETRICS)获取NONCLIENTMETRICS,然后使用LOGFONT数据,用于创建自字体。或者您可以查询 SystemParametersInfo(SPI_GETICONTITLELOGFONT) 并使用它。

希望能帮到你:)