I wanted to display text in a label on a Windows form in a fixed-width font.
In C++ Windows API programming I’ve always used:
GetStockObject( SYSTEM_FIXED_FO NT).
Using the following C# code to do the same throws an exception:
[System.Runtime. InteropServices .DllImportAttri bute("gdi32.dll ")]
private static extern IntPtr GetStockObject( int fnObject);
private static Font GetSystemFont()
{
// Get a handle for a GDI font.
IntPtr hFont = GetStockObject( 16); // #define SYSTEM_FIXED_FO NT 16
// Create a Font object from hFont.
return Font.FromHfont( hFont);
}
Unhandled Exception: System.Argument Exception: Only TrueType fonts are
supported, and this is not a TrueType font.
Is there a way I can do what I want to do?
In C++ Windows API programming I’ve always used:
GetStockObject( SYSTEM_FIXED_FO NT).
Using the following C# code to do the same throws an exception:
[System.Runtime. InteropServices .DllImportAttri bute("gdi32.dll ")]
private static extern IntPtr GetStockObject( int fnObject);
private static Font GetSystemFont()
{
// Get a handle for a GDI font.
IntPtr hFont = GetStockObject( 16); // #define SYSTEM_FIXED_FO NT 16
// Create a Font object from hFont.
return Font.FromHfont( hFont);
}
Unhandled Exception: System.Argument Exception: Only TrueType fonts are
supported, and this is not a TrueType font.
Is there a way I can do what I want to do?
Comment