Windows Mobile アプリケーションの ListView から継承されたオーナー描画コントロールを作成しています。Graphics.DrawString
2 行のテキスト文字列を書き出すために使用しています (.NET CF 3.5 を使用)。問題は、一部の項目に 2 行に収まらない特に長いテキストがあることです。グーグルは、文字列を使用MeasureString
して手動で切り捨てる方法を見つけましたが、これは単一行の文字列でのみ機能します。ここで省略記号を取得する方法はありますか、またはクリップされたテキストを受け入れるか、1 行だけを使用するように再設計する必要がありますか? (どちらも契約を破るものではありませんが、省略記号は確かに優れています。)
1343 次
1 に答える
2
はい、省略記号を表示することはできますが、P/Invoking を実行する必要があります (新機能は何ですか?)。
public static void DrawText(Graphics gfx, string text, Font font, Color color, int x, int y, int width, int height)
{
IntPtr hdcTemp = IntPtr.Zero;
IntPtr oldFont = IntPtr.Zero;
IntPtr currentFont = IntPtr.Zero;
try
{
hdcTemp = gfx.GetHdc();
if (hdcTemp != IntPtr.Zero)
{
currentFont = font.ToHfont();
oldFont = NativeMethods.SelectObject(hdcTemp, currentFont);
NativeMethods.RECT rect = new NativeMethods.RECT();
rect.left = x;
rect.top = y;
rect.right = x + width;
rect.bottom = y + height;
int colorRef = color.R | (color.G << 8) | (color.B << 16);
NativeMethods.SetTextColor(hdcTemp, colorRef);
NativeMethods.DrawText(hdcTemp, text, text.Length, ref rect, NativeMethods.DT_END_ELLIPSIS | NativeMethods.DT_NOPREFIX);
}
}
finally
{
if (oldFont != IntPtr.Zero)
{
NativeMethods.SelectObject(hdcTemp, oldFont);
}
if (hdcTemp != IntPtr.Zero)
{
gfx.ReleaseHdc(hdcTemp);
}
if (currentFont != IntPtr.Zero)
{
NativeMethods.DeleteObject(currentFont);
}
}
}
NativeMethods は、すべてのネイティブ コールを含むクラスです。含む:
internal const int DT_END_ELLIPSIS = 32768;
internal const int DT_NOPREFIX = 2048;
[DllImport("coredll.dll", SetLastError = true)]
internal static extern int DrawText(IntPtr hDC, string Text, int nLen, ref RECT pRect, uint uFormat);
[DllImport("coredll.dll", SetLastError = true)]
internal static extern int SetTextColor(IntPtr hdc, int crColor);
[DllImport("coredll.dll", SetLastError = true)]
internal static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
[DllImport("coredll.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeleteObject(IntPtr hObject);
[StructLayout(LayoutKind.Sequential)]
internal struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
于 2010-09-13T04:57:45.560 に答える