Search In Site

27 June, 2013

How To Use Font Dialog Box in Visual Basic

Using A Font Dialog Box
The Testing Department is calling again. Your new word processor, SuperDuperTextPro, is great, but why can't the users select the font they want to use?
You ask, should they be able to do that? The Testing Department says, take a look at the Font dialog box. You use the Common Dialog control's ShowFont method to show a Font dialog box.
Note that before you use the ShowFont method, you must set the Flags property of the Common Dialog control to one of three constants to indicate if you want to display screen fonts, printer fonts, or both. The possible values are as follows:
" cdlCFScreenFonts_&H1; show screen fonts
" cdlCFPrinterFonts_&H2; show printer fonts
" cdlCFBoth_&H3; show both types of fonts

If you don't set one of these in the Flags property, a message box is displayed
advising the user that There are no fonts installed, which will probably cause them to panic. When the user dismisses the Font dialog box by clicking on OK, you can determine their font selections using these properties of the Common Dialog control:
" Color The selected color. To use this property, you must first set the Flags property
to cdlCFEffects.
" FontBold_True if bold was selected.
" FontItalic_True if italic was selected.
" FontStrikethru_True if strikethru was selected. To use this property, you must first set the Flags property to cdlCFEffects.
" FontUnderline_True if underline was selected. To use this property, you must first set the Flags property to cdlCFEffects.
" FontName_The selected font name.
" FontSize_The selected font size.
Let's see an example. Here, we'll let the user set the font, font size, and font styles (like underline and bold) in a text box. We start by setting the Common Dialog control's CancelError property to True so clicking the Cancel button causes a trappable error:

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

Next, we set the Flags property and show the Font dialog box:

Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.Flags = cdlCFBoth Or cdlCFEffects
CommonDialog1.ShowFont
...
Cancel:
End Sub

Finally, we set the text box's properties to match what the user set in the Font dialog box:

Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.Flags = cdlCFBoth Or cdlCFEffects
CommonDialog1.ShowFont
Text1.FontName = CommonDialog1.FontName
Text1.FontBold = CommonDialog1.FontBold
Text1.FontItalic = CommonDialog1.FontItalic
Text1.FontUnderline = CommonDialog1.FontUnderline
Text1.FontSize = CommonDialog1.FontSize
Text1.FontName = CommonDialog1.FontName
Cancel:
End Sub


That's it now we're using Font dialog boxes.The listing for this program,
fontdialog.frm,is located in the fontdialog folder on this book's accompanying CD-ROM.

Setting Font Dialog Flags
You can set a wide variety of options when using Font dialog boxes by using the Common Dialog control's Flags property. Here are the possible values to use with that property:

" cdlCFANSIOnly_&H400; specifies that the dialog box allows only a selection of the fonts that use the Windows character set. If this flag is set, the user won't be able to select a font that contains only symbols.

" cdlCFApply_&H200; enables the Apply button on the dialog box.
" cdlCFBoth_&H3; causes the dialog box to list the available printer and screen fonts. The hDC property identifies the device context associated with the printer.
" cdlCFEffects_&H100; specifies that the dialog box enables strikethru, underline, and color effects.
" cdlCFFixedPitchOnly_&H4000; specifies that the dialog box selects only
fixed-pitch fonts.
" cdlCFForceFontExist_&H10000; specifies that an error message box is displayed if the user attempts to select a font or style that doesn't exist.
" cdlCFHelpButton_&H4; causes the dialog box to display a Help button.
" cdlCFLimitSize_&H2000; specifies that the dialog box selects only font sizes
within the range specified by the Min and Max properties.
" cdlCFNoFaceSel_&H80000; no font name was selected.
" cdlCFNoSimulations_&H1000; specifies that the dialog box doesn't allow graphic device interface (GDI) font simulations.
" cdlCFNoSizeSel_&H200000; no font size was selected.
" cdlCFNoStyleSel_&H100000; no style was selected.
" cdlCFNoVectorFonts_&H800; specifies that the dialog box doesn't allow
vector-font selections.
" cdlCFPrinterFonts_&H2; causes the dialog box to list only the fonts supported by the printer, specified by the hDC property.
" cdlCFScalableOnly_&H20000; specifies that the dialog box allows only the
selection of fonts that can be scaled.
" cdlCFScreenFonts_&H1; causes the dialog box to list only the screen fonts
supported by the system.
" cdlCFTTOnly_&H40000; specifies that the dialog box allows only the selection of TrueType fonts.
" cdlCFWYSIWYG_&H8000; specifies that the dialog box allows only the selection of fonts that are available on both the printer and on screen. If this flag is set, the cdlCFBoth and cdlCFScalableOnly flags should also be set.


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 Max And Min Font Sizes
The Testing Department is calling again. Now users are setting the font size in your program, SuperDuperTextPro, to 3 points_and then complaining they can't read what they've typed. Can you limit the allowed font range?
Yes, you can, using the Common Dialog control's 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 control's Flags property. Then you're free to restrict the possible range of font sizes.

Here's an example. We set the Common Dialog's 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

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