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.

DimDim
  • 148
  • 1
  • 10

1 Answers1

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);
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • 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 ( – DimDim Jul 09 '13 at 06:28
  • @DimDim 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. – LarsTech Jul 09 '13 at 14:19
  • 1
    Just use TextFormatFlags.TextBoxControl instead. Don't hurt me :) – Hans Passant Feb 04 '14 at 22:52
  • @HansPassant The information by the [TextFormatFlags Enumeration](http://msdn.microsoft.com/en-us/library/system.windows.forms.textformatflags(v=vs.110).aspx) document is sparse on the subject to say the least. As always, danke schön. – LarsTech Feb 04 '14 at 23:19
  • 2
    Yes, that's close :) Dank je between friends or growing up in the 1970s. Definitely je. – Hans Passant Feb 05 '14 at 00:38