"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
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?
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)]