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.
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 ));
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.
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.
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.
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.
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.