0

我正在尝试使用 TextRenderer (.NET v4.0) 在自定义控件上显示一段文本。如果文本对于我允许的最大空间而言太大,我希望将文本截断并将省略号放在末尾 - 但我无法找出 TextFormatFlags 的正确组合来做到这一点。到目前为止的测试代码摘录;

Using vPlotFont As New Font("Verdana", 7, FontStyle.Regular, GraphicsUnit.Point)
  Dim vWidth = Me.Width - vTextLeft - Me.Margin.Right
  Dim vFlags = TextFormatFlags.Left Or TextFormatFlags.EndEllipsis Or TextFormatFlags.WordBreak
  Dim vRequiredHeight = TextRenderer.MeasureText(mvSummaryData, vPlotFont, New Size(vWidth, 1), vFlags).Height
  Dim vRectangle As New Rectangle(vTextLeft, 25, vWidth, Math.Min(vRequiredHeight, 40))  ' Restrict to 40 pixels maximum
  TextRenderer.DrawText(e.Graphics, mvSummaryData, vPlotFont, vRectangle, Color.Red, vFlags)
  e.Graphics.DrawRectangle(Pens.Blue, vRectangle)  ' For debugging/testing only
End Using

因为高度限制在 40,所以任何不适合的东西都会被截断 - 我如何告诉它在溢出时应用修剪/省略号?!

4

0 回答 0