I've nearly given up on trying to find out how to send output such as Hello World to a printer. I'm new at this, and have googles a lot but still have no idea. I don't want to do it from the Prt Scr button, because There may be several pages of output, but no graphics. I've tried the HWPrint, and that won't even compile.
I agree the documentation is very confusing. Fortunately I figured out the answer.
It is rather verbose so please look up each step on MSDN. Yell if you get stuck.
1. Firstly you need to get a HDC from the Printdlg function.
2. Fill in a DOCINFO structure.
3. StartDoc(HDC, &the doc info structure);
4. StartPage(HDC)
5. Draw on your HDC, I think you are after TextOut or DrawText
6. EndPage(HDC)
7. EndDoc(HDC)
I don't know what HDC is, DOCINFO, etc. I thot this forum was for beginners. I appreciate your help, and no offense is intended, it's just that I need real basic things, such as a program that sends Hello World to the printer, as an example.
HDC gHDC = 0;
// Show a print dialog
PRINTDLG lPrintDialog = {0};
lPrintDialog.lStructSize = sizeof(PRINTDLG);
lPrintDialog.hwndOwner = gMainWindow;
lPrintDialog.Flags = PD_RETURNDC;
PrintDlgA(&lPrintDialog); // Shows a print dialog
gHDC = lPrintDialog.hDC; // HDC means a handle to the "device context"
// This sets up information for the spooler on document information
DOCINFO lDocInfo = {0};
lDocInfo.cbSize = sizeof(DOCINFO);
lDocInfo.fwType = 0;
strcpy(lDocInfo.lpszDatatype, "Test Document");
strcpy(lDocInfo.lpszOutput, "";
// Start the document - tells windows there is a document coming
StartDoc(gHDC, &lDocInfo);
// Indicates you are starting a new page
StartPage(gHDC);
// Prepare the text to be written out
char *pCaption = newchar[1024];
strcpy(pCaption, "Hello world of ink.");
// Draw text A requires a rectangle area to draw the text so we set it up
RECT lRect = {0};
lRect.left = 0;
lRect.right = 300;
lRect.top = 0;
lRect.bottom = 300;
// Draws the text on to the device context of the print job
DrawTextA(gHDC, pCaption, strlen(pCaption), &lRect, DT_CENTER+DT_VCENTER);
delete pCaption;
// We are finished now, so like XML tags we need to tell windows we have finished document
EndPage(gHDC);
// We now finish the document too
EndDoc(gHDC);
Drop that code in to a method and call the method from a button click.
Line 27 sets the text that is printed. The above code might not work because it was mostly typed out. e.g. The Rect values are just thrown together. I think using TextOut would be easier than DrawText. I use drawtext more because it has better alignment options.
http://www.msdn.com will be able to show you the different ways you can draw text or pictures.
Method, or procedure or function or process. I come from many languages and therefore get confused with the official term for each :) If you are unfamiliar with this term then I would suggest to go out there and read a few more tutorials on programming, as it is a fundamental basic of every language.
DrawText and TextOut directly draws text to a device. These are windows basics so you might want to do search the web for some tutorials. To read more about them do a search at http://www.msdn.com.
Another possibility, if you are happy with a very basic solution and you are using plain (ascii) text, is to write your text to a (temp?) file and then call.