|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Setting Max And Min Font SizesThe Testing Department is calling again. Now users are setting the font size in your program, SuperDuperTextPro, to 3 pointsand then complaining they cant read what theyve typed. Can you limit the allowed font range? Yes, you can, using the Common Dialog controls Min and Max properties. When you want to make these properties active with a Font dialog box, you must first add the cdlCFLimitSize flag to the Common Dialog controls Flags property. Then youre free to restrict the possible range of font sizes. Heres an example. We set the Common Dialogs CancelError property to True to catch Cancel button clicks, then set the Flags property of the Common Dialog control to display both screen fonts and printer fonts, and set the cdlCFLimitSize flag:
Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.Flags = cdlCFBoth Or cdlCFLimitSize
...
Then we set the minimum and maximum font sizes we want to allow, measured in points:
Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.Flags = cdlCFBoth Or cdlCFLimitSize
CommonDialog1.Min = 12
CommonDialog1.Max = 24
...
Finally, we show the Font dialog box, and then make use of the newly set font size:
Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.Flags = cdlCFBoth Or cdlCFLimitSize
CommonDialog1.Min = 12
CommonDialog1.Max = 24
CommonDialog1.ShowFont
Text1.FontName = CommonDialog1.FontSize
Cancel:
End Sub
Thats all we needthe result of this code appears in Figure 11.11, where, as you can see, weve restricted the range of font sizes from 12 to 24 points in the Font dialog box.
Using The Print Dialog BoxThe Testing Department is calling again. The Print button youve placed in your word processor, SuperDuperTextPro, is very nice, but it doesnt let the user set the number of copies of a document they want to print. You cant do that with a button, you explain. Right, they sayuse a Print dialog box. You show the Print dialog box with the Common Dialog controls ShowPrinter method. If you know your documents length, you can set the minimum and maximum pages to print in the Common Dialog controls Min and Max properties; setting these properties enables the From and To page range text boxes in the Print dialog box (see Setting The Minimum And Maximum Pages To Print later in this chapter). You can also set the Common Dialog controls Flags property to select various options heresee the next topic in this chapter. 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 youve 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 theyve selected:
Lets see an example. In this case, well 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 controls 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 thats 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
Thats itwhen 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 successthe code for this program is located in the printerdialog folder on this books accompanying CD-ROM.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |