Compact Framework:用省略号绘制多行字符串

发布于 2024-09-18 19:56:58 字数 242 浏览 1 评论 0原文

我正在为 Windows Mobile 应用程序创建一个继承自 ListView 的所有者绘制控件。我正在使用 Graphics.DrawString 写出两行文本字符串(使用 .NET CF 3.5)。问题是有些项目的文本特别长,不适合两行。谷歌搜索找到了使用 MeasureString 并手动截断字符串的方法,但这仅适用于单行字符串。有什么方法可以在这里得到省略号,还是我必须接受剪切文本或重新设计以仅使用一行? (两者都不是破坏交易的因素,但省略号肯定会很好。)

I'm creating an owner-drawn control inherited from ListView for a Windows Mobile application. I'm using Graphics.DrawString to write out a two-line text string (using .NET CF 3.5). Problem is some items have particularly long text that doesn't fit on the two lines. Googling has found methods for using MeasureString and to manually truncate my string, but this only works on a single-line string. Is there any way to get the ellipses here, or do I have to either accept clipped text or redesign to use only a single line? (Neither is a deal-breaker, but ellipses sure would be nice.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春花秋月 2024-09-25 19:56:58

是的,您可以显示省略号,但您必须执行一些 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;

}

Yes, you can get the ellipses to display, but you'll have to do some P/Invoking (what's new?):

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 is a class that has all of my native calls. Including:

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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文