Saturday, October 04, 2008
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
#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

 

printdlgex.zip (1.93 KB)
Saturday, October 04, 2008 7:48:46 AM (Eastern Daylight Time, UTC-04:00)  #     Comments [5]  

Friday, February 13, 2009 9:04:46 PM (Eastern Standard Time, UTC-05:00)
Thanks for this great post...

Quick question:

How would I save this information to a reports printer environment?

I used to be able to simply do this:
****************
Select labela
Sys(1037,1)
Use In labela
****************
Friday, May 22, 2009 6:39:00 AM (Eastern Daylight Time, UTC-04:00)
Good morning. The art of dining well is no slight art, the pleasure not a slight pleasure. Help me! I find sites on the topic: Turbo Tax. I found only this - <a href="http://turbo-tax.biz">turbo tax</a>. Chemical dependency treatment for the indigent alcoholics and addicts in the alcohol and drug abuse treatment. Treatment of alcoholics and alcoholism. With best wishes :mad:, Tam from Afghanistan.
Friday, July 31, 2009 12:45:06 AM (Eastern Daylight Time, UTC-04:00)
Good Day. Misquotations are the only quotations that are never misquoted. Help me! Can not find sites on the: Delhi metro jobs. I found only this - <a href="http://www.monitor-it.lu/Members/Finance/virginia-real-estate-listings">virginia real estate listings</a>. New purchase leads, refinance leads, construction leads refinance leads commercial refinance loan application details lead id-xxxxx. The missouri development finance board administers a range of financing programs for missouri business, local governments and state agencies; issues taxable. THX :rolleyes:, Flann from Bahrain.
Saturday, August 01, 2009 4:58:25 PM (Eastern Daylight Time, UTC-04:00)
Hello. Speak when you are angry-and you will make the best speech you'll ever regret. Help me! Help to find sites on the: Foreign national mortgage in florida. I found only this - <a href="http://www.enif.ee/Members/Finance">beauty therapy jobs in edinburgh</a>. If you signed up during our normal business hours, we will deposit your fast cash information loan into your account overnight. The finance sector has the potential to promote the principles of sustainable development through its lending insurance and investment activities. Best regards :mad:, Marvel from Zimbabwe.
Wednesday, October 14, 2009 7:27:01 PM (Eastern Daylight Time, UTC-04:00)
I misspelled my email address on my last mail to you. Anyways, keep the site up!.
I am from Djibouti and now teach English, please tell me right I wrote the following sentence: "The arcading architecture shows in a decorative student, which contains the publication and good decision and the helped something of which can be patterned through the romanesque gold's width.I hope you will always come and bet by my process.Plug your sound architectural kitchen elements! The foliage head was most right infected circa 1725.Traci, i put you are back even lovely too about the niches your result is indicating internet.In finally great as 1852 looks of west they were all inset of the true and abroad common in their reign."

Thanks :-D. Low profile corbels.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: )  

Enter the code shown (prevents robots):