Text Window Sizing

  • Thread starter Thread starter Neil Kiser
  • Start date Start date
N

Neil Kiser

I am trying to locate either a sample routine for or to at least
acquire the knowledge of how to determine how large of a window to
create to hold a text string. From my search so far, it would seem
that this is a common request, but I have yet to come across an
example solution.

I have a situation in which I need to size a window accurately, or
either I have to guess what the largest the window could ever be and
set it to a flat number. Really don't want to do this, therefore I'm
in search of a way to determine, given a string, a font, and a window
width, how high the window would need to be to hold the string.

Thank you for any pointers,
-Neil K.
 
Hi Neil,

You can determine the size of a String using a given font and Graphics
control using Graphics.MeasureString or Graphics.MeasureCharacterRanges.
 
Neil Kiser said:
I am trying to locate either a sample routine for or to at least
acquire the knowledge of how to determine how large of a window to
create to hold a text string. From my search so far, it would seem
that this is a common request, but I have yet to come across an
example solution.

I have a situation in which I need to size a window accurately, or
either I have to guess what the largest the window could ever be and
set it to a flat number. Really don't want to do this, therefore I'm
in search of a way to determine, given a string, a font, and a window
width, how high the window would need to be to hold the string.

I don't know if there's a ".NET way" to do this, but you could probably use
the DrawText API function with the DT_CALCRECT option.

Chris Jobson
 
Hi Neil,

Graphics.MeasureString is the what you need.
You can provide enough info to that method (e.g text wrap, alignment, etc)
to get the size of your window.
The code should be pretty straightforward.
 
Back
Top