Click here to Skip to main content
15,920,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm trying to use the DrawText() function in winapi so that the function will automatically insert a word break when the text grows outside the rectangle, as well as grow and shrink vertically with the number of lines in my text. When i set DT_WORDBREAK, it does it's job correctly, but when i set DT_CALCRECT next to it, all text disappears. any help would be appreciated.

C++
LRESULT CALLBACK printout(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	static PAINTSTRUCT paint;
	static RECT textbox;
	static LPCTSTR blah = "this is a testing string with a bunch of insignifigant words that are for testing purposes only";

	switch(msg)
	{
		case WM_CLOSE:
		{
                        DestroyWindow(hwnd);
			break;
		}
                case WM_DESTROY:
		{
			PostQuitMessage(0);
			break;
		}
		case WM_PAINT:
		{
			hdc = BeginPaint(hwnd, &paint);

				SelectObject(hdc, hFont);
				SetRect(&textbox, 20, 20, 373, 60);
				DrawText(hdc, blah, strlen(blah), &textbox, DT_WORDBREAK | DT_CALCRECT );

			EndPaint(hwnd, &paint);
			break;
		}
		default:
                        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}
Posted

So what's the problem? When the DT_CALCRECT flag is passed, the function ISN'T supposed to actually draw the text. It's supposed to er, calculate the rect that will be needed to display it in - hence the cryptic name of the flag - DT_CALCRECT.

The answer is all in the documentation, I'd suggest you take a closer look:
Draw Text function(windows)[^]
 
Share this answer
 
Comments
FatalCatharsis 28-Aug-11 13:42pm    
Okay, now i get it, yerp another stupid mistake, I'm on a roll today. When the text said it didn't draw the text, i thought it was mentioning it wouldn't draw in those specific cases, where the size of the text is less than the width or when it's a single line. So when the DT_CALCRECT flag is set, all it does when the function is called is return the rectangle with new dimentions to encompass all the text? totally misread that :P .So i just had to call it once with DT_CALCRECT | DT_WORDBREAK and then once more with just DT_WORDBREAK to actually print it out. Worked like a charm! thanks for your help!
Did you see this discussion: http://www.codeguru.com/forum/archive/index.php/t-311264.html[^]? Looks like a known problem.

—SA
 
Share this answer
 
Comments
FatalCatharsis 28-Aug-11 1:35am    
um, i don't quite see how that relates to my problem :/ that guy was complaining that the width of the rect changed slightly with DT_CALCRECT even though it wasn't supposed to. i'm having difficulty with it even being drawn. when i set the DT_CALCRECT flag, there is no text in my control when my application is run.
Sergey Alexandrovich Kryukov 28-Aug-11 14:28pm    
I think you case is a special case of the same problem. Play with parameters to see something first.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900