2016-06-01

Bo Durban - Moxiedata Blog - Display the Print Dialog Using PrintDlgEx

Original post by Bo Durban, replicated here because the original blog http://blog.moxiedata.com is not responding.
Copied from:
http://web.archive.org/web/20101017154139/http://blog.moxiedata.com/PermaLink,guid,7876146a-96e0-47dd-b442-271cad494a5d.aspx


Display the Print Dialog Using PrintDlgEx

As I mentioned in my post from 9/29/2008, Microsoft removed the Printer button from the "Page Setup" dialog in Vista. It was reccommneded to use the "Print" dialog to change printer preferences. However, the "Print" dialog option is not always available on the menu, and there is no simple call to activate the dialog.
In my post from 9/29, I provided code for displaying the DocumentProperties dialog. This populates a DEVMODE structure from the Advanced Properties dialog for a specific printer. In today's post, I provide sample code for displaying the Print dialog using the PrintDlgEx API function. This is the dialog Microsoft recommendeds for setting printer options in Vista. This dialog populates a DEVMODE structure also, however, it includes the ability to select a printer and specify a print range too.

Paste this code into a PRG and run it. It will display user chosen options to the screen.
While this dialog is meant to replace missing functionality from SYS(1037) in Vista, this sample does not update your user preferences. You will need to update those based on the user action (see "Action" in the code below).
Note that the PrintDlgEx API function works with Windows 2000 and later.


** Converted from: http://msdn.microsoft.com/en-us/library/ms646829.aspx
** AUTHOR: BO DURBAN** 

#Define SIZEOF_PRINTDLGEX     84
#Define SIZEOF_PRINTPAGERANGE  8

** nFlag options
#Define PD_ALLPAGES                  0x00000000
#Define PD_SELECTION                 0x00000001
#Define PD_PAGENUMS                  0x00000002
#Define PD_NOSELECTION               0x00000004
#Define PD_NOPAGENUMS                0x00000008
#Define PD_COLLATE                   0x00000010
#Define PD_PRINTTOFILE               0x00000020
#Define PD_PRINTSETUP                0x00000040
#Define PD_NOWARNING                 0x00000080
#Define PD_RETURNDC                  0x00000100
#Define PD_RETURNIC                  0x00000200
#Define PD_RETURNDEFAULT             0x00000400
#Define PD_SHOWHELP                  0x00000800
#Define PD_USEDEVMODECOPIES          0x00040000
#Define PD_USEDEVMODECOPIESANDCOLLATE 0x00040000
#Define PD_DISABLEPRINTTOFILE        0x00080000
#Define PD_HIDEPRINTTOFILE           0x00100000
#Define PD_NONETWORKBUTTON           0x00200000
#Define PD_CURRENTPAGE               0x00400000
#Define PD_NOCURRENTPAGE             0x00800000

#Define START_PAGE_GENERAL    Bitlshift(0xffffffff,0)
#Define GMEM_FIXED       0x00
#Define GMEM_ZEROINIT    0x40
#Define GPTR             (GMEM_FIXED+GMEM_ZEROINIT)
#Define DM_OUT_BUFFER  2
#Define DM_IN_PROMPT   4
#Define CCHDEVICENAME 32
#Define CCHFORMNAME   32
#Define PD_RESULT_CANCEL               0
#Define PD_RESULT_PRINT                1
#Define PD_RESULT_APPLY                2

Declare Long GlobalAlloc In WIN32API Long uFlags, Long uBytes
Declare Long GlobalLock In WIN32API Long Hmem
Declare Long GlobalUnlock In WIN32API Long Hmem
Declare Long GlobalFree In WIN32API Long Hmem
Declare Integer PrintDlg In comdlg32.Dll Long lppd
Declare Integer PrintDlgEx In comdlg32.Dll Long lppd
Declare Long DeleteDC In WIN32API Long hdc

Local hPRINTDLG, hPageRanges, hdc, hResult, nFlagsm, hResult
Local hGDevMode, hGDevNames, nPageRange, hPageRange, lnAction
Local lcDeviceName, lcFormName, hDevMode, hDevNames

** Allocate memory for the PRINTDLGEX structure
hPRINTDLG = GlobalAlloc(GPTR,SIZEOF_PRINTDLGEX)
** Allocate memory for 10x PRINTPAGERANGE structures
hPageRanges = GlobalAlloc(GPTR,SIZEOF_PRINTPAGERANGE*10)
hdc = 0
hResult = 0

** Initialize the PRINTPAGERANGE structure
nFlags = Bitor(PD_RETURNDC,PD_ALLPAGES)
Sys(2600,hPageRanges,8,BinToC(1,"4rs")+BinToC(1,"4rs"))

** Initialize the PRINTDLGEX structure
Sys(2600,hPRINTDLG+ 0,4,BinToC(SIZEOF_PRINTDLGEX,"4rs")) && lStructSize
Sys(2600,hPRINTDLG+ 4,4,BinToC(_vfp.HWnd,"4rs"))         && hwndOwner
Sys(2600,hPRINTDLG+20,4,BinToC(nFlags,"4rs"))            && Flags
Sys(2600,hPRINTDLG+32,4,BinToC(0,"4rs"))                 && nPageRanges
Sys(2600,hPRINTDLG+36,4,BinToC(10,"4rs"))                && nMaxPageRanges
Sys(2600,hPRINTDLG+40,4,BinToC(hPageRanges,"4rs"))       && lpPageRanges
Sys(2600,hPRINTDLG+44,4,BinToC(1,"4rs"))                 && nMinPage
Sys(2600,hPRINTDLG+48,4,BinToC(1000,"4rs"))              && nMaxPage
Sys(2600,hPRINTDLG+52,4,BinToC(1,"4rs"))                 && nCopies
Sys(2600,hPRINTDLG+76,4,BinToC(START_PAGE_GENERAL,"4rs")) && nStartPage

** Display the Print dialog
hResult = PrintDlgEx(hPRINTDLG)

** Pull updated values from PRINTDLGEX structure
hGDevMode = CToBin(Sys(2600,hPRINTDLG+8,4),"4rs")
hGDevNames = CToBin(Sys(2600,hPRINTDLG+12,4),"4rs")
hdc = CToBin(Sys(2600,hPRINTDLG+16,4),"4rs")
nFlags = CToBin(Sys(2600,hPRINTDLG+20,4),"4rs")
lnAction = CToBin(Sys(2600,hPRINTDLG+80,4),"4rs")

If hResult = 0
** Lock movable memory
   hDevMode = GlobalLock(hGDevMode)
   hDevNames = GlobalLock(hGDevNames)

** Display Action taken by user
   Do Case
      Case lnAction = PD_RESULT_CANCEL
         ?"Action: CANCEL"
      Case lnAction = PD_RESULT_PRINT
         ?"Action: PRINT"
      Case lnAction = PD_RESULT_APPLY
         ?"Action: APPLY"
   Endcase

** Display print range selection
   Do Case
      Case lnAction != PD_RESULT_PRINT
** No Printing
      Case Bitand(nFlags,PD_SELECTION)=PD_SELECTION
         ?" Selection"
      Case Bitand(nFlags,PD_CURRENTPAGE)=PD_CURRENTPAGE
         ?" Current Page"
      Case Bitand(nFlags,PD_PAGENUMS)=PD_PAGENUMS
         ?" Page Ranges"
         nPageRanges = CToBin(Sys(2600,hPRINTDLG+32,4),"4rs")
         For nPageRange = 0 To nPageRanges-1
            hPageRange = hPageRanges+(nPageRange*SIZEOF_PRINTPAGERANGE)
            nFrom = CToBin(Sys(2600,hPageRange,4),"4rs")
            nTo = CToBin(Sys(2600,hPageRange+4,4),"4rs")
            ?"   From: "+Transform(nFrom)+" To: "+Transform(nTo)
         Endfor
      Otherwise
         ?" All Pages"
   Endcase


** Display the DEVMODE structure, showing printing preferences
   If hDevMode <> 0
      ?"DEVMODE:"
** DEVMODE: http://msdn2.microsoft.com/en-us/library/ms535771.aspx
      lcDeviceName = Sys(2600,hDevMode+0,CCHDEVICENAME)
      lcDeviceName = " "+Left(lcDeviceName, At(0h00,lcDeviceName)-1)
      ?" Device Name: ",   lcDeviceName
      ?" Orientation: ",   CToBin(Sys(2600,hDevMode+44,2),"2rs")
      ?" Paper Size: ",    CToBin(Sys(2600,hDevMode+46,2),"2rs")
      ?" Paper Length: ",  CToBin(Sys(2600,hDevMode+48,2),"2rs")
      ?" Paper Width: ",   CToBin(Sys(2600,hDevMode+50,2),"2rs")
      ?" Paper Scale: ",   CToBin(Sys(2600,hDevMode+52,2),"2rs")
      ?" Paper Copies: ",  CToBin(Sys(2600,hDevMode+54,2),"2rs")
      ?" Default Source: ",CToBin(Sys(2600,hDevMode+56,2),"2rs")
      ?" Print Qualilty: ",CToBin(Sys(2600,hDevMode+58,2),"2rs")
      ?" Color: ",         CToBin(Sys(2600,hDevMode+60,2),"2rs")
      ?" Duplex: ",        CToBin(Sys(2600,hDevMode+62,2),"2rs")
      ?" Y Resolution: ",  CToBin(Sys(2600,hDevMode+64,2),"2rs")
      ?" TT Option: ",     CToBin(Sys(2600,hDevMode+66,2),"2rs")
      ?" Collate: ",       CToBin(Sys(2600,hDevMode+68,2),"2rs")
      lcFormName = Sys(2600,hDevMode+70,CCHFORMNAME)
      lcFormName = " "+Left(lcFormName, At(0h00,lcFormName)-1)
      ?" Form Name: ",      lcFormName
      ?" LogPixels: ",      CToBin(Sys(2600,hDevMode+102,2),"2rs")
      ?" BitsPerPixel: ",   CToBin(Sys(2600,hDevMode+104,2),"2rs")
      ?
   Endif

** Unlock movable memory
   GlobalUnlock(hGDevMode)
   GlobalUnlock(hGDevNames)
Else
   ?"ERROR: "+Transform(hResult,"@0")
Endif

** Clean up allocated memory
If hGDevMode <> 0
   GlobalFree(hGDevMode)
Endif
If hGDevNames <> 0
   GlobalFree(hGDevNames)
Endif
GlobalFree(hPageRanges)
GlobalFree(hPRINTDLG)
If hdc <> 0
   DeleteDC(hdc)
Endif
Return

No comments:

Post a Comment