Google グループは Usenet の新規の投稿と購読のサポートを終了しました。過去のコンテンツは引き続き閲覧できます。
Dismiss

Re: Font Metrics

閲覧: 4 回
最初の未読メッセージにスキップ

Alex Feinman [MVP]

未読、
2006/03/02 11:54:532006/03/02
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]

未読、
2006/03/02 20:09:332006/03/02
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

未読、
2006/03/06 12:34:392006/03/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 件