INF: Steps to Send a Document to a Printer

ID Number: Q75339

3.00

WINDOWS

Summary:

If code simplicity is more desirable than printing efficiency, an

application can implement printing with almost no additional code. For

this discussion, the application defines a DrawStuff procedure that

renders text and/or graphics into a specified display context. The

application can render the image to the screen by getting a screen

display context and passing its handle to DrawStuff. Printing is

more involved; the seven steps required are detailed below.

1. Use the GetProfileString function to get the printer driver name

and port to print to from the WIN.INI file. Specify "windows" as

the application and "device" as the key name.

2. Call the CreateDC function to get a display context for the

printer.

The following code demonstrates these two steps:

//

// HDC GetPrinterDC(void)

//

// Return a DC to the currently selected printer.

// Returns NULL on error.

//

HDC GetPrinterDC(void)

{

static char szPrinter[64];

char *szDevice, *szDriver, *szOutput;

GetProfileString("windows", "device", "", szPrinter, 64);

if ((szDevice = strtok(szPrinter, "," ))

&& (szDriver = strtok(NULL, ", "))

&& (szOutput = strtok(NULL, ", ")))

return CreateDC(szDriver, szDevice, szOutput, NULL);

return NULL;

}

3. Use the STARTDOC Escape to start a print job.

szJobName = "<job name>";

Escape(hDC, STARTDOC, lstrlen(szJobName), szJobName, NULL);

4. Draw the page by calling DrawStuff(hDC).

5. Use the NEWFRAME Escape to start the next page.

Escape(hDC, NEWFRAME, 0, 0L, 0L);

[If more than one page is printed, repeat steps 4 and 5 for each page]

6. Use the ENDDOC Escape to end the print job.

Escape(hDC, ENDDOC, 0, 0L, 0L);

7. Call the DeleteDC function to free the printer display context.

DeleteDC(hDC);

Printing requires little extra work if the drawing code is modular.

The drawback to the approach above is that it can require more memory

and printing time than may otherwise be necessary. For more

information on speeding the printing process, query in this knowledge

base on the following words:

prod(winsdk) and printing and banding