Windows mobile 紧凑框架:使用椭圆绘制多行字符串
我正在为Windows Mobile应用程序创建从ListView继承的所有者绘制控件。我正在使用Windows mobile 紧凑框架:使用椭圆绘制多行字符串,windows-mobile,compact-framework,drawstring,Windows Mobile,Compact Framework,Drawstring,我正在为Windows Mobile应用程序创建从ListView继承的所有者绘制控件。我正在使用Graphics.DrawString写出一个两行文本字符串(使用.NET CF 3.5)。问题是有些项目有特别长的文本,不适合两行。谷歌已经找到了使用MeasureString和手动截断字符串的方法,但这只适用于单行字符串。有没有办法在这里得到省略号,或者我必须接受剪裁文本或重新设计以仅使用一行?(两者都不是交易破坏者,但省略号肯定很好。)是的,您可以让省略号显示出来,但您必须进行一些p/调用(有
Graphics.DrawString
写出一个两行文本字符串(使用.NET CF 3.5)。问题是有些项目有特别长的文本,不适合两行。谷歌已经找到了使用MeasureString
和手动截断字符串的方法,但这只适用于单行字符串。有没有办法在这里得到省略号,或者我必须接受剪裁文本或重新设计以仅使用一行?(两者都不是交易破坏者,但省略号肯定很好。)是的,您可以让省略号显示出来,但您必须进行一些p/调用(有什么新功能?):
我担心P/Invoke会牵涉其中。但答案很好!
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);
}
}
}
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;
}