Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a combination of TextFormatFlags to allow text wrapping and an ending ellipsis?

I have been unable to put together a bit of string rendering code that gives me everything I am aiming for. To this point, only trade-offs.

I am shooting for something that behaves the way Windows7 desktop shortcuts do, in that there is an image above text and that text can wrap once, with anything more truncated with an ellipse.

Such as:

enter image description here

I am using TextRenderer.DrawText and I have tried various combinations of TextFormatFlags, but I can only get either text that wraps indefinitely, or text that stops at a single line and has the ellipsis.

Some examples. The bounds provided to TextRenderer are, in all cases, within the bounds of the ClipRectangle of the surface in which I am drawing. The text it is attempting to render is "This is the rather long name of a group called #1.".

This code:

TextFormatFlags flags = TextFormatFlags.HorizontalCenter | 
     TextFormatFlags.NoPadding | TextFormatFlags.WordBreak | 
     TextFormatFlags.EndEllipsis;
Rectangle rect = new Rectangle(x, y, this.Width - 6, this.Height - y);
TextRenderer.DrawText(e.Graphics, _text, Font, rect, _textColor, flags);

...produces this:

enter image description here

While simply removing the TextFormatFlags.WordBreak flag produces:

enter image description here

How can I do this such as I get wrapping as far as is possible within the bounds of the Rectangle I pass, followed by an ending ellipse when truncation occurs?

like image 647
DonBoitnott Avatar asked Feb 04 '14 22:02

DonBoitnott


1 Answers

You have to use a, cough, unintuitively named option to get both word breaks and the end-ellipsis. TextFormatFlags.TextBoxControl gives you what you are looking for.

This has some additional formatting behavior, none that you are likely to be upset about if you like the way the icon title looks, it uses the same winapi function. Key thing it does is not display a clipped line, like a multi-line TextBox doesn't, also giving you the ellipsis back.

like image 192
Hans Passant Avatar answered Oct 01 '22 12:10

Hans Passant