Give me please an example of using PrintDlg in VB6. I need to use this dialog just to select printer (I think it's better to use standard dialog). I can't find anything on searching engines. I haven't even found the LPPRINTDLG type description (for C I've found but I'm not sure about types correspondence).
You can use the print dialog to allow the user to select printer settings, which your application can then use to print. Below are the most common parameters you will need to set before displaying the Print Dialog Box.
Parameter
DialogTitle -- the Title displayed in the Dialog Box
Printer Default -- whether to use the default printer
Flags -- custom settings such as print selection only
CancelError -- whether an error occurs when the dialog box is cancelled.
The following code displays a print dialog, with page numbers disabled, and then prints the text in RichTextBox1. It also returns the selected printer
VB Code:
CommonDialog1.PrinterDefault = True
CommonDialog1.CancelError = True
' Set flags - no page numbers, return the selected printer
Put a commondialog control on your form, call it dlgPrinter and use its .ShowPrinter function to select a printer and the VB Printer object to print, i.e.
----------------
on error goto eh_print
with dlgPrinter
.ShowPrinter ' if user selects printer, the printer device will be set
Printer.Orientation = .Orientation ' if user selected an orientation
end with
exit sub
eh_print:
' maybe user canceled from dialog
----------------
Then later use Printer.Print "Print this text"
It doesn't solute a problem because project structure implyes using it in separated module. Using
Dim CommonDialog1 As New CommonDialog
doesn't work too.
Assuming you have a form, you have to right-click on the components tab and add the control from list of components. Then drag it on the form. It doesn't show up anywhere, but you can name it in the properties window and use it in the code as people have suggested.
If you're going formless, then you might have to do some trick to add a form that does not really show but from which you run some code.
Thank you, it almost works but one question left: a print method of data component requires not only device name but also driver name. As written in MSDN I can't find a property of Printer object that includes driver name. So tell me please how to get driver name of the selected by this dialog printer.
I've attached a BAS file which should have everything you need.
Also, here is some code I pulled from one of my projects:
Code:
Public Declare Function PrtDialog Lib "comdlg32.dll" Alias "PrintDlgA" (pPrintdlg As PRINTDLG) As Long
Public Declare Function GlobalLock Lib "KERNEL32" (ByVal hMem As Long) As Long
Public Declare Function GlobalUnlock Lib "KERNEL32" (ByVal hMem As Long) As Long
Public Declare Function GlobalAlloc Lib "KERNEL32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Public Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
' Window API PRINTER information type
Public Type DEVMODE
dmDeviceName As String * 32 ' Specifies the the "friendly" name of the printer (ie - "PCL/HP LaserJet"). This string is unique among device drivers. Note that this name may be truncated to fit in the dmDeviceName array
dmSpecVersion As Integer ' Specifies the version number of the initialization data specification on which the structure is based
dmDriverVersion As Integer ' Specifies the printer driver version number assigned by the printer driver developer
dmSize As Integer ' Specifies the size, in bytes, of the DEVMODE structure, not including any private driver-specific data that might follow the structure’s public members. You can use this member to determine the number of bytes of public data regardless of the version of the DEVMODE structure being used
dmDriverExtra As Integer ' Contains the number of bytes of private driver-data that follow this structure. If a device driver does not use device-specific information, set this member to zero
dmFields As Long ' Sets which members of the type are active. A set of bit flags that specify whether certain members of the DEVMODE structure have been initialized. If a field is initialized, its corresponding bit flag is set, otherwise the bit flag is clear. A printer driver supports only those DEVMODE structure members that are appropriate for the printer technology (See Constants)
dmOrientation As Integer ' Selects the orientation of the paper - This can be either DMORIENT_PORTRAIT (1) or DMORIENT_LANDSCAPE (2)
dmPaperSize As Integer ' Selects the size of the paper to print on. This member can be set to zero if the length and width of the paper are both set by the dmPaperLength and dmPaperWidth members (See Constants)
dmPaperLength As Integer ' Selects the size of the paper to print on. This member can be set to zero if the length and width of the paper are both set by the dmPaperLength and dmPaperWidth members (See Constants)
dmPaperWidth As Integer ' Overrides the width of the paper specified by the dmPaperSize member
dmScale As Integer ' Specifies the factor by which the printed output is to be scaled. The apparent page size is scaled from the physical page size by a factor of dmScale/100. For example, a letter-sized page with a dmScale value of 50 would contain as much data as a page of 17- by 22-inches because the output text and graphics would be half their original height and width
dmCopies As Integer ' Selects the number of copies printed if the device supports multiple-page copies
dmDefaultSource As Integer ' Reserved - must be zero
dmPrintQuality As Integer ' Specifies the printer resolution (See Constants). If a positive value is given, it specifies the number of dots per inch (DPI) and is therefore device dependent
dmColor As Integer ' Switches between color and monochrome on color printers (See Constants)
dmDuplex As Integer ' Selects duplex or double-sided printing for printers capable of duplex printing (See Constants)
dmYResolution As Integer ' Specifies the Y-resolution, in dots per inch, of the printer. If the printer initializes this member, the dmPrintQuality member specifies the X-resolution, in dots per inch, of the printer
dmTTOption As Integer ' Specifies how TrueType® fonts should be printed (See Constants)
'dmUnusedPadding As Long ' ** Used to align the structure to a DWORD boundary. This should not be used or referenced. Its name and usage is reserved, and can change in future releases
dmCollate As Integer ' [ TRUE (1) / FALSE (0 ] Specifies whether collation should be used when printing multiple copies. (See Constants) (This member is ignored unless the printer driver indicates support for collation by setting the dmFields member to DM_COLLATE)
dmFormName As String * 32 ' Windows NT: Specifies the name of the form to use; for example, "Letter" or "Legal". A complete set of names can be retrieved by using the EnumForms function. Windows 95: Printer drivers do not use this member
' Not used by Printer Drivers
dmUnusedPadding As Integer ' Specifies the number of pixels per logical inch. Printer drivers do not use this member
dmBitsPerPel As Integer ' Specifies the color resolution, in bits per pixel, of the display device (for example: 4 bits for 16 colors, 8 bits for 256 colors, or 16 bits for 65536 colors). Display drivers use this member, for example, in the ChangeDisplaySettings function. Printer drivers do not use this member
dmPelsWidth As Long ' Specifies the width, in pixels, of the visible device surface. Display drivers use this member, for example, in the ChangeDisplaySettings function. Printer drivers do not use this member.
dmPelsHeight As Long ' Specifies the height, in pixels, of the visible device surface. Display drivers use this member, for example, in the ChangeDisplaySettings function. Printer drivers do not use this member.
dmDisplayFlags As Long ' Specifies the device’s display mode (See Constants)
dmDisplayFrequency As Long ' Specifies the frequency, in hertz (cycles per second), of the display device in a particular mode. This value is also known as the display device’s vertical refresh rate. Display drivers use this member. It is used, for example, in the ChangeDisplaySettings function. Printer drivers do not use this member
' Windows 95 Only
dmICMMethod As Long ' Specifies how ICM is handled. For a non-ICM application, this member determines if ICM is enabled or disabled. For ICM applications, Windows examines this member to determine how to handle ICM support. This member can be one of the constant values, or a driver-defined value greater than the value of DMICMMETHOD_USER (See Constants)
dmICMIntent As Long ' Specifies which of the three possible color matching methods, or intents, should be used by default. This member is primarily for non-ICM applications. ICM applications can establish intents by using the ICM functions. This member can be one of the constant values, or a driver defined value greater than the value of DMICM_USER (See Constants)
dmMediaType As Long ' Specifies the type of media being printed on. The member can be one of the constant values, or a driver-defined value greater than the value of DMMEDIA_USER (See Constants)
dmDitherType As Long ' Specifies how dithering is to be done. The member can be one of the constant values, or a driver-defined value greater than the value of DMDITHER_USER (See Constants)
dmReserved1 As Long ' Windows 95: Not used - must be zero. Windows NT: This member is not supported on Windows NT.
dmReserved2 As Long ' Windows 95: Not used - must be zero. Windows NT: This member is not supported on Windows NT.
End Type
Public Type DEVNAMES
wDriverOffset As Integer
wDeviceOffset As Integer
wOutputOffset As Integer
wDefault As Integer
Extra As String * 100
End Type
selectedPrinterName = "Adobe PDF"
For Each tmpPrinter In Printers
If tmpPrinter.DeviceName = selectedPrinterName Then
Exit For
End If
Next tmpPrinter
With whDevMode
.dmSize = Len(whDevMode)
.dmFields = DM_ORIENTATION Or DM_DUPLEX Or DM_PAPERSIZE Or DM_COLOR Or DM_SCALE
.dmDeviceName = tmpPrinter.DeviceName
.dmOrientation = DMORIENT_LANDSCAPE
.dmCopies = 1
.dmPaperSize = DMPAPER_TABLOID
' .dmColor = DMCOLOR_MONOCHROME 'sets printer to grayscale, not image to bw
End With
'Allocate memory for the initialization hDevMode structure
'and copy the settings gathered above into this memory
pd.hDevMode = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(whDevMode))
lpDevMode = GlobalLock(pd.hDevMode)
If lpDevMode > 0 Then
CopyMemory ByVal lpDevMode, whDevMode, Len(whDevMode)
bReturn = GlobalUnlock(pd.hDevMode)
End If
'Set the current driver, device, and port name strings
With whDevNames
.wDriverOffset = 8
.wDeviceOffset = .wDriverOffset + 1 + Len(tmpPrinter.DriverName)
.wOutputOffset = .wDeviceOffset + 1 + Len(tmpPrinter.Port)
.wDefault = 0
.Extra = tmpPrinter.DriverName & Chr(0) & tmpPrinter.DeviceName & Chr(0) & tmpPrinter.Port & Chr(0)
End With
'Allocate memory for the initial hDevName structure
'and copy the settings gathered above into this memory
pd.hDevNames = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(whDevNames))
lpDevName = GlobalLock(pd.hDevNames)
If lpDevName > 0 Then
CopyMemory ByVal lpDevName, whDevNames, Len(whDevNames)
bReturn = GlobalUnlock(lpDevName)
End If