⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch07.htm

📁 VC 21天 学习VC 的好东西
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>

<HEAD>
	<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<SCRIPT LANGUAGE="JavaScript">

<!--

function popUp(pPage) {
 var fullURL = document.location;
 var textURL = fullURL.toString();
 var URLlen = textURL.length;
 var lenMinusPage = textURL.lastIndexOf("/");
 lenMinusPage += 1;
 var fullPath = textURL.substring(0,lenMinusPage);
 popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
 figDoc= popUpWin.document;
 zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
 zhtm += '</head>';
 zhtm += '<BODY bgcolor="#FFFFFF">
<!-- Spidersoft WebZIP Ad Banner Insert -->
<TABLE width=100% border="0" cellpadding="0" cellspacing="0">
<TR>
<TD>
<ILAYER id=ad1 visibility=hidden height=60></ILAYER>
<NOLAYER>
<IFRAME SRC="http://www.spidersoft.com/ads/bwz468_60.htm" width="100%" height="60" marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no></IFRAME>
</NOLAYER>
</TD>
</TR>
</TABLE>
<!-- End of Spidersoft WebZIP Ad Banner Insert-->
';
 zhtm += '<IMG SRC="' + fullPath + pPage + '">';
 zhtm += '<P><B>' + pPage + '</B>';
 zhtm += '
<layer src="http://www.spidersoft.com/ads/bwz468_60.htm" visibility=hidden id=a1 width=600 onload="moveToAbsolute(ad1.pageX,ad1.pageY); a1.clip.height=60;visibility='show';"></layer>
</BODY></HTML>';
 window.popUpWin.document.write(zhtm);
 window.popUpWin.document.close();
 // Johnny Jackson 4/28/98
 }

//-->
                                                                
</SCRIPT>
<link rel="stylesheet" href="../../../../includes/stylesheets/ebooks.css">

	<META NAME="GENERATOR" Content="Symantec Visual Page Mac 1.1.1">
	<TITLE>Teach Yourself Visual C++ 6 in 21 Days -- Ch 7 -- Working with Text and Fonts</TITLE>
</HEAD>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF">

<H1 ALIGN="CENTER"><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM"
BORDER="0"><BR>
Teach Yourself Visual C++ 6 in 21 Days</H1>
<CENTER>
<P><A HREF="../ch06/ch06.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch08/ch08.htm"><IMG
SRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> 
<HR>

</CENTER>
<H1 ALIGN="CENTER">- 7 -<BR>
Working with Text and Fonts</H1>
<H1></H1>

<UL>
	<LI><A HREF="#Heading1">Finding and Using Fonts</A>
	<UL>
		<LI><A HREF="#Heading2">Listing the Available Fonts</A>
		<LI><A HREF="#Heading3">Using a Font</A>
	</UL>
	<LI><A HREF="#Heading4">Using Fonts</A>
	<UL>
		<LI><A HREF="#Heading5">Creating the Application Shell</A>
		<LI><A HREF="#Heading6">Building a List of Fonts</A>
		<LI><A HREF="#Heading7">Setting the Font Sample Text</A>
		<LI><A HREF="#Heading8">Selecting a Font to Display</A>
	</UL>
	<LI><A HREF="#Heading9">Summary</A>
	<LI><A HREF="#Heading10">Q&amp;A</A>
	<LI><A HREF="#Heading11">Workshop</A>
	<LI><A HREF="#Heading12">Quiz</A>
	<UL>
		<LI><A HREF="#Heading13">Exercises</A>
	</UL>
	<LI><A HREF="#Heading14">In Review</A>
</UL>

<P>
<HR SIZE="4">
<BR>
In most Windows applications, you don't need to worry about specifying fonts, much
less their weight, height, and so on. If you don't specify the font to be used, Windows
supplies a default font for your application. If you do need to use a particular
font, you can specify a font to use for a particular dialog window through the dialog
properties. Sometimes, however, you want or need to control the font used in your
application. You might need to change the font being used or allow the user to select
a font to use in a particular instance. It is for those circumstances that you will
learn how to change and list fonts today. Among the things that you will learn are</P>

<P>

<UL>
	<LI>How to build a list of available fonts.
	<P>
	<LI>How to specify a font for use.
	<P>
	<LI>How to change fonts dynamically.
</UL>

<H2><A NAME="Heading1"></A>Finding and Using Fonts</H2>
<P>One of the first things that you need to know when working with fonts is that
not every system that your applications run on will have the same fonts installed.
Fonts are specified in files that can be installed and removed from Windows systems
with relative ease. Every computer user can customize his system with whatever combination
of fonts he wants. If you specify a font that doesn't exist on the system, Windows
will choose either the system default font or what the operating system considers
to be a reasonably close alternative font.</P>
<P>What you can do instead is ask the operating system what fonts are available.
This method allows you to make your own decisions on which font to use or let the
user make the decision. When you ask what fonts are available, you can limit the
types of fonts that are listed, or you can choose to list them all and select various
fonts based on various attributes.</P>
<P>
<H3><A NAME="Heading2"></A>Listing the Available Fonts</H3>
<P>To get a list of all available fonts on a computer, you call a Windows API (Application
Programming Interface) function called EnumFontFamiliesEx. This function tells Windows
that you want a list of the fonts on the system. Before you start using this function
and expecting it to pass you a big list of available fonts, you need to understand
how it gives you the list.</P>
<P>
<H4>Callback Functions</H4>
<P>One of the key arguments to the EnumFontFamiliesEx function is the address of
another function. This second function is what is known as a callback function, which
is called by the operating system. For almost every enumeration function in the Windows
operating system, you pass the address of a callback function as an argument because
the callback function is called once for each of the elements in the enumerated list.
In other words, you have to include a function in your application to receive each
individual font that is on the system and then build the list of fonts yourself.</P>
<P>When you create this function to receive each font and build your list, you cannot
define your callback function in any way you want. All callback functions are already
defined in the Windows API. You have to use a specific type of callback function
to receive the list of fonts. For getting a list of fonts, the function type is EnumFontFamProc.
This function type specifies how your function must be defined, what its arguments
must be, and what type of return value it must return. It does not specify what your
function should be named or how it needs to work internally. These aspects are left
completely up to you.</P>
<P>
<H4>The EnumFontFamiliesEx Function</H4>
<P>The EnumFontFamiliesEx function, which you call to request the list of available
fonts, takes five arguments. A typical use of this function follows:</P>
<P>
<PRE>// Create a device context variable
CClientDC dc (this);
// Declare a LOGFONT structure
LOGFONT lLogFont;
// Specify the character set
lLogFont.lfCharSet = DEFAULT_CHARSET;
// Specify all fonts
lLogFont.lfFaceName[0] = NULL;
// Must be zero unless Hebrew or Arabic
lLogFont.lfPitchAndFamily = 0;
// Enumerate the font families
::EnumFontFamiliesEx((HDC) dc, &amp;lLogFont,
(FONTENUMPROC) EnumFontFamProc, (LPARAM) this, 0);
</PRE>
<P>The first argument is a device context, which can be an instance of the CClientDC
class. Every application running within the Windows operating system has a device
context. The device context provides a lot of necessary information to the operating
system about what is available to the application and what is not.</P>
<P>The second argument is a pointer to a LOGFONT structure. This structure contains
information about the fonts that you want listed. You can specify in this structure
which character set you want to list or whether you want all the fonts in a particular
font family. If you want all the fonts on the system, you pass NULL in the place
of this argument.</P>
<P>The third argument is the address of the callback function that will be used to
build your list of fonts. Passing the address of your callback function is a simple
matter of using the function name as the argument. The Visual C++ compiler takes
care of replacing the function name with the function address. However, you do need
to cast the function as the type of callback function that the function requires.</P>
<P>The fourth argument is a LPARAM value that will be passed to the callback function.
This parameter is not used by Windows but provides your callback function with a
context in which to build the font list. In the example, the value being passed is
a pointer to the window in which the code is being run. This way, the callback function
can use this pointer to access any structures it needs to build the list of fonts.
This pointer can also be the first node in a linked list of fonts or other such structure.</P>
<P>The fifth and final argument is always 0. This reserved argument may be used in
future versions of Windows, but for now, it must be 0 so that your application passes
a value that won't cause the function to misbehave.</P>
<P>
<H4>The EnumFontFamProc Function Type</H4>
<P>When you create your callback function, it must be defined as an independent function,
not as a member of any C++ class. A typical EnumFontFamProc function declaration
follows:</P>
<P>
<PRE>int CALLBACK EnumFontFamProc(
LPENUMLOGFONT lpelf,
LPNEWTEXTMETRIC lpntm,
DWORD nFontType,
long lParam)
{
    // Create a pointer to the dialog window
    CMyDlg* pWnd = (CMyDlg*) lParam;
    // Add the font name to the list box
    pWnd-&gt;m_ctlFontList.AddString(lpelf-&gt;elfLogFont.lfFaceName);
    // Return 1 to continue font enumeration
    return 1;
}
</PRE>
<P>The first argument to this function is a pointer to an ENUMLOGFONTEX structure.
This structure contains information about the logical attributes of the font, including
the font name, style, and script. You may have numerous fonts listed with the same
name but different styles. You can have one for normal, one for bold, one for italic,
and one for bold italic.</P>
<P>The second argument is a pointer to a NEWTEXTMETRICEX structure. This structure
contains information about the physical attributes of the font, such as height, width,
and space around the font. These values are all relative in nature because they need
to scale as the font is made larger or smaller.</P>
<P>The third argument is a flag that specifies the type of font. This value may contain
a combination of the following values:</P>

<UL>
	<LI>DEVICE_FONTYPE
	<LI>RASTER_FONTYPE
	<P>
	<LI>TRUETYPE_FONTYPE
</UL>

<P>Finally, the fourth argument is the value that was passed into the EnumFontFamiliesEx
function. In the example, it was a pointer to the dialog on which the list of fonts
is being built. If you cast this value as a pointer to the dialog, the function can
access a list box control to add the font names.</P>
<P>The return value from this function determines whether the listing of fonts continues.
If 0 is returned from this function, the operating system quits listing the available
fonts. If 1 is returned, the operating system continues to list the available fonts.</P>
<P>
<H3><A NAME="Heading3"></A>Using a Font</H3>
<P>To use a particular font in an application, you call an instance of the CFont
class. By calling the CreateFont method, you can specify the font to be used, along
with the size, style, and orientation. Once you've created a font, you can tell a
control or window to use the font by calling the object's SetFont method. An example
of this process follows:</P>
<P>
<PRE>CFont m_fFont;    // The font to be used
// Create the font to be used
m_fFont.CreateFont(12, 0, 0, 0, FW_NORMAL,
        0, 0, 0, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS,
        CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH |
        FF_DONTCARE, m_sFontName);
// Set the font for the display area
m_ctlDisplayText.SetFont(&amp;m_fFont);
</PRE>


<BLOCKQUOTE>
	<P>
<HR>
<STRONG>TIP:</STRONG> The CFont variable used in the previous code should be declared as
	a member variable of the class in which this code is placed. In the sample code,
	it is declared above where it is used to show how it is declared. This variable should
	not be declared or used as a local variable in a function.
<HR>


</BLOCKQUOTE>

<P>Seems simple enough--just two function calls--but that CreateFont function needs
an awful lot of arguments passed to it. It is these arguments that make the CreateFont
method a flexible function with a large amount of functionality. Once you create
the font, using it is a simple matter of passing the font to the SetFont method,
which is a member of the CWnd class and thus available to all window and control
classes in Visual C++. This means that you can use this technique on any visible
object within a Visual C++ application.</P>
<P>To understand how the CreateFont function works, let's look at the individual
arguments that you have to pass to it. The function is defined as</P>
<P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -