Printing out

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.
1. What kind of system (OS) are you using? (Linux, OS X, Window$)
2. How you connect your printer to the system? (USB, Com-port, paraller port)

AFAIK older printers can be attached to paraller port and send text to /dev/lpX (in Linux).

As for OS X and windows go... idk
I'm using XP SP3. I conncect to a newer HP printer with a USB port.
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)

Good luck :)
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.
I'm assuming you are doing windows development being in XP. Sorry if I am incorrect.

Try this - sorry for any bugs/errors I'm quite busy atm. You'll need to include the library comdlg32.

1
2
3
4
// Include these also 
#include <windows.h>
#include <winspool.h>
#include <Commdlg.h> 


To do the actual printing in your code you want to print put in the following.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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 = new char[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);

Last edited on
James B, thank you so much. This is what I was looking for; a solid example of how to. I'll see if I can get it to work.
Oh, I forgot! Now how do I use your program, James B., to print out Hello World for example. Thanks.
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.


What is a method, and how do I use it? And what are TestOut and DrawTest? Thanks.
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.

ShellExecute(NULL, "print", "c:\\temp\\file.txt", NULL, NULL, 0);

This will cause the application associated with .txt file to print the file for you.

(the same trick can be used with other shell verbs like "open" and "explore")

Andy

P.S. You might also be interested in the function GetTempFileName

P.P.S. Actually, you could use the same trick with HTML, RTF, ... easily enough as well.
Last edited on
Topic archived. No new replies allowed.