Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1


|Views: 94|Likes:
Published by api-3715867

More info:

Published by: api-3715867 on Oct 14, 2008
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less






Font is another very important GDI object, every application deals with font. Usually a system contains some default fonts that can be used by all the applications. Besides these default fonts, we can also install fonts provided by the thirty party. For word processing applications, using font is a complex issue. There are many things we need to take care. For example, when creating this type of applications, we need to think about the following issues: how to display a font with different styles; how to change the text alignment; how to add special effects to characters.

9.1 Outputting Text Using Different Fonts
When we implement a font dialog box, all the available fonts contained in the system will
be listed in it. We can select font size, font name, special styles, and text color.

Like other GDI objects such as pen and brush, we need to create font with specific styles and select it into a DC in order to use it for outputting text. In MFC, the class that can be used to implement font is CFont. To create a font, we can call either CFont::CreateFont(...) or CFont::CreateFontIndirect(...), whose formats are listed as follows:

(Code omitted)

The first function has many parameters and the second one needs only a LOGFONT type pointer. The results of the two member functions are exactly the same, every style we need to specify for a font in the first function has a corresponding member in structure

typedef struct tagLOGFONT {
LONG lfHeight;
LONG lfWidth;
LONG lfEscapement;
LONG lfOrientation;
LONG lfWeight;
BYTE lfItalic;
BYTE lfUnderline;
BYTE lfStrikeOut;
BYTE lfCharSet;

BYTE lfOutPrecision;
BYTE lfClipPrecision;
BYTE lfQuality;
BYTE lfPitchAndFamily;

Here, member lfFaceName specifies the font name; lfHeight and lfWidth specify font size; lfWeight, lfItalic, lfUnderline and lfStrikeOut specify font styles. Besides these styles, there are two other styles that can be specified: lfEscapement and lfOrientation.

Under Windows 95, lfEscapement and lfOrientation must be assigned the same value when a font is being created. If they are non-zero, the text will have an angle with respect to the horizontal border of the window when it is displayed (Figure 9-1). To display text this way, we must assign the angle to both lfEscapement and lfOrientation when creating the font, the unit of the angle is one tenth of a degree. Please note that only True Type fonts can have such orientation.

After a font is created, it can be selected into DC for outputting text. After the text output is over, it must be selected out of the DC. This procedure is exactly the same with other GDI objects.

Sample 9.1\GDI demonstrates how to create and use a font with specified styles. It is a standard SDI application generated by Application Wizard. In the sample, the user can choose any available font in the system and set its styles (bold, italic, underline, etc). The face name of the font will be displayed in the client window using the selected font, and the user can also set the escapement of the font.

Two variables are added to class CGDIDoc: CGDIDoc::m_fontDraw and CGDIDoc::m_colorFont. The first variable is declared as a CFont type variable, it will be used to create the font. The second variable is declared as a COLORREF type variable, it will be used to store the color of the text.

Besides font color, we also need to consider the background color of the text. A text can be displayed with either a transparent or opaque background. In the latter case, we can set the background to different colors. In order to display text in different styles, another Boolean type variable CGDIDoc:: m_bTransparentBgd is declared, it will be used to indicate if the background is transparent or opaque.

The following is the modified class CGDIDoc:
(Code omitted)

Besides the three new member variables, there are also three new member functions added to the class. These functions allow the information stored in CGDIDoc to be accessible


CGDIDoc::GetFontColor() and CGDIDoc::GetBgdStyle().
The above variables are initialized in the constructor of class CGDIDoc:
(Code omitted)

When a DC is created, it selects the default font, pen, brush and other GDI objects. So here we create a DC that does not belong to any window, and call function CDC::GetCurrentFont() to obtain its currently selected font (which is the default font). Then function CFont::GetLogFont(...) is called to retrieve the font information, which is stored in a LOGFONT type object. With this object, we can create a system default font by calling function CFont::CreateFontIndirect(...). By default, the font color is set to black and the text background mode is set to transparent.

We need to provide a way of letting user modify the font styles. This can be easily implemented by using a font common dialog box. In the sample, two commands are added to the application: Font | Select and Font | Escapement and Orientation, whose IDs are ID_FONT_SELECT and ID_FONT_STYLE respectively. Also, message handlers are added

Class Wizard,
CGDIDoc::OnFontStyle() and CGDIDoc::OnFontSelect().
Function CGDIDoc::OnFontSelect() lets the user select a font, set its styles, and specify
the text color. It is impelemented as follows:
(Code omitted)

A font common dialog is implemented to let the user pick up a font. If a font is selected, function CFontDialog::GetCurrentFont(...) is called to retrieve the information of the font, which is stored in a LOGFONT type object. Because member m_fontDraw is already initialized, we need to delete the old font before creating a new one. The font is created by calling function CFont::CreateFontIndirect(...). After this the color of the font is retrieved by calling function CFontDialog::GetColor(), and stored in the variable CGDIDoc::m_colorFont. Finally function CDocument::UpdateAllViews(...) is called to update the client window of the application.

Since font common dialog box does not contain escapement and orientation choices, we have to implement an extra dialog box to let the user set them. In the sample, dialog template IDD_DIALOG_STYLE is added for this purpose. Within this template, besides the default "OK" and "Cancel" buttons, there are two other controls included in the dialog box: edit box IDC_EDIT_ESP, which allows the user to set escapement angle; check box IDC_CHECK, which allows the user to select text background style (transparent or opaque). A new class CStyleDlg is added for this dialog template, within which two variables m_lEsp (long type) and m_bBgdStyle (Boolean type) are declared. Both of them are added through using Class Wizard, and are associated with controls IDC_EDIT_ESP and IDC_CHECK respectively.

Activity (2)

You've already reviewed this. Edit your review.
1 hundred reads
Mahendra Nayak liked this

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->