Problem text wrapping on a label at Runtime.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I can’t find a way to determine how high a label control should be when the
text is longer than the width of the label. The Graphics.MesureSting on CF2.0
does not support multiple lines.

Does anybody know how to measure the number of lines a sting requires within
a given width, without using the DirectX libraries( as I am not sure how to,
nor wish to )?

Further context, the application I am writing, reads an XML file full of
text questions and creates controls on tab pages to gather input from the
user. The controls are created at runtime, the app has to layout the
questions correctly, which means that I need to know how many lines of text
are required to display a string within a given width.
 
Not in managed code, but you can accomplish this with DrawText() from native
code and, I suppose you could P/Invoke it. You'd want something roughly
like:

RECT r;
r.left = left;
r.right = left + width; // The width will be fixed. The call will
calculate a bigger height,
// as necessary, to contain the text
string.
r.top = top;
r.bottom = top + height_of_one_line_of_text_from_font_info;

DrawText( hdc, text, -1 /* assume text is null-terminated */,
r, DT_CALCRECT | DT_LEFT | DT_NOPREFIX | DT_WORDBREAK );

// On return, r contains the 'new' rectangle.

Paul T.
 
Very good, made me giggle, As I am trying to write cross platform code
between desktop and mobile device (I should know better!) and I am back to
P/Invoke. Is it possiable to find the size without drawing the text (drawing
the text is ok if I have to)
 
That won't draw the text; it just calls the function that can also draw the
text. The CalcRect flag stops it from actually being drawn.

If you're expecting to avoid P/Invoke, you're in for a big surprise with the
Compact Framework. OpenNETCF has a huge library that is composed
significantly of wrappers for P/Invokes that the CF doesn't have itself.

Paul T.
 
Back
Top