Font Metrics

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

Guest

I would like to be able to get Font Metrics (Ascent, Descent, Baseline,
character widths, etc.) in the .NET CF. How can I do this?

It seems pretty nasty to have to use PInvoke on "Coredll.lib" which I assume
is a statically linked library (meaning I need to create a DLL that I then
PInvoke on).

It also requires a device context with a currently set font that it will
make measurements on. How do I set the font on a graphics object so that I
can make measurements with it? Is there special clean-up involved with this?

Thank you,
Richard Arthur
 
You have to P/Invoke things. Something like this (not compiled, not tested)

IntPtr hDC = GetDC(IntPtr.Zero); //Screen DC
IntPtr hObjOld = SelectObject(hDC, font.GetHfont());
TEXTMETRICS tm = new TEXTMETRICS ();
GetTextMetrics(hDC, ref tm);
SelectObject(hDC, hObjOld);
ReleaseDC(IntPtr.Zero, hDC);

[DllImport("coredll")]
extern static IntPtr GetDC(IntPtr hWnd);

[DllImport("coredll")]
extern static IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);

[DllImport("coredll")]
extern static IntPtr SelectObject(IntPtr hDC, IntPtr hObj);

[DllImport("coredll")]
extern static int GetTextMetrics(IntPtr hDC, ref TEXTMETRICS tm);

[StructLayout(LayoutType.Sequential)]
public struct TEXTMETRICS
{
public int tmHeight;
public int tmAscent;
public int tmDescent;
public int tmInternalLeading;
public int tmExternalLeading;
public int tmAveCharWidth;
public int tmMaxCharWidth;
public int tmWeight;
public int tmOverhang;
public int tmDigitizedAspectX;
public int tmDigitizedAspectY;
public byte tmFirstChar;
public byte tmLastChar;
public byte tmDefaultChar;
public byte tmBreakChar;
public byte tmItalic;
public byte tmUnderlined;
public byte tmStruckOut;
public byte tmPitchAndFamily;
public byte tmCharSet;
}
 
That worked very well. Thank you. I just assumed that coredll was a
statically linked library because the documentation mentioned coredll.lib. I
adjusted a couple things to get this to definitely work, and I am posting it
below.

IntPtr hDC = GetDC(IntPtr.Zero); //Screen DC
IntPtr hFont = font.ToHfont();
IntPtr hObjOld = SelectObject(hDC, hFont );
TEXTMETRICS tm = new TEXTMETRICS();
GetTextMetrics(hDC, ref tm);
SelectObject(hDC, hObjOld);
DeleteObject(hFont);
ReleaseDC(IntPtr.Zero, hDC);


[DllImport("coredll")]
private extern static IntPtr GetDC(IntPtr hWnd);

[DllImport("coredll")]
private extern static IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);

[DllImport("coredll")]
private extern static IntPtr SelectObject(IntPtr hDC, IntPtr hObj);

[DllImport("coredll")]
private extern static IntPtr DeleteObject(IntPtr hObject);

[DllImport("coredll", CharSet = CharSet.Unicode)]
private extern static int GetTextMetrics(IntPtr hDC, ref TEXTMETRICS
tm);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct TEXTMETRICS
{
public int tmHeight;
public int tmAscent;
public int tmDescent;
public int tmInternalLeading;
public int tmExternalLeading;
public int tmAveCharWidth;
public int tmMaxCharWidth;
public int tmWeight;
public int tmOverhang;
public int tmDigitizedAspectX;
public int tmDigitizedAspectY;
public byte tmFirstChar;
public byte tmLastChar;
public byte tmDefaultChar;
public byte tmBreakChar;
public byte tmItalic;
public byte tmUnderlined;
public byte tmStruckOut;
public byte tmPitchAndFamily;
public byte tmCharSet;
}
 
Back
Top