Bare-Bones Printing

PRINT1, the first version of the printing program, is shown in Figure 15-6. After compiling PRINT1, you can execute it and then select Print from the system menu. If your WIN.INI file has the line Spooler=yes and if Windows can find PRINTMAN.EXE, you should see the Print Manager icon appear at the bottom of the screen. If the TEMP variable in your MS-DOS environment indicates a fixed disk (or if you have no TEMP variable), then you should see some disk activity as the GDI module saves the printer output to a temporary file. You won't be able to do anything in Windows during this time. After PRINT1 has finished, the Print Manager should display the text ”Print1: Printing“ in its client area and begin sending the disk file out to the printer. You'll be able to work normally in Windows again.

Let's look at the code in PRINT1.C. If PrintMyPage can't obtain a device context handle for the printer, it returns TRUE, and WndProc displays the message box indicating an error. If the function succeeds in obtaining the device context handle, it then determines the horizontal and vertical size of the page in pixels by calling GetDeviceCaps:

xPage = GetDeviceCaps (hdcPrn, HORZRES) ;

yPage = GetDeviceCaps (hdcPrn, VERTRES) ;

This is not the full size of the paper but rather its printable area. After that call, the code in PRINT1's PrintMyPage function is structurally the same as the code in FORMFEED, except that PRINT1 calls PageGDICalls between the STARTDOC and NEWFRAME Escape calls. Only if both the STARTDOC and NEWFRAME calls are successful does PRINT1 call ENDDOC Escape.

PRINT1.MAK

#----------------------

# PRINT1.MAK make file

#----------------------

print1.exe : print.obj print1.obj print1.def

link print1 print, /align:16, NUL, /nod slibcew libw, print1

rc print1.exe

print.obj : print.c

cl -c -Gsw -Ow -W2 -Zp print.c

print1.obj : print1.c

cl -c -Gsw -Ow -W2 -Zp print1.c

PRINT1.C

/*---------------------------------------

PRINT1.C -- Bare-Bones Printing

(c) Charles Petzold, 1990

---------------------------------------*/

#include <windows.h>

HDC GetPrinterDC (void) ; // in PRINT.C

void PageGDICalls (HDC, short, short) ;

HANDLE hInst ;

char szAppName [] = "Print1" ;

char szCaption [] = "Print Program 1" ;

BOOL PrintMyPage (HWND hwnd)

{

static char szMessage [] = "Print1: Printing" ;

BOOL bError = FALSE ;

HDC hdcPrn ;

short xPage, yPage ;

if (NULL == (hdcPrn = GetPrinterDC ()))

return TRUE ;

xPage = GetDeviceCaps (hdcPrn, HORZRES) ;

yPage = GetDeviceCaps (hdcPrn, VERTRES) ;

if (Escape (hdcPrn, STARTDOC, sizeof szMessage - 1, szMessage, NULL) > 0)

{

PageGDICalls (hdcPrn, xPage, yPage) ;

if (Escape (hdcPrn, NEWFRAME, 0, NULL, NULL) > 0)

Escape (hdcPrn, ENDDOC, 0, NULL, NULL) ;

else

bError = TRUE ;

}

else

bError = TRUE ;

DeleteDC (hdcPrn) ;

return bError ;

}

PRINT1.DEF

;-----------------------------------

; PRINT1.DEF module definition file

;-----------------------------------

NAME PRINT1

DESCRIPTION 'Printing Program No. 1 (c) Charles Petzold, 1990'

EXETYPE WINDOWS

STUB 'WINSTUB.EXE'

CODE PRELOAD MOVEABLE DISCARDABLE

DATA PRELOAD MOVEABLE MULTIPLE

HEAPSIZE 1024

STACKSIZE 8192

EXPORTS WndProc