471,348 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

DrawString, multiline string but full width

Hello.
It seems like the default behavior of DrawString is trying to keep the
whole word if possible. I'd like the string to be drawn in multilines,
but filling the entire line width.
For example I'm using a fixed-width font, and the width of the layout
rectangle is that of 12 characters, the result I want is,
This is a sa
mple text.
not,
This is a
sample text.

I set the StringFormatFlags.Wrap = false, but it disabled multiline
drawing that wasn't what I wanted.

Is what I'm trying to do achievable with any formatting options or do
I have to draw each character manually with my own calculation?
Jun 27 '08 #1
4 5941
On Fri, 25 Apr 2008 21:51:04 -0700, Sin Jeong-hun <ty*******@gmail.com>
wrote:
It seems like the default behavior of DrawString is trying to keep the
whole word if possible. I'd like the string to be drawn in multilines,
but filling the entire line width.
[...]
Is what I'm trying to do achievable with any formatting options or do
I have to draw each character manually with my own calculation?
Off the top of my head, I think maybe you want to set the
StringFormat.Trimming property to Character.

If I'm mistaken about that, you may be able to use the TextRenderer class
instead to do your drawing, using the TextFormatFlags.WordBreak flag (or
rather, making sure that flag is _not_ set) to have the text wrapped on
whatever character reaches the very end of the line.

Sorry I'm being so vague...I don't have something right in front of me
that would let me test the code to make sure my recollection is right.

Pete
Jun 27 '08 #2
On Apr 26, 3:29*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Fri, 25 Apr 2008 21:51:04 -0700, Sin Jeong-hun <typing...@gmail.com*
wrote:
It seems like the default behavior of DrawString is trying to keep the
whole word if possible. I'd like the string to be drawn in multilines,
but filling the entire line width.
[...]
Is what I'm trying to do achievable with any formatting options or do
I have to draw each character manually with my own calculation?

Off the top of my head, I think maybe you want to set the *
StringFormat.Trimming property to Character.

If I'm mistaken about that, you may be able to use the TextRenderer class *
instead to do your drawing, using the TextFormatFlags.WordBreak flag (or *
rather, making sure that flag is _not_ set) to have the text wrapped on *
whatever character reaches the very end of the line.

Sorry I'm being so vague...I don't have something right in front of me *
that would let me test the code to make sure my recollection is right.

Pete
Thank you for your reply. But, DrawString() with Trimming property set
to Character didn't work. It was the same as when I didn't even set
the Trimming property. Secondly, TextRender.DrawText draws multiline
text if and only if the TextFormatFlag has WordBreak. Setting
WorkBreak gave the same result as when I used DrawString().
Jun 27 '08 #3
On Apr 26, 5:27*pm, Sin Jeong-hun <typing...@gmail.comwrote:
On Apr 26, 3:29*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Fri, 25 Apr 2008 21:51:04 -0700, Sin Jeong-hun <typing...@gmail.com*
wrote:
It seems like the default behavior of DrawString is trying to keep the
whole word if possible. I'd like the string to be drawn in multilines,
but filling the entire line width.
[...]
Is what I'm trying to do achievable with any formatting options or do
I have to draw each character manually with my own calculation?
Off the top of my head, I think maybe you want to set the *
StringFormat.Trimming property to Character.
If I'm mistaken about that, you may be able to use the TextRenderer class *
instead to do your drawing, using the TextFormatFlags.WordBreak flag (or*
rather, making sure that flag is _not_ set) to have the text wrapped on *
whatever character reaches the very end of the line.
Sorry I'm being so vague...I don't have something right in front of me *
that would let me test the code to make sure my recollection is right.
Pete

Thank you for your reply. But, DrawString() with Trimming property set
to Character didn't work. It was the same as when I didn't even set
the Trimming property. Secondly, TextRender.DrawText draws multiline
text if and only if the TextFormatFlag has WordBreak. Setting
WorkBreak gave the same result as when I used DrawString().
Please just let me know if what I'm trying to do cannot be achieved
with some flags. Then I can start writing manual line breaking codes.
Thank you.

Jun 27 '08 #4
On Sat, 26 Apr 2008 18:29:12 -0700, Sin Jeong-hun <ty*******@gmail.com>
wrote:
Please just let me know if what I'm trying to do cannot be achieved
with some flags. Then I can start writing manual line breaking codes.
For what it's worth, it's very difficult with many esoteric operations for
a person to say for sure that you can't do it easily in .NET. .NET is
incredibly broad in many areas. If a person knows for sure that there
_is_ a way to do it, that's one thing. But to prove that you can't is
much harder.

In other words, you may not find anyone who is confident that their
knowledge is encyclopedic enough about .NET to state for sure that there's
no option that will break a line within a word rather than between words.
And this may be true even if it turns out that you actually can't.

I'm actually surprised that neither of the options I mentioned address the
issue. Just as I can't say for sure that what you want isn't in .NET, I
also can't say for sure that you have successfully applied the options I
mentioned in a way that would perform as you want. It's possible that
they only work in that specific way when used in combination with other
aspects of text formatting and that you haven't tried that particular
combination yet.

But, the fact is that Graphics.DrawString() and TextRenderer.DrawText()
are the two primary ways to draw text in .NET. If you're sure you've
explored all of the viable combinations of settings and overloads for each
of those methods and none do what you want, I suppose you might as well
start working on a more explicit implementation instead.

Pete
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mark Keats | last post: by
1 post views Thread by timtos | last post: by
1 post views Thread by Drew | last post: by
3 posts views Thread by Jonah Olsson | last post: by
7 posts views Thread by Joel Finkel | last post: by
1 post views Thread by johnb41 | last post: by
6 posts views Thread by Dilip | last post: by
1 post views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.