Fixed-width font issues

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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_FONT).

Using the following C# code to do the same throws an exception:
[System.Runtime.InteropServices.DllImportAttribute("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_FONT 16
// Create a Font object from hFont.
return Font.FromHfont(hFont);
}

Unhandled Exception: System.ArgumentException: Only TrueType fonts are
supported, and this is not a TrueType font.

Is there a way I can do what I want to do?
 
Unless you really like doing it the hard way try:
FontFamily.GenericMonoSpace

Cheers

Doug Forster
 
Thanks

Doug Forster said:
Unless you really like doing it the hard way try:
FontFamily.GenericMonoSpace

Cheers

Doug Forster

dlgproc said:
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_FONT).

Using the following C# code to do the same throws an exception:
[System.Runtime.InteropServices.DllImportAttribute("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_FONT 16
// Create a Font object from hFont.
return Font.FromHfont(hFont);
}

Unhandled Exception: System.ArgumentException: Only TrueType fonts are
supported, and this is not a TrueType font.

Is there a way I can do what I want to do?
 
Back
Top