views:

150

answers:

3
+1  Q: 

Wordwrap Win32

Hello all :)

I'm trying to wordwrap a block of text for display in a window that would otherwise be too long. The font I'm using is not a fixed width font, so GetSystemMetrics will not return accurate values for my text.

Using a static window is not an option here because a static window doesn't tell me one crucial piece of information: The height of the text drawn after wordwrapping.

Any ideas?

Billy3

+2  A: 

You could use the DrawText() API function with the DT_CALCRECT flag set. You would need to select the correct font for the HDC first.

mghie
Thank you very much :)
Billy ONeal
+4  A: 

Check out the Win32 API call DrawtextEx. You'll need to pass the DT_CALCRECT option, telling Windows that you wish for the rectangle to be calculated.

Stephen Nutt
Thank you very much :)
Billy ONeal
A: 

DrawTextEx() will do everything you want and need if the entire text is to be displayed in a single font.

If you need to mix fonts, you'll have to do the work yourself. In that case, you'll want to look at APIs like GetTextMetrics() (not GetSystemMetrics()) and GetTextExtentExPoint32() to figure out positions and sizes for each run.

If you need to handle complex scripts (e.g., right-to-left languages and scripts where the letters change shape depending on context), you'll need Uniscribe. Caution: Uniscribe is powerful but very low level. It can take a lot of work to wrap it with a higher-level interface. If you need complex script handling, you might be better off using a browser control.

Adrian McCarthy