📄 ch13.htm
字号:
match logical font attributes to the physical representation of the font. Here, again,
there are three options:
<UL>
<LI><TT>DEFAULT_QUALITY</TT>: Appearance doesn't matter; Windows is free to provide
a "reasonable" font. This is a commonly selected option and is equivalent
to using zero as a parameter.<BR>
<BR>
<LI><TT>DRAFT_QUALITY</TT>: Fast output is given higher priority than print quality.
Some effects, such as strikethrough, bold, italic, and underlined characters, are
synthesized by GDI routines if necessary.<BR>
<BR>
<LI><TT>PROOF_QUALITY</TT>: The output quality is given higher priority than output
speed. The quality of the font is more important than exact matching of the logical-font
attributes. Some effects, such as strikethrough, bold, italics, and underlined characters,
are synthesized by GDI routines if necessary.
</UL>
<H3><FONT COLOR="#000077"><B>Font Pitch and Family Attributes</B></FONT></H3>
<P>All fonts have a certain pitch. When requesting a font from Windows, you have
three different choices for the pitch:
<UL>
<LI><TT>DEFAULT_PITCH</TT>: Windows selects a reasonable font, based on other specified
attributes.
<LI><TT>FIXED_PITCH</TT>: The font created by Windows must have a fixed pitch.
<LI><TT>VARIABLE_PITCH</TT>: The font is specified to have a variable pitch.
</UL>
<P>As was discussed earlier, the font family describes general characteristics for
a type of font and can be used when a specific font might not be available on all
machines. Here are the values for font families:
<UL>
<LI><TT>FF_DECORATIVE</TT>
<LI><TT>FF_DONTCARE</TT>
<LI><TT>FF_MODERN</TT>
<LI><TT>FF_ROMAN</TT>
<LI><TT>FF_SCRIPT</TT>
<LI><TT>FF_SWISS</TT>
</UL>
<P>The pitch attribute can be combined with a font family attribute using the bitwise
<TT>OR</TT> operator, like this:</P>
<PRE><FONT COLOR="#0066FF"><TT>lfHeading.lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;</TT>
</FONT></PRE>
<BLOCKQUOTE>
<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>CAUTION:</B></FONT><B> </B>Combining the pitch and
family attributes isn't necessary; often, the family name implies a pitch. In the
preceding example, it's possible to specify just <TT>FF_SWISS</TT>.</P>
<P><TT>FF_ROMAN</TT> and <TT>FF_SWISS</TT> always imply a variable pitch. <TT>FF_MODERN</TT>
always implies a fixed pitch. Other family types contain fonts that have both fixed
and variable pitch.
<HR>
</BLOCKQUOTE>
<H3><FONT COLOR="#000077"><B>Font Weights</B></FONT></H3>
<P>You can specify the relative weight of a font, based on a scale from 0 to 1,000.
A weight of 400 describes a normal font, whereas 700 is used for a bold font. If
you use 0, Windows uses a reasonable default weight for the font. Each of the weight
options between 0 and 900 has a symbolic name, as shown in Table 13.1.
<H4><FONT COLOR="#000077">Table 13.1. Symbolic names for font weights.</FONT></H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><B>Symbol</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Weight</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>FW_DONTCARE</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">0</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>FW_THIN</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">100</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>FW_EXTRALIGHT</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">200</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>FW_ULTRALIGHT</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">200</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>FW_LIGHT</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">300</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>FW_NORMAL</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">400</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>FW_REGULAR</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">400</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>FW_MEDIUM</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">500</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>FW_SEMIBOLD</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">600</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>FW_DEMIBOLD</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">600</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>FW_BOLD</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">700</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>FW_EXTRABOLD</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">800</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>FW_ULTRABOLD</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">800</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>FW_BLACK</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">900</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>FW_HEAVY</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">900</TD>
</TR>
</TABLE>
</P>
<P>Although not every weight is available for every font, Windows tries to select
a font weight close to the requested value.
<H3><FONT COLOR="#000077"><B>Other Font Attributes</B></FONT></H3>
<P>It's possible to define the escapement and orientation of a font. The <I>escapement</I>
is the angle, in tenths of a degree, formed by a line of text in relation to the
bottom of the page. Each degree in escapement adds 10 to the parameter value. For
example, an escapement parameter value of 900 (90deg. x 10) describes a font where
each line of text is rotated 90 degrees counterclockwise. The <I>orientation</I>
of a font is similar to the escapement, but applies to each character rather than
to an entire line of text.</P>
<P>Italic, underline, and strikethrough effects are assigned by specifying <TT>TRUE</TT>
or <TT>FALSE</TT> for each of these attributes.</P>
<P>Finally, you can specify the typeface name. This is the name of a font that should
be a good match for the parameters specified in other parts of the font description.
If this parameter is set to <TT>NULL</TT>, Windows uses the other parameters when
searching for a font. If you specify a name, that name is used to search for a font.
If a font with that name is found, it is used.
<H2><FONT COLOR="#000077"><B>Creating Fonts for Windows Programs</B></FONT></H2>
<P>There are two ways to create fonts using MFC. If you are creating a small number
of fonts, you can use the <TT>CFont</TT> class and its <TT>CreateFont</TT> member
function. If you're creating several similar fonts or a large number of fonts, you
can use the <TT>LOGFONT</TT> structure.
<H3><FONT COLOR="#000077"><B>Creating a Font Using <TT>CFont</TT></B></FONT></H3>
<BLOCKQUOTE>
<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Time Saver:</B></FONT><B> </B>The first time you
consider creating a <TT>CFont</TT> object, you might be intimidated by the large
number of parameters it takes. Don't worry; most of the parameters can actually be
set to default values or zero, and the Windows font mapper selects a font for you.
<HR>
</BLOCKQUOTE>
<P>To illustrate this, Listing 13.1 creates two fonts. One font, <TT>fntArial</TT>,
uses zero for all the parameters and specifies a font name. The other font, <TT>fntBoldSwiss</TT>,
specifies many of the characteristics of a desired font. In both cases the font mapper
determines a reasonable font. Add the source code from Listing 13.1 to the <TT>CDCTestView::OnDraw</TT>
function in the DCTest project that was originally created in Hour 11, "Device
Contexts."
<H4><FONT COLOR="#000077">TYPE: Listing 13.1. Two different ways to create a CFont
object.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CDCTestView::OnDraw(CDC* pDC)</TT>
<TT>{</TT>
<TT> pDC->SetMapMode( m_nMapMode );</TT>
<TT> CRect rcClient;</TT>
<TT> GetClientRect( rcClient );</TT>
<TT> pDC->DPtoLP( rcClient );</TT>
<TT> COLORREF clrOld = pDC->SetTextColor( m_clrChoice );</TT>
<TT> int nOldMode = pDC->SetBkMode( TRANSPARENT );</TT>
<TT> CFont fntArial, fntBoldSwiss;</TT>
<TT> fntArial.CreateFont( 0, 0, 0, 0, 0, 0, 0, 0,</TT>
<TT> 0, 0, 0, 0, 0, "Arial" );</TT>
<TT> fntBoldSwiss.CreateFont( rcClient.Height()/20, 0, 0, 0,</TT>
<TT> FW_BOLD, TRUE, FALSE, 0, ANSI_CHARSET,</TT>
<TT> OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,</TT>
<TT> DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS,</TT>
<TT> NULL );</TT>
<TT> CString szMsg = "Hello! Change the color and mapping mode";</TT>
<TT> CFont* pOldFont = pDC->SelectObject( &fntArial );</TT>
<TT> int cy = rcClient.Height()/4;</TT>
<TT> pDC->TextOut( 0, cy, szMsg );</TT>
<TT> pDC->SelectObject( &fntBoldSwiss );</TT>
<TT> TEXTMETRIC tm;</TT>
<TT> pDC->GetTextMetrics(&tm);</TT>
<TT> cy += tm.tmHeight + tm.tmExternalLeading;</TT>
<TT> pDC->TextOut( 0, cy , szMsg );</TT>
<TT> // Restore the old GDI objects</TT>
<TT> pDC->SelectObject( pOldFont );</TT>
<TT> pDC->SetTextColor( clrOld );</TT>
<TT> pDC->SetBkMode( nOldMode );</TT>
</FONT></PRE>
<P><FONT COLOR="#0066FF"><TT>}</TT></FONT>
<BLOCKQUOTE>
<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>CAUTION:</B></FONT><B> </B>As with all GDI objects,
you must save the original font that is returned when a new font is selected into
a device context. If you fail to select the original font into the device context
when you're finished with the DC, you will create a resource leak.
<HR>
</BLOCKQUOTE>
<H3><FONT COLOR="#000077"><B>Creating a Font Using a <TT>LOGFONT</TT> Structure</B></FONT></H3>
<P>The <TT>LOGFONT</TT> structure is often used to describe a font. Just as the <TT>LOGBRUSH</TT>
structure discussed in Hour 12 was used to describe a particular brush, the <TT>LOGFONT</TT>
structure is used to describe a particular font. A <TT>LOGFONT</TT> isn't a font;
it's just a description, so it contains members for all the attributes available
for a font.</P>
<P>Using a <TT>LOGFONT</TT> simplifies creating fonts because many of the attributes
for a series of fonts can be shared. Listing 13.2 is a version of <TT>CDCTestView::OnDraw</TT>
that uses a <TT>LOGFONT</TT> structure to create several different fonts.
<H4><FONT COLOR="#000077">TYPE: Listing 13.2. Using a LOGFONT structure to create
fonts.</FONT></H4>
<PRE><FONT COLOR="#0066FF">
<TT>void CDCTestView::OnDraw(CDC* pDC)</TT>
<TT>{</TT>
<TT> CRect rcClient;</TT>
<TT> GetClientRect( rcClient );</TT>
<TT> pDC->DPtoLP( rcClient );</TT>
<TT> COLORREF clrOld = pDC->SetTextColor( m_clrChoice );</TT>
<TT> int nOldMode = pDC->SetBkMode( TRANSPARENT );</TT>
<TT> CString szMsg = "Hello! I'm an Arial font";</TT>
<TT> CFont fntArial;</TT>
<TT> LOGFONT lf;</TT>
<TT> ZeroMemory( &lf, sizeof(LOGFONT) );</TT>
<TT> lstrcpy( lf.lfFaceName, "Arial" );</TT>
<TT> fntArial.CreateFontIndirect( &lf );</TT>
<TT> CFont* pOldFont = pDC->SelectObject( &fntArial );</TT>
<TT> pDC->TextOut( rcClient.Width()/2, rcClient.Height()/2, szMsg );</TT>
<TT> pDC->SelectObject( pOldFont );</TT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -