I had the code before, but I somehow lost it. How do I use TextOut?
Printable View
I had the code before, but I somehow lost it. How do I use TextOut?
Hi,
BOOL TextOut(
HDC hdc, // handle to device context
int nXStart, // x-coordinate of starting position
int nYStart, // y-coordinate of starting position
LPCTSTR lpString, // pointer to string
int cbString // number of characters in string
);
Eg:
TextOut(hdc, 10, 10, "Printing Text", 13);
Hope That Helps...
Cheers
TNT
Arg!!! You got to it before me TNT. I'm new to Windows programming (1 week to be exact) and I actaully had the answer to it. Oh well, at least I know I know it.
WINDOWS PROGRAMMING IS GREAT!!!
--Garfield
Well, I tried:
HDC hdc;
TextOut(hdc,10,10,"Hi",2);
but no text shows up.
forgot to assign hdc to something
hdc = GetDC(hwnd);
I tried:
HDC hdc;
hdc = GetDC(hwnd);
TextOut(hdc,10,10,"Hi",2);
but it didn't show up.
KEN is right, you forgot to assign the HDC to something. You could do what KEN said:
[code]
HDC hdc;
hdc = GetDC(hwnd);
TextOut(hdc, 10, 10, TEXT("TEST"), 4);
ReleaseDC(hwnd, hdc); // don't forget to release the HDC
[code]
Or you could use another set of functions known as BeginPaint and EndPaint:
So, there are a couple of ways. You would normally use BeginPaint and EndPaint inside the WM_PAINT message. And then GetDC and ReleaseDC outside of it.Code:case WM_PAINT:
hdc = BeginPaint(hwnd, &ps); // where ps is defined PAINTSTRUCT
TextOut(hdc, 10, 10, TEXT("TEST"), 4);
EndPaint(hwnd, &ps);
return 0;
--Garfield
How's that for just one week of learning Windows Programming :D
then try...
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(hwnd, &ps);
TextOut(hdc,10,10,"Hi",2);
EndPaint(hwnd, &ps);
Ack! Garfield beat me to it while I was replying...damn you, garfield! :p
>
I tried:
HDC hdc;
hdc = GetDC(hwnd);
TextOut(hdc,10,10,"Hi",2);
but it didn't show up.
<
Maybe you need to release the HDC (ReleaseDC(hwnd)). I don't know if that's the problem though. By the way, I'll have to check the parameters for ReleaseDC, it doesn't look right.
--Garfield
KEN, am I on the right track? After all, I'm really new to Windows programming. Thanks.
--Garfield
sure, but I dunno if releasing the DC would do much to stop the text from showing up...
Yeh, I didn't think it would cause the text not to appear. Maybe it is the rest of his code. And, KEN, do I have the parameters right for ReleaseDC? Thanks.
--Garfield
(Wow, 10 posts in 9 minutes. That has to be a record)
Oh, and garfield I can't send large PMs, so in response to you PM...
Here's how I want you to think of a message from now on (and if you look at the struct MSG I'm not far off)
Windows Messages:
Kinda gettin the picture? Every time a message is sent to windows, the WPARAM is to the main window, and LPARAM is a message to any child window, etc. The Hiword and loword contain additional data such as coordinates.PHP Code:
WPARAM LPARAM
------------------------------------------------------
| | |
| | |
| HIWORD | HIWORD |
| | |
| | |
| ---------------------------------------------------|
| | |
| | |
| | |
| LOWORD | LOWORD |
| | |
| | |
------------------------------------------------------
Okay, so LPARAM and WPARAM are just additional information of the MSG? And HIWORD and LOWARD are different ways to get at the different information? Thanks. I guess I understand.
--Garfield
whoo. I got my graph working right...
Nono, he msg is only composed of WPARAM and LPARAM. Those are the only two pieces of info in there, and the HIWORD and LOWORD are ways to get at different parts of them.
Oh, I see. Your graph couldn't have been better.
--Garfield
Back to the subject. SyntaxBubble, post your code or attach the file. Then we can look at it fully.
--Garfield the Great
Hi,
I am trying to get a multi-line string (m_Poem) to print out -- how do you get the Textout to acknowledge the carriage returns, instead of printing them out as little boxes? Or is there a better function to use? This is Visual C++, btw.
if (dcPrint.StartDoc(&myPrintjob)>=0)
{
dcPrint.StartPage();
dcPrint.TextOut(10,10,m_Poem);
dcPrint.EndPage();
dcPrint.EndDoc();
dcPrint.DeleteDC();
}
Try this.
First find out how big the font prints out with
GetTextExtentPoint32(hdcPrinter, sBuffer, lstrlen(sBuffer), &Size);
the Size struct will be stacked with values
Size.cx is the length of the string across
Size.cy is the height of the text
You will need a loop for each line of the poem
After each line increment the Y Cood by the Size.cy
Size.cx can be used to justify text
ie
(Width-Size.cx)/2 == Center
>>Or is there a better function to use? <<
Or you could always try DrawText:(Words 'borrowed' from Al Stewart's - a small fruit song). Hope that helps. :)Code:case WM_PAINT:
{
PAINTSTRUCT ps;
BeginPaint(hwnd,&ps);
TCHAR chTxt[]=TEXT("Said the apple to the orange,\n")
TEXT("Oh, I wanted you to come\n")
TEXT("Close to me and kiss me to the core.\n")
TEXT("Then you might know me like no other orange\n")
TEXT("Has ever done\n")
TEXT("Before");
RECT rc;
GetClientRect(hwnd,&rc);
InflateRect(&rc,-30,-30);
SetBkMode(ps.hdc,TRANSPARENT);
DrawText(ps.hdc, chTxt, lstrlen(chTxt), &rc, DT_WORDBREAK);
EndPaint(hwnd,&ps);
return 0;
}
edit: format
Note: If you use mingw compiler(gcc v3.2) then multi-line text is deprecated. Just use one TEXT macro to enclose the whole of the text which can still occupy multiple lines rather than one usage per line of text.
Thanks. I was trying to do an example from a book and replace their text string with a variable. It prints out, but it's all on one line, LOL. I was thinking I'd have to loop, but my VC++ MSDN file is corrupted for some reason (probably because I had installed VB.net, then removed it after I got mad at it for not having a decent packager like the old one), & I couldn't find the string length function in the book.
Anyway, what I really wanted to do was build sort of like an e-book. So I wanted to print out using a string variable, which would vary in length, depending on the poem that the user selects. What I need to do is loop through a string, and look for a specific character (\r\n) & use that as the string length, then go on to the next part of the string, & so on. There has to be a way to do this in C++, I've done it in Cold Fusion.
This is the whole code for the printing function.
void CPoetryDlg::OnPrint()
{
// TODO: Add your control notification handler code here
//Construct a CPrint Dialog object
CPrintDialog dlgPrint(FALSE,PD_ALLPAGES,this);
if (dlgPrint.DoModal()==IDOK)
{
//Attach printer DC from dialog to a CDC object
CDC dcPrint;
dcPrint.Attach(dlgPrint.GetPrinterDC());
//Create and fill a Docinfo structure
DOCINFO myPrintjob;
myPrintjob.cbSize=sizeof(myPrintjob);
myPrintjob.lpszDocName="myPrintjob";
myPrintjob.lpszOutput=NULL;
myPrintjob.lpszDatatype=NULL;
myPrintjob.fwType=NULL;
//Start printing document
if (dcPrint.StartDoc(&myPrintjob)>=0)
{
dcPrint.StartPage();
dcPrint.TextOut(10,10,m_Poem);
dcPrint.EndPage();
dcPrint.EndDoc();
dcPrint.DeleteDC();
}
}
}
Argh! Why is this board completely ignoring my formattting?
Anyway...is there a good, advanced, free printing tutorial online that would cover this? I can't afford to buy another $40 programming book at the moment.
Then you really should use DrawText(), it will take care of that for you. But novacains idea is basically what I use for printing, and in fact you can wrap these up into functions quite easily. SetTextAlign() is helpful too, and be sure to set the BackMode to TRANSPARENT if the background is colored.
Thanks for the help. :)
I know this is a very old thread, but it's where I found the answer to my issue but it raises a question for me.
Why does this work (specifically the part IDM_FILE_NEW)
but the following has no effect at all.Code:switch (wmId) {
case IDM_FILE_EXIT:
DestroyWindow(hWnd);
break;
case IDM_FILE_NEW:
hdc = GetDC(hWnd);
TextOut(hdc, 5, 5, greeting1, _tcslen(greeting1));
ReleaseDC(hWnd, hdc);
break;
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
do the BeginPaint, and EndPaint functions only work in the context ofCode:switch (wmId) {
case IDM_FILE_EXIT:
DestroyWindow(hWnd);
break;
case IDM_FILE_NEW:
hdc = BeginPaint(hWnd, &ps);
TextOut(hdc, 5, 5, greeting1, _tcslen(greeting1));
EndPaint(hWnd, &ps);
break;
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
because that doesn't make sense to me.Code:case WM_PAINT:
...
break;
Holy Thread Necrophillia!
Yes, BeginPaint() can only be called in response to a WM_PAINT msg.
It is not valid to call it from within any other msg.
Also the HDC, rect, etc from the BeginPaint will only be valid if there has been a call to InvalidateRect() (which sets the area requiring a redraw).
I suggest putting abreak point in and looking at the values returned.
Please start a new thread next time...
EDIT: seven years and I am still answering the same question....