Declare Function PrintDlg Lib "comdlg32.dll" Alias "PrintDlgA" (pPrintdlg As PRINTDLG_TYPE) As Long
Platforms: Win 32s, Win 95/98, Win NT
PrintDlg displays either the Print common dialog box or the Print Setup dialog box. Either box can be used to allow the user to select a printer and other settings, such as the number of copies and the page range, desired for a print operation. Information for initializing the dialog box as well as information returned from it is stored in the structure passed as pPrintdlg. See the pages for the PRINTDLG_TYPE, DEVMODE, and DEVNAMES structures for more details about using this function. Note that instead of using the latter two structures explicitly, handles to the memory blocks holding their data are required by the function; see the example below. The function returns 0 if either an error occured or the user pressed Cancel, or a non-zero value if the user successfully pressed OK.
Example:
' Open a Print common dialog box. Then display certain selections the user made,
' such as the printer name, number of copies, and orientation. Carefully note how memory
' blocks are allocated to hold the two data structures containing information about the
' printer device. To save space, Visual Basic's Printer object, referring to the default
' printer, is used to provide the defaults. Of course, API functions could also be used
' to get these defaults.
Dim pd As PRINTDLG_TYPE ' holds information to make the dialog box
Dim printmode As DEVMODE ' holds settings for the printer device
Dim printnames As DEVNAMES ' holds device, driver, and port names
Dim pMode As Long, pNames As Long ' pointers to the memory blocks for the two DEV* structures
Dim retval As Long ' return value of function
' First, load default settings into printmode. Note that we only fill relevant information.
printmode.dmDeviceName = Printer.DeviceName ' name of the printer
printmode.dmSize = Len(printmode) ' size of the data structure
printmode.dmFields = DM_ORIENTATION ' identify which other members have information
printmode.dmOrientation = DMORIENT_PORTRAIT ' default to Portrait orientation
' Next, load strings for default printer into printnames. Note the unusual way in which such
' information is stored. This is explained on the DEVNAMES page.
devnames.wDriverOffset = 8 ' offset of driver name string
devnames.wDeviceOffset = devnames.wDriverOffset + 1 + Len(Printer.DriverName) ' offset of printer name string
devnames.wOutputOffset = devnames.wDeviceOffset + 1 + Len(Printer.Port) ' offset to output port string
devnames.wDefault = 0 ' maybe this isn't the default selected printer
' Load the three strings into the buffer, separated by null characters.
devnames.extra = Printer.DriverName & vbNullChar & Printer.DeviceName & vbNullChar & Printer.Port & vbNullChar
' Finally, load the initialization settings into pd, which is passed to the function. We'll
' set the pointers to the structures after this.
pd.lStructSize = Len(pd) ' size of structure
pd.hwndOwner = Form1.hWnd ' window Form1 is opening the Print dialog box
' Flags: All Pages default, disable Print to File option, return device context:
pd.flags = PD_ALLPAGES Or PD_DISABLEPRINTTOFILE Or PD_RETURNDC
pd.nMinPage = 1 ' allow user to select first page of "document"
pd.nMaxPage = 15 ' let's say there are 15 pages of the "document"
' Note how we can ignore those members which will be set or are not used here.
' Copy the data in printmode and printnames into the memory blocks we allocate.
pd.hDevMode = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(printmode) ' allocate memory block
pMode = GlobalLock(pd.hDevMode) ' get a pointer to the block
CopyMemory ByVal pMode, printmode, Len(printmode) ' copy structure to memory block
retval = GlobalUnlock(pd.hDevMode) ' unlock the block
' Now do the same for printnames.
pd.hDevNames = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(printnames) ' allocate memory block
pNames = GlobalLock(pd.hDevNames) ' get a pointer to the block
CopyMemory ByVal pNames, printnames, Len(printnames) ' copy structure to memory block
retval = GlobalUnlock(pd.hDevNames) ' unlock the block
' Finally, open the dialog box!
retval = PrintDlg(pd) ' looks so simple, doesn't it?
' If the user hit OK, display some information about the selection.
If retval <> 0 Then
' First, we must copy the memory block data back into the structures. This is almost identical
' to the code above where we did the reverse. Comments here are omitted for brevity.
pMode = GlobalLock(pd.hDevMode)
CopyMemory printmode, ByVal pMode, Len(printmode)
retval = GlobalUnlock(pd.hDevMode)
pNames = GlobalLock(pd.hDevNames)
CopyMemory printnames, ByVal pNames, Len(printnames)
retval = GlobalUnlock(pd.hDevNames)
' Now, display the information we want. We could instead use this info to print something.
Debug.Print "Printer Name: "; printmode.dmDeviceName
Debug.Print "Number of Copies:"; pd.nCopies
Debug.Print "Orientation: ";
If printmode.dmOrientation = DMORIENT_PORTRAIT Then Debug.Print "Portrait" Else Debug.Print "Landscape"
If (pd.flags And PD_SELECTION) = PD_SELECTION Then ' user chose "Selection"
Debug.Print "Print the current selection."
Elseif (pd.flags And PD_PAGENUMS) = PD_PAGENUMS Then ' user chose a page range
Debug.Print "Print pages"; pd.nFromPage; "to"; pd.nToPage
Else ' only one left is "All"
Debug.Print "Print all pages."
End If
End If
' No matter what, we have to deallocate the memory blocks from before.
retval = GlobalFree(pd.hDevMode)
retval = GlobalFree(pd.hDevNames)
Category: Common Dialog
Go back to the alphabetical Function listing.
Go back to the Reference section index.
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information.
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/p/printdlg.html