Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use DrawString without trimming?

Tags:

c#

winforms

I find the way the DrawString function cuts off entire characters or words to be counterintuitive. Showing parts of the text clearly conveys that something is missing.

Here are some examples:

StringTrimming.None: enter image description here

StringTrimming.Character: enter image description here

What I want: enter image description here (GIMP mockup)

Is StringTrimming.None the same as StringTrimming.Character? They seem to behave exactly alike in my case. Is there something I could have overlooked or is this a known "feature"?

According to the docs StringTrimming.None "Specifies no trimming."

This site with examples created with Java even show "None" to trim more characters than "Character".

Are there other tricks to get this effect?

Note: I do not want to display "…" or similar. I find this to be a waste of space but that is probably a discussion for UX.

like image 577
Sarien Avatar asked Jun 04 '13 14:06

Sarien


1 Answers

It's possible that the text appears to be trimmed because it's actually wrapping invisibly onto the next line. In the call to Graphics.DrawString, disable wrapping by passing a StringFormat object whose FormatFlags property is set to NoWrap:

StringFormat format =
    new StringFormat
    {
        FormatFlags = StringFormatFlags.NoWrap,
        Trimming = StringTrimming.None
    };
g.DrawString(s, font, brush, rect, format);
like image 141
Michael Liu Avatar answered Nov 01 '22 08:11

Michael Liu