12.3 Printing a Line of Text

Printing a single line of text requires the following steps:

1.Create the device context for the printer.

2.Start the print request (also called a print job).

3.Start a page.

4.Print the line.

5.End the page.

6.End the print request.

7.Delete the device context.

The following example prints a single line of text on the currently selected default printer:

LPSTR lpPrintDevice;
LPSTR lpPrintDriver;
LPSTR lpPrintPort;

HDC        hdcPrint;
DOCINFO    DocInfo;         /* used in StartDoc function     */

hdcPrint = CreateDC(lpPrintDriver,
    lpPrintDevice,
    lpPrintPort,
    (LPSTR) NULL);






if (hdcPrint != NULL) {
    DocInfo.cbSize = sizeof(DOCINFO);
    DocInfo.lpszDocName = "Test";
    DocInfo.lpszOutput = (LPSTR) NULL;
    StartDoc(hdcPrint, &DocInfo);
    StartPage(hdcPrint);
    TextOut(hdcPrint, 10, 10, "A single line of text.", 22);
    EndPage(hdcPrint);
    EndDoc(hdcPrint);
    DeleteDC(hdcPrint);
}

In this example, the CreateDC function creates the device context for the printer and returns a handle of the printer device context. This example stores the handle in the variable hdcPrint. When calling CreateDC, an application must supply the first three parameters; the fourth parameter can be set to NULL. In this example, it is assumed that the GetProfileString function has been used to supply the parameters to the CreateDC function. The last parameter to CreateDC specifies how to initialize the printer. NULL specifies the default print settings. For more information about specifying print settings that differ from the default settings, see Chapter 17, “Print Settings.”

After the device context has been created, the StartDoc function starts the print request by sending Windows the handle of the device context and a far pointer to a DOCINFO structure. The DOCINFO structure has three members, which describe the size of the structure, the name of the document being sent to the printer (that is, the name that is displayed by Print Manager), and the name of the output file (if the application sends output to a file). If the name of the output file is NULL, the output is sent to the device specified by the device context. (In this example, the output goes to the port specified by the lpPrintPort parameter in the call to the CreateDC function).

The StartPage function instructs the printer driver to begin a new page frame. The TextOut function copies the line of text to the printer. The line will be placed starting at the coordinates (10,10) on the printer paper. The default units are printer pixels. The default printer coordinates are relative to the upper-left corner of the printable area. (An application can change either of these by changing the mapping mode associated with the device context.)

Note:

Do not expect the line of text to be printed immediately. The spooler collects all output for a print request before sending it to the printer, so any printing does not begin until after the call to the EndDoc function.

The EndPage function completes the page. The EndDoc function signals the end of the print request. Finally, the DeleteDC function deletes the printer device context.