Click here to Skip to main content
15,913,836 members
Home / Discussions / C#
   

C#

 
AnswerRe: Fetching Font information ? Pin
Heath Stewart22-Jul-04 4:41
protectorHeath Stewart22-Jul-04 4:41 
GeneralRe: Fetching Font information ? Pin
sachinkalse22-Jul-04 14:50
sachinkalse22-Jul-04 14:50 
GeneralRe: Fetching Font information ? Pin
Heath Stewart23-Jul-04 5:27
protectorHeath Stewart23-Jul-04 5:27 
D'Oh! | :doh: Sorry 'bout that. Wasn't really thinking much about it.

If that's not working the way you expect it, I'd seriously take another look at the code surrounding EnumFontFamilies(Ex). Be sure to read the docs in the Platform SDK if you haven't already.

Here's an example I threw together quick that works fine:
using System;
using System.Runtime.InteropServices;

class EnumFonts
{
  static void Main(string[] args)
  {
    string family = null;
    if (args.Length > 0) family = args[0];

    // Get the HDC for the desktop.
    IntPtr hdc = GetDC(IntPtr.Zero);

    try
    {
      EnumFontFamilies(hdc, family,
        new EnumFontFamProc(Callback), IntPtr.Zero);
    }
    catch (Exception ex)
    {
      Console.Error.WriteLine("There was an error enumerating the " +
        "font families: " + ex.Message);
    }
    finally
    {
      ReleaseDC(IntPtr.Zero, hdc);
    }
  }

  static IntPtr Callback(ref ENUMLOGFONT font, ref NEWTEXTMETRIC metric,
    int fontType, IntPtr lParam)
  {
    Console.WriteLine(font.FullName);
    return new IntPtr(1); // Continue enumeration.
  }

  [DllImport("user32.dll")]
  static extern IntPtr GetDC(IntPtr hWnd);

  [DllImport("user32.dll")]
  static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);

  [DllImport("gdi32.dll", CharSet=CharSet.Auto)]
  static extern IntPtr EnumFontFamilies(
    IntPtr hdc,
    string family,
    EnumFontFamProc proc,
    IntPtr lParam);

  delegate IntPtr EnumFontFamProc(
    ref ENUMLOGFONT font,
    ref NEWTEXTMETRIC metric,
    [MarshalAs(UnmanagedType.U4)] int fontType,
    IntPtr lParam);

  [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
  struct LOGFONT
  {
    public int Height;
    public int Width;
    public int Escapement;
    public int Orientation;
    public int Weight;
    public byte Italic;
    public byte Underline;
    public byte StrikeOut;
    public byte CharSet;
    public byte OutPrecision;
    public byte ClipPrecision;
    public byte Quality;
    public byte PitchAndFamily;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
      public string FaceName;
  }

  [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
  struct ENUMLOGFONT
  {
    public LOGFONT LogFont;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
      public string FullName;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
      public string Style;
  }

  [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
  struct NEWTEXTMETRIC // Also TEXTMETRIC for non-TrueType fonts
  {
    public int Height;
    public int Ascent;
    public int Descent;
    public int InternalLeading;
    public int ExternalLeading;
    public int AveCharWidth;
    public int MaxCharWidth;
    public int Weight;
    public int Overhang;
    public int DigitizedAspectX;
    public int DigitizedAspectY;
    public char FirstChar;
    public char Lastchar;
    public char DefaultChar;
    public char BreakChar;
    public byte Italic;
    public byte Underlined;
    public byte StruckOut;
    public byte PitchAndFamily;
    public byte CharSet; // TEXTMETRIC ends here
    [MarshalAs(UnmanagedType.U4)] public int Flags;
    [MarshalAs(UnmanagedType.SysUInt)] public IntPtr SizeEM;
    [MarshalAs(UnmanagedType.SysUInt)] public IntPtr CellHeight;
    [MarshalAs(UnmanagedType.SysUInt)] public IntPtr AvgWidth;
  }
}


 

Microsoft MVP, Visual C#
My Articles
GeneralRe: Fetching Font information ? Pin
sachinkalse25-Jul-04 16:41
sachinkalse25-Jul-04 16:41 
GeneralRe: Fetching Font information ? Pin
Heath Stewart26-Jul-04 4:38
protectorHeath Stewart26-Jul-04 4:38 
GeneralRe: Fetching Font information ? Pin
sachinkalse26-Jul-04 15:27
sachinkalse26-Jul-04 15:27 
GeneralRe: Fetching Font information ? Pin
Heath Stewart26-Jul-04 17:56
protectorHeath Stewart26-Jul-04 17:56 
GeneralRe: Fetching Font information ? Pin
sachinkalse26-Jul-04 18:26
sachinkalse26-Jul-04 18:26 
GeneralRe: Fetching Font information ? Pin
Heath Stewart26-Jul-04 18:37
protectorHeath Stewart26-Jul-04 18:37 
Generalevaluating simple strings as boolean Pin
MonkeyBob21-Jul-04 20:09
MonkeyBob21-Jul-04 20:09 
GeneralRe: evaluating simple strings as boolean Pin
Angel Reyes21-Jul-04 20:20
Angel Reyes21-Jul-04 20:20 
GeneralRe: evaluating simple strings as boolean Pin
MonkeyBob21-Jul-04 20:57
MonkeyBob21-Jul-04 20:57 
GeneralRe: evaluating simple strings as boolean Pin
mav.northwind21-Jul-04 22:38
mav.northwind21-Jul-04 22:38 
GeneralRe: evaluating simple strings as boolean Pin
Heath Stewart22-Jul-04 4:20
protectorHeath Stewart22-Jul-04 4:20 
GeneralRe: evaluating simple strings as boolean Pin
Nick Parker22-Jul-04 4:13
protectorNick Parker22-Jul-04 4:13 
GeneralCut Image Pin
jzb21-Jul-04 18:50
jzb21-Jul-04 18:50 
GeneralRe: Cut Image Pin
Heath Stewart22-Jul-04 4:10
protectorHeath Stewart22-Jul-04 4:10 
GeneralUser Controls's question Pin
jzb21-Jul-04 18:48
jzb21-Jul-04 18:48 
GeneralRe: User Controls's question Pin
Heath Stewart22-Jul-04 4:03
protectorHeath Stewart22-Jul-04 4:03 
Generalusing different interfaces still works! Pin
ting66821-Jul-04 18:36
ting66821-Jul-04 18:36 
GeneralRe: using different interfaces still works! Pin
Colin Angus Mackay21-Jul-04 23:52
Colin Angus Mackay21-Jul-04 23:52 
GeneralOne major tcp/ip headache Pin
TalkingBabb0t21-Jul-04 17:10
TalkingBabb0t21-Jul-04 17:10 
GeneralRe: One major tcp/ip headache Pin
Heath Stewart22-Jul-04 3:56
protectorHeath Stewart22-Jul-04 3:56 
GeneralRe: One major tcp/ip headache Pin
TalkingBabb0t22-Jul-04 16:19
TalkingBabb0t22-Jul-04 16:19 
GeneralAbout C# Rs232 communication Pin
resocman21-Jul-04 17:09
resocman21-Jul-04 17:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.