TextOut vs TextDraw

I am trying to display text on my program. I have searched for about 12 hours total trying to figure out how to use one of them. If someone could help me that would be fantastic.

Thanks,
MM
What have you written so far.

P.S - It is DrawText not TextDraw
Here is what i have written so far:
1
2
HDC hdc;
RECT* prc

DrawText(hdc, "Score", -1, prc, DT_WORDBREAK);
That is what i have so far.

Thanks,
MM
Assuming you have a window up and running and have an hdc, it's hard to see what the problem is. Have you read up on the functions?

TextOut
http://msdn.microsoft.com/en-us/library/dd145133(VS.85).aspx

DrawText
http://msdn.microsoft.com/en-us/library/dd162498(VS.85).aspx

The main problem beginners might have here is that you need to write the string to a buffer first. Since (w)sprintf returns the number of characters it wrote into the buffer, you can use it's return value for the last argument. The idiom for TextOut is:

1
2
char buf[ BUFFER_SIZE ];
TextOut( hdc, x, y, buf, wsprintf( buf, "The number is %d.", n ));

I don't think you have got hold of the DC for the window.

Did you setup the DC either using the GetDC function (or by calling BeginPaint function in response to the WM_PAINT message) before calling TextOut ??
Last edited on
What do you mean by:
"setup the DC either using the GetDC function (or by calling BeginPaint function in response to the WM_PAINT message) before calling TextOut ??"
I don't get any of that.

MM
You say you are "trying to display text on my program", incicating that you have some kind of program already. But now you imply that you don't even know the basics of windows programming? That's a different question altogether.

So your real question is "how do I make a non-console program in windows". Most people here (having learned unix in university) will suggest a cross-platform solution, such as Qt. So your first decision is whether you want to go cross-platform or native.

To see what a native windows program looks like, see the generic app on MSDN: http://msdn.microsoft.com/en-us/library/aa383668(VS.85).aspx
Ok i do know how to make a window, menu, and ect. I just have never hear of GetDC. I havn't had my programs so far display text on the sceen. So that is why im getting help.

MM
Last edited on
I see. What did your program (with the window, menu, etc) do? Do you know what a device context is? I need to understand the level of help you need, otherwise I'll waste my time explaining the basics (like device contexts).
My program was Notepad and it could save, open, close, option to word wrap, and other stuff that makes a good note pad program including an icon. A device contexts is a data structure.

MM
> I havn't had my programs so far display text on the sceen.

You made a notepad replacement that doesn't display text on the screen?
Ok, Im still confused on how to use the TextOut function.
Here is what i have:
1
2
3
4
int TestX = 5;
int TestY = 5;
HDC hdc;
TextOut(hdc, TextX, TextY, "Score:", //I'm not sure what goes here 
Windows have the concept of a Device Context (aka DC).
This applies to things like windows on screen, and the printer.
To display stuff in a window for example you have to get a handle to the Device Context (HDC) for that window.
You then use that HDC in GDI function calls to display your stuff in that window.

You can also create device contextes in memory and use the blitting function to transfer stuff
between device contextes.

So in essense you do not manipulate the window display area directly, you do it indirectly through the DC.

(Note that if you are painting on a window in response to a WM_PAINT message - you have to get the DC in a particular way and end the paint sequence in a particular way.

Here is a simple peice of code in response to a WM_PAINT message, that paints "Hello World"
in a window.

1
2
3
4
5
6
7
8
	
case WM_PAINT:
PAINTSTRUCT ps;
HDC hdc; 
hdc = BeginPaint(hWnd, &ps);  //ask windows for the Context - use this method when dealing with WM_PAINT messages
TextOut(hdc, 0,0, _T("Hello World"),11); //put some text on the window client area.
EndPaint(hWnd, &ps); //Always end a response to WM_PAINT like this
break;

Ok Thanks to everyones help i got the text to work. guestgulkan Thanks for that source i got it to work. All i had to change was the TextOut(hdc, 0,0, _T("Hello World"),11);
to TextOut(hdc, 0,0, TEXT("Hello World"),11);.
Thanks agin for all yor help.
Topic archived. No new replies allowed.