Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Font Metrics

4 views
Skip to first unread message

Alex Feinman [MVP]

unread,
Mar 2, 2006, 11:54:53 AM3/2/06
to
CF1 or CF2?

"StarTether" <StarT...@discussions.microsoft.com> wrote in message
news:FE9382F5-D6B6-4F33...@microsoft.com...
>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

Alex Feinman [MVP]

unread,
Mar 2, 2006, 8:09:33 PM3/2/06
to
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;
}

"StarTether" <StarT...@discussions.microsoft.com> wrote in message

news:74A9BD9E-B11A-4790...@microsoft.com...
> CF2
>
> -Richard
>
> "Alex Feinman [MVP]" wrote:
>
>> CF1 or CF2?

StarTether

unread,
Mar 6, 2006, 12:34:39 PM3/6/06
to
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)]

0 new messages