Print Setup Dialog Question (Long Code)
I put the following code in a module in VB5. It uses API to display
the Printer Setup dialog box. In a form I put a call to the sub
PrintDialog as PrintDialog Form1.
I update the printer object to reflect the changes the user makes in
the dialog box.
I can get the dialog box to display just fine. The problem is that
when I call the sub again the dialog doesn't display the settings
chosen the last time. It will display the correct printer, but all
of the settings are the same each time. What I am trying to do is
get the settings to show up as the user chose them the last time. I
found this code on the internet, and don't totally understand how it
works, or why it doesn't. If anyone can give me any help please post
it here.
Thanks
'============CODE============
'======PUT IN VB MODULE======
Type PRINTDLG
lStructSize As Long
hwndOwner As Long
hDevMode As Long
hDevNames As Long
hdc As Long
flags As Long
nFromPage As Integer
nToPage As Integer
nMinPage As Integer
nMaxPage As Integer
nCopies As Integer
hInstance As Long
lCustData As Long
lpfnPrintHook As Long
lpfnSetupHook As Long
lpPrintTemplateName As String
lpSetupTemplateName As String
hPrintTemplate As Long
hSetupTemplate As Long
End Type
Private Const CCHDEVICENAME = 32
Private Const CCHFORMNAME = 32
Type DEVMODE
dmDeviceName As String * CCHDEVICENAME
dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer
dmDriverExtra As Integer
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperLength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmYResolution As Integer
dmTTOption As Integer
dmCollate As Integer
dmFormName As String * CCHFORMNAME
dmUnusedPadding As Integer
dmBitsPerPel As Integer
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
End Type
Type DEVNAMES
wDriverOffset As Integer
wDeviceOffset As Integer
wOutputOffset As Integer
wDefault As Integer
extra As String * 100
End Type
Declare Function PRINTDLG Lib "comdlg32.dll" Alias "PrintDlgA"
(pPrintdlg As PRINTDLG) As Long
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long,
ByVal dwBytes As Long) As Long
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As
Long
Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As
Long
Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As
Long
Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory"
(Destination As Any, Source As Any, ByVal Length As Long)
Public Const GMEM_MOVEABLE = &H2
Public Const GMEM_ZEROINIT = &H40
Public Const DM_DEFAULTSOURCE = &H200&
Public Const DM_ORIENTATION = &H1&
Public Const PD_RETURNDC = &H100
Public Const PD_PRINTSETUP = &H40
Sub PrintDialog(OwnerForm As Form)
' Open a Print Setup common dialog box. Then display certain
selections the user made,
' such as the printer name, paper size, paper source, 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 ' 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_PAPERSIZE Or DM_DEFAULTSOURCE Or
DM_ORIENTATION ' identify which other members have information
printmode.dmPaperSize = Printer.PaperSize
printmode.dmDefaultSource = Printer.PaperBin
printmode.dmOrientation = Printer.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.
printnames.wDriverOffset = 8 ' offset of driver name string
printnames.wDeviceOffset = printnames.wDriverOffset + 1 + Len
(Printer.DriverName) ' offset of printer name string
printnames.wOutputOffset = printnames.wDeviceOffset + 1 + Len
(Printer.Port) ' offset to output port string
printnames.wDefault = 0 ' maybe this isn't the default selected
printer
' Load the three strings into the buffer, separated by null
characters.
printnames.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 = OwnerForm.hWnd ' window Form1 is opening the
Print dialog box
' Flags: Show print setup, return device context:
pd.flags = PD_PRINTSETUP Or PD_RETURNDC
' 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, change printer object settings.
For Each Prt In Printers
If Prt.DeviceName = Left(printmode.dmDeviceName, InStr
(printmode.dmDeviceName, vbNullChar) - 1) Then
Set Printer = Prt
Exit For
End If
Next
Printer.PaperBin = printmode.dmDefaultSource
Printer.PaperSize = printmode.dmPaperSize
Printer.Orientation = printmode.dmOrientation
End If
' No matter what, we have to deallocate the memory blocks from
before.
retval = GlobalFree(pd.hDevMode)
retval = GlobalFree(pd.hDevNames)
End Sub
'==========END CODE==========
where did you find this ?
Can you give me a link to where you found this ....
I can't seem to chnage the printer object from the common dialog.