TextRenderer. How to render text multiline with endellipsis?
Asked Answered
M

1

3

How i can render text like this(simple listview)?

default ListView render

Trying the code like this renders no ellipsis:

TextRenderer.DrawText(_listGraphics,
                   anItem.Text, GetItemFont(anItem),
                   textRec,
                   Color.FromKnownColor(KnownColor.ControlText),
                   TextFormatFlags.Top| TextFormatFlags.EndEllipsis|
                   TextFormatFlags.WordBreak | TextFormatFlags.HorizontalCenter);

if I remove TextFormatFlags.WordBreak then the text becomes single line.

Its to manual hot track the items while Drag-n-drop over them.

Morry answered 8/7, 2013 at 7:42 Comment(0)
S
8

As Hans taught me, there is a flag for that by including the TextBoxControl flag:

TextRenderer.DrawText(e.Graphics, myString, this.Font,
                      textRec, Color.Black, Color.Empty,
                      TextFormatFlags.HorizontalCenter |
                      TextFormatFlags.TextBoxControl |
                      TextFormatFlags.WordBreak |
                      TextFormatFlags.EndEllipsis);
Showdown answered 8/7, 2013 at 15:53 Comment(5)
However, it uses different approach to render the text and i still cant get the exact copy of text. For example : gap between "ll" rendered new way a bit bigger and all text looks wider (Morry
@Morry DrawString had a lot of issues, which was why Microsoft fixed a lot of things with TextRenderer. Unfortunately, TextRenderer has some limitations, too. There is always WPF to consider, for another answer.Showdown
Just use TextFormatFlags.TextBoxControl instead. Don't hurt me :)Angio
@HansPassant The information by the TextFormatFlags Enumeration document is sparse on the subject to say the least. As always, danke schön.Showdown
Yes, that's close :) Dank je between friends or growing up in the 1970s. Definitely je.Angio

© 2022 - 2024 — McMap. All rights reserved.