Fixed-width font issues

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dlgproc

    Fixed-width font issues

    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?

  • Doug Forster

    #2
    Re: Fixed-width font issues

    Unless you really like doing it the hard way try:
    FontFamily.Gene ricMonoSpace

    Cheers

    Doug Forster

    "dlgproc" <dlgproc@discus sions.microsoft .com> wrote in message
    news:CC425EFF-6F5A-433B-85E4-BEF8D694166F@mi crosoft.com...[color=blue]
    >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?
    >[/color]


    Comment

    • dlgproc

      #3
      Re: Fixed-width font issues

      Thanks

      "Doug Forster" wrote:
      [color=blue]
      > Unless you really like doing it the hard way try:
      > FontFamily.Gene ricMonoSpace
      >
      > Cheers
      >
      > Doug Forster
      >
      > "dlgproc" <dlgproc@discus sions.microsoft .com> wrote in message
      > news:CC425EFF-6F5A-433B-85E4-BEF8D694166F@mi crosoft.com...[color=green]
      > >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?
      > >[/color]
      >
      >
      >[/color]

      Comment

      Working...