Graphics.DrawString question

D

Dilip

Howdy Folks

I have a display where the Graphics.DrawString function is called to
display something. Since the text seems to be larger than its
bounding rectangle, the call basically splits the string into multiple
lines.

Is there a way to find out into how many lines the string was split?

I am asking because this string is being displayed inside an owner
drawn list view. I need to make the height of the row big enough to
accommodate this multi-line string. But I can't seem to reliably
determine how to do that. I thought:

Graphics graphicsObject = ...;
Font someFont = ...;
StringFormat sf = ...;
Rectangle rect = ...;

string s = "This is some string";
SizeF measuredSize = graphicsObject.MeasureString(s, someFont, new
Point(2, 2), sf);

graphicsObject.DrawString(s, someFont, someBrush, rect, sf);

int lines = (int)(measuredSize.Width / rect.Width) + 1; // add 1 to be
on the safe side

The above line doesn't seem to give the correct answer.

Any ideas?
 
D

Dilip

[...]
SizeF measuredSize = graphicsObject.MeasureString(s, someFont, new
Point(2, 2), sf);
graphicsObject.DrawString(s, someFont, someBrush, rect, sf);
int lines = (int)(measuredSize.Width / rect.Width) + 1; // add 1 to be
on the safe side
The above line doesn't seem to give the correct answer.

It's hard to know for sure when you don't post the actual code you're  
using.  But assuming the above is reasonably close to what you're doing,  
then the most obvious problem is that you're not calling a MeasureString()  
overload that allows you to specify the width of the rendered text.  
Because of that, MeasureString() has no way to take into account any  
automatic wrapping that will occur when you call DrawString().

Pete

Peter
Apologies. The codebase is quite hairy and I wanted to post something
minimal. Do you mean I should use this overload of MeasureString?

public SizeF MeasureString(string text, Font font, SizeF layoutArea,
StringFormat stringFormat,
out int charactersFitted, out
int linesFilled);

IOW, the code I posted:

SizeF measuredSize = graphicsObject.MeasureString(s, someFont, new
Point(2, 2), sf);
graphicsObject.DrawString(s, someFont, someBrush, rect, sf);

becomes:

Rectangle rect = ...; // bounding rectangle where string is to be
displayed
SizeF layoutRect = new SizeF(rect.Width, rect.Height);
int charsFitted, linesFilled;
SizeF measuredSize = graphicsObject.MeasureString(s, someFont,
layoutRect, sf, out charsFitted,

out linesFilled);

am I right?
 
D

Dilip

Dilip:
        Try with this code segment:

        Graphics graphicsObject = ...;
        Font someFont = ...;
        StringFormat sf = ...;
        Rectangle rect = ...;

        string s = "This is some string";
        SizeF measuredSize = graphicsObject.MeasureString(s, someFont,
new Point(2, 2), sf);

        int lines = (int)(measuredSize.Height /
someFont.GetHeight(graphicsObject));

Gustavo A. Cantero
CEO - Scientia® Soluciones Informáticas
MCP - MCSD - MCTShttp://www.scientia.com.arhttp://www.programandoamedianoche.comhttp://foro.scientia.com.ar

Gustavo

Thank you very much. Much appreciated. I actually noticed your post
a tad late so I didn't try your approach yet. My reply to Peter
elsethread also solved the problem. Do take a look and let me know if
you find anything wrong with it.

Cheers
 
D

Dilip

That's assuming my interpretation of the problem and guess at the answer  
is correct.  

Your interpretation and your guess were both right. I am now able to
determine the # of lines the string is being split into if it doesn't
fit inside the layout area.

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top