问题
According to my understanding of the documentation, when using TextFormatFlags.EndEllipsis the text should be trimmed to fit inside the display rectangle and replaced with an ellipsis:
EndEllipsis | Removes the end of trimmed lines, and replaces them with an ellipsis.
When using TextFormatFlags.WordEllipsis, the text should be trimmed to the last word that fits inside the display rectangle, and add an ellipsis:
WordEllipsis | Trims the line to the nearest word and an ellipsis is placed at the end of a trimmed line.
However, I can't seem to find any difference between these two.
Here is my test control's code:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
this.Text = "This it my text. It's long enough to get cut in display.";
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var flags = (this.IsWordEllipsis) ? TextFormatFlags.WordEllipsis : TextFormatFlags.EndEllipsis;
TextRenderer.DrawText(e.Graphics, this.Text, this.Font, e.ClipRectangle, this.ForeColor, flags);
}
private bool _IsWordEllipsis;
public bool IsWordEllipsis
{
get { return _IsWordEllipsis; }
set { _IsWordEllipsis = value; this.Invalidate(); }
}
}
No matter what value I set the IsWordEllipsis property - the text is always rendered asThis it my text. It's long eno....
Shouldn't using the WordEllipsis flag trim the displayed string to the last complete word?
(This it my text. It's long ...)
回答1:
Internally TextRenderer.DrawText calls DrawText function
Corresponding flags are:
DT_WORD_ELLIPSIS: Truncates any word that does not fit in the rectangle and adds ellipses.
DT_END_ELLIPSIS For displayed text, if the end of a string does not fit in the rectangle, it is truncated and ellipses are added. If a word that is not at the end of the string goes beyond the limits of the rectangle, it is truncated without ellipses.
I have interpreted .NET documentation for WordEllipsis same way as you, but above sentence probably better explains what this option is supposed to do.
In case it is still unclear take a look at this simple example, which tries to render var quote = "Not every thing that\n can be counted counts,\n and not everything that\n counts can be counted.";
来源:https://stackoverflow.com/questions/36615862/whats-the-difference-between-endellipsis-and-wordellipsis-in-textformatflags-en