DimitryG 的解决方案似乎效果很好,但只有当没有足够大的单词来填充超过一整行时。如果存在这样的词,则宽度将大于建议的宽度。这种情况下有标志TextFormatFlags.EndEllipsis
,但是我没有设法以某种方式组合标志,因此输出是正确的(如果我使用TextFormatFlags.WordEllipsis | TextFormatFlags.WordBreak
的宽度是正确的,但是当 Word Ellipsis 发生时高度不会更新,这意味着大字会被修剪,但高度会和不修剪一样)。我也尝试了标志TextFormatFlags.EndEllipsis
,但没有结果。
因此,在有人说清楚之前,我建议使用TextBox
for 自动换行,然后将 中的行数乘以 的TextBox
高度Font
。
代码:
int MeasureMultilineTextHeigh(string text, Font font, int proposedWidth)
{
// Exception handling.
TextBox textBox = new TextBox()
{
Multiline = true,
BorderStyle = BorderStyle.None,
Width = proposedWidth,
Font = font,
Text = text,
};
int lineCount = textBox.GetLineFromCharIndex(int.MaxValue) + 1;
int fontHeight = TextRenderer.MeasureText("X", font).Height;
return lineCount * fontHeight;
}
然而,这种方法有一个问题:当且仅当Multiline
a 的属性TextBox
设置为 时true
,每个Font
都有自己的左右填充。有关更多详细信息,请参阅此 stackoverflow.com 问题和此 social.msdn.microsoft.com 问题。所以这意味着在某些情况下,返回的值可能比预期的要大。要解决此问题,您可以使用该SetPadding
函数删除填充(您可以在第一个问题中找到该方法作为答案),代码:
private const int EM_SETRECT = 0xB3;
[DllImport(@"User32.dll", EntryPoint = @"SendMessage", CharSet = CharSet.Auto)]
private static extern int SendMessageRefRect(IntPtr hWnd, uint msg, int wParam, ref RECT rect);
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public readonly int Left;
public readonly int Top;
public readonly int Right;
public readonly int Bottom;
private RECT(int left, int top, int right, int bottom)
{
Left = left;
Top = top;
Right = right;
Bottom = bottom;
}
public RECT(Rectangle r) : this(r.Left, r.Top, r.Right, r.Bottom) { }
}
public void SetPadding(TextBox textBox, Padding padding)
{
var rect = new Rectangle(padding.Left, padding.Top, textBox.ClientSize.Width - padding.Left - padding.Right, textBox.ClientSize.Height - padding.Top - padding.Bottom);
RECT rc = new RECT(rect);
SendMessageRefRect(textBox.Handle, EM_SETRECT, 0, ref rc);
}
int MeasureMultilineTextHeigh(string text, Font font, int proposedWidth)
{
// Exception handling.
TextBox textBox = new TextBox()
{
Multiline = true,
BorderStyle = BorderStyle.None,
Width = proposedWidth,
Font = font,
};
SetPadding(textBox, Padding.Empty);
textBox.Text = text;
int lineCount = textBox.GetLineFromCharIndex(int.MaxValue) + 1;
int fontHeight = TextRenderer.MeasureText("X", font).Height;
return lineCount * fontHeight;
}
需要使用语句:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
我希望这有帮助。对不起我的英语,如果我犯了任何错误。