Search In Site

28 June, 2013

Print Dialog Box in Visual Basic

Using The Print Dialog Box
The Testing Department is calling again. The Print button you’ve placed in your word processor, Super Duper Text Pro, is very nice, but it doesn’t let the user set the number of copies of a document they want to print. You can’t do that with a button, you explain. Right, they say use a Print dialog box.You show the Print dialog box with the Common Dialog control’s ShowPrinter method. If you know your document’s length, you can set the minimum and maximum pages to print in the Common Dialog control’s Min and Max properties; setting these properties enables the From and To page range text boxes in the Print dialog box 
This dialog box does not send data to the printer; instead, it lets the user specify how he wants data printed. Printing is up to you. How do you print? If you’ve set the PrinterDefault property to True, you can use the
Printer object to print data (the user can change the default printer from the Printer dialog box, setting a new default printer in the Windows registry or win.ini, but that new printer automatically becomes the one referred to by the Printer object). 
For example, you can print the picture in a picture box using the Printer object this way: Printer.PaintPicture Picture1.Picture, 0, 0. Otherwise, you must use Windows functions to print to the device represented by the hDC (a device context handle) property. After the user clicks on OK, you can read these properties from the Common Dialog control to determine what printer options they've selected:
" Copies The number of copies to print
" FromPage The page to start printing
" ToPage The page to stop printing
" hDC The device context for the selected printer

Let’s see an example. In this case, we’ll use the Visual Basic PrintForm method to print a copy of the current form as many times as the user specifies. We start by setting the Common Dialog control’s CancelError property to True so we can catch Cancel button clicks as trappable errors:

Private Sub Command1_Click()
On Error GoTo Cancel
...
Cancel:
End Sub

Then we set the PrinterDefault property to True and show the Print dialog box:

Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.PrinterDefault = True
CommonDialog1.ShowPrinter
...
Cancel:
End Sub

All that's left is to loop over the number of copies the user has requested (as returned in the Copies property) and call PrintForm each time: 

Private Sub Command1_Click()
Dim intLoopIndex As Integer
On Error GoTo Cancel
CommonDialog1.PrinterDefault = True
CommonDialog1.ShowPrinter
For intLoopIndex = 1 To CommonDialog1.Copies
PrintForm
Next intLoopIndex
Cancel:
End Sub

That's it when the user clicks Command1, the program displays the Print dialog box; the user can set the number of copies to print and when they click on OK, Visual Basic displays a dialog box with the text Printing & momentarily, and the print job starts. Our Print dialog box example is a success the code for this program is located in the printerdialog folder on this books accompanying CD-ROM.

Setting Print Dialog Flags
You can set a number of options in the Common Dialog control's Flags property when working with the Print dialog box:
" cdlPDAllPages &H0; returns or sets the state of the All Pages option button.
" cdlPDCollate &H10; returns or sets the state of the Collate checkbox.
" cdlPDDisablePrintToFile &H80000; disables the Print To File checkbox.
" cdlPDHelpButton &H800; causes the dialog box to display the Help button.
" cdlPDHidePrintToFile &H100000; hides the Print To File checkbox.
" cdlPDNoPageNums &H8; disables the Pages option button and the associated edit control.
" cdlPDNoSelection &H4; disables the Selection option button.
" cdlPDNoWarning &H80; prevents a warning message from being displayed when there is no default printer.
" cdlPDPageNums &H2; returns or sets the state of the Pages option button.
" cdlPDPrintSetup &H40; causes the system to display the Print Setup dialog box rather than the Print dialog box.
" cdlPDPrintToFile &H20; returns or sets the state of the Print To File checkbox.
" cdlPDReturnDC &H100; returns a device context for the printer selection made in the dialog box. The device context is returned in the dialog box's hDC property.
" cdlPDReturnDefault &H400; returns the default printer name.
" cdlPDReturnIC &H200; returns an information context for the printer selection made in the dialog box. An information context provides a fast way to get information about the device without creating a device context. The information context is returned in the dialog box's hDC property.
" cdlPDSelection &H1; returns or sets the state of the Selection option button. If neither cdlPDPageNums nor cdlPDSelection is specified, the All option button is in the selected state.
" cdlPDUseDevModeCopies_&H40000; if a printer driver doesn't support multiple copies, setting this flag disables the Number Of Copies control in the Print dialog box. If a driver does support multiple copies, setting this flag indicates that the dialog box stores the requested number of copies in the Copies property.
You can set more than one flag for a dialog box using the Or operator. For example:
CommonDialog1.Flags = &H10& Or &H200& :Adding the desired constant values produces the same result.


Setting The Minimum And Maximum Pages To Print
When displaying a Print dialog box, you can set the minimum and maximum allowed values for the print range (in other words, the From and To pages to print) using the Min and Max properties of the Common Dialog control. The Min property sets the smallest number the user can specify in the From text box. The Max property sets the largest number the user can specify in the To text box. For example, here we restrict
the possible pages to print to a maximum of 10, in the range 0 to 9:

Private Sub Command1_Click()

Dim intLoopIndex As Integer
On Error GoTo Cancel
CommonDialog1.PrinterDefault = True
CommonDialog1.Min = 0
CommonDialog1.Max = 9
CommonDialog1.ShowPrinter
For intLoopIndex = 1 To CommonDialog1.Copies
PrintForm
Next intLoopIndex
Cancel:
End Sub

Now when the Print dialog box appears, you can see that in the Print Range box, one option button says All 10 Pages. That is, we've set a maximum total of 10 pages for our document. The actual page range is from 0 to 9.
Setting Page Orientation
When printing, you can set the page orientation portrait (upright) or landscape (sideways)_with the Common Dialog control's Orientation property. This setting is communicated to the printer automatically, but note that not all printers will be able to set a document's orientation.
Here are the possible values for the Orientation property:
" cdlPortrait 1; documents are printed with the top at the narrow side of the paper (the default).
" cdlLandScape 2; documents are printed with the top at the wide side of the paper.

Here's an example. In this case, we're setting the printer's Orientation property to landscape:

Private Sub Command1_Click()
Dim intLoopIndex As Integer
On Error GoTo Cancel
CommonDialog1.PrinterDefault = True
CommonDialog1.Orientation = cdlLandscape
CommonDialog1.ShowPrinter
For intLoopIndex = 1 To CommonDialog1.Copies
PrintForm
Next intLoopIndex
Cancel:
End Sub

0 comments:

Post a Comment

Dear Visitors All The Tricks And Hacks Posted Here Are Only For Knowledge Purpose.Don't Use These for Illegal Operations.

 
Twitter Bird Gadget