# Win32 Printing

At the time of writing this extra lesson, Win32lib.ew did not yet include any interface to the Windows print spooler. I created the include ( printw.ew ) using routines written by Jacques Dechenes in his api_wrap package as a temporary file that would allow at least some basic printing to be done through Windows, using the 'default' system printer.

This program simply illustrates how to print using the print spooler using this .ew. In order to use the spooler, one has to first create the necessary links to the Windows .dlls concerned with printing. Printw.ew does this.
One then creates a 'handle' to the default printer using DefaultPrinterName(), and GetPrinterDC().
Then one uses:
GetTextMetrics() to determine the page extents. ( size of default page )
and the 'default' printer's font size.
StartDoc() to start a print document.
StartPage() to start a page in the document.
pTextOut() to 'print' to the printer. ( see caution below ! )
EndPage() to complete that page.
...
EndDoc() to finish the document, and ...
DeleteDC() to 'release' the 'handle' for the print job.

-- pr123.exw

    include win32lib.ew
    include printw.ew
    without warning

    -- create the window and menu.
    constant MenuWin =
      create(Window,"Print Demo",0,Default,Default,340,130,0)
    constant
      PopupFile = create(Menu,"&File",MenuWin,0,0,0,0,0),
         MenuPrint = create(MenuItem,"&Print",PopupFile,0,0,0,0,0),
         MenuSep1  = create(MenuItem,"-",PopupFile,0,0,0,0,0),
         MenuExit  = create(MenuItem,"E&xit",PopupFile,0,0,0,0,0),
      PopupHelp = create(Menu,"&Help",MenuWin,0,0,0,0,0),
         MenuAbout = create(MenuItem,"&About",PopupHelp,0,0,0,0,0),
      Dummy = create(Menu," -prints 3 pages thru FILE,PRINT-",MenuWin,0,0,0,0,0)

    setFont( MenuWin,"Courier New",10,1)

    procedure onMenu_MenuAbout()
        integer result
        result = message_box("Win32Lib Print Demo, prints itself !",
                             "HELLO !",#2000)
    end procedure
    onClick[MenuAbout] = routine_id("onMenu_MenuAbout")

    integer Handle
    object line
    procedure print_stuff(sequence a) --a is filename
    atom myphdc, i, j, charheight, width, hor, ver
    sequence mypr, textinfo, string1
    mypr=DefaultPrinterName()
    myphdc=GetPrinterDC(mypr)
    textinfo=GetTextMetrics(myphdc)
    charheight=textinfo[1]+1 --character height+1,
    --using textinfo[1] only, allows characters to "touch"
    --might be useful for DOS'like graphics and real 'bitmap' printing.
    width=textinfo[2] --character width
    hor=GetDeviceCaps(myphdc,8) --page width
    ver=GetDeviceCaps(myphdc,10) --page length

    string1= " character height  = " & sprintf("%d",textinfo[1]) & " + 1"
    wPuts(MenuWin,string1)
    setPenPos(MenuWin,1,18)
    string1= " character width   = " & sprintf("%d",width)
    wPuts(MenuWin,string1)
    setPenPos(MenuWin,1,36)
    string1= " total page height = " & sprintf("%d",ver)
    wPuts(MenuWin,string1)
    setPenPos(MenuWin,1,54)
    string1=" total page width   = " & sprintf("%d",hor)
    wPuts(MenuWin,string1)

    i=StartDoc(myphdc, a )
    --a bit of fooling around...
    i=StartPage(myphdc)
    pTextOut(myphdc,150,70,"An Example")
    pTextOut(myphdc,400,140,"of placing stuff")
    pTextOut(myphdc,20*width,400,"wherever")
    pTextOut(myphdc,28*width,408," you want..")
    pTextOut(myphdc,10*width,400+4*charheight,"within the page extents.")
    pTextOut(myphdc,floor(hor/2-(8*width)),floor(ver/2-(charheight/2)),
                          "near the middle.")
    pTextOut(myphdc,50,floor(ver-(8*charheight)),"end of page one.")
    i=EndPage(myphdc)
    --end of fooling...

    i=StartPage(myphdc)
     Handle=open( a ,"r")
     j=charheight*4 --some empty space
       while 1 do
         line=gets(Handle)
         if atom(line) then exit end if
         line=line[1..(length(line)-1)] --remove line feed.
    --max. char. per line has not been calculated here !!
    --so do some line parsing here for 'real' printing, using "hor/width=maxx"
    --or the end of your line will "disappear" in the printout.
         if j > ver-charheight*8 then --close to bottom?
           i=EndPage(myphdc)
           i=StartPage(myphdc) --next page
           j=charheight*4 --reset line count
         end if
    --careful here, I think this only likes integers.
         pTextOut(myphdc,0,j,line )
         j=j+charheight --increment line count
       end while
     close(Handle)
    i=EndPage(myphdc) --eject page
    i=EndDoc(myphdc) --and close
    DeleteDC(myphdc)
    end procedure

    procedure onMenu_MenuExit()
        unloadWindow(MenuWin)
    end procedure
    onClick[MenuExit] = routine_id("onMenu_MenuExit")

    procedure onMenu_MenuPrint()
        print_stuff("pr123.exw") --if it ain't there,CRASH.
    end procedure
    onClick[MenuPrint] = routine_id("onMenu_MenuPrint")
    WinMain(MenuWin,Normal)
    ----

These are the original notes I included with my first distribution of this program.

    ..This program ( and printw.ew ) requires win32lib.ew, and it's contents
    were shamelessly "stolen" from the win32lib and api_wrap packages,
    by David Cuny and Jacques Deschenes.

    The included printw.ew is mostly a "cut" and "paste" of the original
    api_wrap code, modified just enough to work with the win32lib package.

    While this program pretends to offer a print demo using win32lib.ew, I
    cannot guarantee that it works anywhere else but on my own computer.
    It will no-doubt crash if your printer can't do "some" graphics, since
    I have NOT included the standard test for printer capability in my
    demo.

    * caution *
    The pTextOut procedure is a "bastard" invention of mine which does not
    exist in the standard Win32 API, and will thus, probably disappear
    when Mr. Cuny adds printing functions to his win32lib.ew, but I believe
    it's the only one, so it shouldn't be too hard to replace in your source
    code later.
    You would also have to remove the reference to my printw.ew 'include'.
    It should not effect any .exe's you create now, however.

    Meanwhile, you CAN play around with Windoze printing thru the "default"
    printer. ;-)

    Wolf
     wolfritz@king.igs.net

..end of extra_02.

CONTENTS