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:
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? I was under the impression that GetStockObject in this case would return a proper HFont:
Only TrueType fonts are supported. This is not a TrueType font.
What you may ignore:
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.
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.
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. or you can query for SystemParametersInfo(SPI_GETICONTITLELOGFONT)
and use it.
Hope to help you:)