Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


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. To see more possible settings for the Flags property, take a look at the next topic in this chapter.

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

Now when you run this program and click the button, the Font dialog box appears, as in Figure 11.9.


Figure 11.9  The Font dialog box.

When you select the font options you want and click on OK, those options are installed in the text box, Text1, as shown in Figure 11.10.


Figure 11.10  Setting fonts and font styles with the Font dialog box.

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.


Previous Table of Contents Next


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.