Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i run this, (it's a child window), all i get is 4 letters of the string. i figure the rectangle is plenty big enough. am i using Drawtext() incorrectly?


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 test";

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

				SetRect(&textbox, 20, 20, 300, 60);
				DrawText(hdc, blah, sizeof(blah), &textbox, DT_VCENTER);

			EndPaint(hwnd, &paint);

		}
		default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}
Posted

1 solution

You're using incorrect second parameter which is supposed to pass number of characters to draw. And it equals to exactly 4. And why? It all has nothing to do with GDI. This is because you probably don't understand what sizeof does. Do you expect it will give you the length of the string? No way.

Let me guess. You're using 32-bit version of Windows, correct? 32 bits is 4 bytes. Your blah is a pointer, you're using its its size which is just the constant size of a pointer, 4 bytes in your case. Size of a pointer is not a length of string. For length of string, for example, use strlen, or use the system type std:string which properly handles lengths.

—SA
 
Share this answer
 
v2
Comments
FatalCatharsis 27-Aug-11 22:11pm    
shoot, your totally right. no, what i overlooked was that it was a pointer. I knew that sizeof gives you the size of the variable or object in bytes, and i knew a character was a single byte, so i incorrectly assumed that it would give me the number of chars in the string. stupid mistake :P. thanks for you help!
Sergey Alexandrovich Kryukov 27-Aug-11 22:19pm    
You're very welcome. You know, with some experience you make less much less stupid mistakes, but some of your mistakes become tricky to fix. Is it any better? :-) Oh, sure, they would make an excellent question for CodeProject :-)

Good luck, call again.
--SA
Albert Holguin 29-Aug-11 14:31pm    
You must be looking to be the first to reach a million points on CP... :D
Sergey Alexandrovich Kryukov 29-Aug-11 15:17pm    
I don't think so (at least I'm not really looking to anything like that), but who knows... I think you just added me 40 of them — thank you.
--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