Printing a single line of text requires the following steps:
1.Create the device context for the printer.
2.Start the print request.
3.Print the line.
4.Start a new page.
5.End the print request.
6.Delete the device context.
The following example shows how to print a single line of text on an Epson
FX-80 printer that is connected to the printer port, LPT1:
1 hPr = CreateDC(“EPSON”,
“EPSON FX-80”,
“LPT1:”,
(LPSTR) NULL);
if(hPr != NULL) {
2 Escape(hPr, STARTDOC, 5, (LPSTR) “Test”, 0L);
3 TextOut(hPr, 10, 10, “A single line of text.”, 22);
4 Escape(hPr, NEWFRAME, 0, 0L, 0L);
5 Escape(hPr, ENDDOC, 0, 0L, 0L);
6 DeleteDC(hPr);
}
In this example:
1 | The CreateDC function creates the device context for the printer, and returns a handle to the printer device context. This example stores the handle in the variable hPr. When calling CreateDC, an application must supply the first three parameters; the fourth parameter can be set to NULL. In this example, the application supplies the following parameters: |
The first parameter specifies the name of the device driver, EPSON. | |
The second parameter specifies the name of the printer device, EPSON FX-80. | |
The third parameter specifies the printer port, LPT1:. | |
The last parameter to CreateDC specifies how to initialize the printer. NULL specifies the default print settings. | |
2 | The Escape function starts the print request by sending the STARTDOC escape sequence to the device context. The name Test identifies the request; the third parameter is the length of the string Test, plus a null terminator. Because the other parameter is not used, it is set to zero. |
3 | TextOut copies the line of text to the printer. The line will be placed starting at the coordinates (10, 10) on the printer paper (the printer coordinates are always relative to the upper-left corner of the paper). The default units are printer pixels. |
4 | The NEWFRAME escape completes the page and signals the printer to advance to the next page. Because the other parameters are not used, they are set to zero. |
5 | The ENDDOC escape signals the end of the print request. Because the other parameters are not used, they are set to zero. |
6 | The DeleteDC function deletes the printer device context. |
NOTE:
You should 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 actual printing does not begin until after the ENDDOC escape.