📄 ch14.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Teach Yourself Visual C++® 5 in 24 Hours -- Hour 14 -- Icons and Cursors</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<CENTER>
<H1><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
<FONT COLOR="#000077">Teach Yourself Visual C++® 5 in 24 Hours</FONT></H1>
</CENTER>
<CENTER>
<P><A HREF="../ch13/ch13.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch15/ch15.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>
<CENTER>
<H1><FONT COLOR="#000077">- Hour 14 -<BR>
Icons and Cursors</FONT></H1>
</CENTER>
<P>Icons and cursors are two commonly used GDI objects. In this hour, you will learn
how to
<UL>
<LI>Use icons in your MFC program<BR>
<BR>
<LI>Add icons to button controls<BR>
<BR>
<LI>Manage cursors in your MFC program
</UL>
<P>There are several examples in this hour; you will add icons to pushbutton controls,
clip cursors to a specific rectangle, and change the cursor to an hourglass to indicate
that a long process is in progress.
<H2><FONT COLOR="#000077"><B>What Is an Icon?</B></FONT></H2>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>An <I>icon</I> is a small
bitmap that represents another object in a Windows program.</P>
<P>Icons are used to represent minimized child windows in an MDI application. Icons
also are widely used by Windows itself. When a program is minimized, its icon is
displayed in the Windows 95 taskbar. When you're using the Explorer, the icon representing
an application associated with each file is displayed next to the file's name. Windows
displays the program's icon in the upper-left corner of the main window title bar.</P>
<P>The Windows 95 Explorer uses the icon resources associated with your program when
determining which icons to display. If large and small icons are available, the Explorer
uses the icon resources from your application's EXE file. However, if you provide
only a large icon, the Explorer synthesizes a small icon, which usually results in
a small, distorted icon.
<BLOCKQUOTE>
<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>Icons also are
used in dialog boxes. For example, the message dialog boxes discussed in Hour 4,
"Dialog Boxes and C++ Classes," use icons to indicate the type of message
conveyed. It's also common practice to include an application's icon in the About
dialog box.
<HR>
</BLOCKQUOTE>
<P>There are several different types of icon resources. In Hour 18, "List View
Controls," you will use large and small icons in a list view control. Four different
types of icons are available:
<UL>
<LI>Large icons: Used for most programs written for Windows before the release of
Windows 95, these icons are 32x32 pixels and support 16 colors.<BR>
<BR>
<LI>Small icons: First introduced with Windows 95, these icons are usually a smaller
(16x16 pixels) version of a program's large icon.<BR>
<BR>
<LI>256-color icons: These icons support more than the standard 16 colors available
to other types of icons. These icons are 48x48 pixels, and are never displayed as
a program's icon when the window is minimized.<BR>
<BR>
<LI>Monochrome icons: These icons support only two colors and are 32x32 pixels large.
</UL>
<P>Icons are similar to bitmaps, which are covered in Hour 15, "Using Bitmaps."
However, the code required to use an icon is so simple that there's not even an MFC
class named <TT>CIcon</TT> dedicated to making using icons easier. Instead, you manipulate
an <TT>HICON</TT>, or handle to an icon, directly.
<BLOCKQUOTE>
<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Time Saver:</B></FONT><B> </B>An image list is ideal
for collecting icon images. Any image stored in an image list can be converted into
an icon using the <TT>ExtractIcon</TT> member function. Image lists are covered in
Hour 17, "Using Image Lists and Bitmaps."
<HR>
</BLOCKQUOTE>
<H2><FONT COLOR="#000077"><B>Creating Icons Using the Image Editor</B></FONT></H2>
<BLOCKQUOTE>
<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>Because icons are
resources, you add them to a program's resource file just as you do bitmaps, menus,
and dialog boxes. You create new icons using the resource editor.
<HR>
</BLOCKQUOTE>
<P>When creating a new project, AppWizard creates a set of default icons for your
project automatically. You can use the Developer Studio image editor to edit or create
new icons for your project.</P>
<P>To open the image editor, open the ResourceView in the project workspace and then
open the Icon folder. Double-click any icon resource contained in the folder to open
the editor. In an MDI application created by AppWizard, two icon resources will be
defined for a new project:
<UL>
<LI><TT>IDR_MAINFRAME</TT>: An icon that is associated with the application; this
is the MFC cube icon by default.<BR>
<BR>
<LI><TT>IDR_MYAPPTYPE</TT>: Where MyApp is the name of the project. This icon is
used to represent the MDI child window. By default, this icon is the standard MFC
Doc icon.
</UL>
<P>You can change these icon resources to represent the application with which you
are working.</P>
<P>The color palette is displayed whenever you are editing an image resource. The
color palette consists of several colored boxes. To change the color of the current
drawing tool, click the color you want. There are two special color icons in the
color palette:
<UL>
<LI>The <I>transparent</I> color: The background shows through the icon<BR>
<BR>
<LI>The <I>reverse video</I> color: The background shows through after reversing
the video
</UL>
<P>You can find the transparent and reverse video colors on the upper-right corner
of the color palette. Switch to the transparent color by clicking the small video
display icon with a green screen, and switch to the reverse video color by clicking
the small video display with the red screen.
<H3><FONT COLOR="#000077"><B>Inserting a New Icon Resource</B></FONT></H3>
<P>To insert a new icon resource into an existing project, right-click the Icon folder
in the resource view, and select Insert Icon from the pop-up menu; this opens the
image editor with a blank icon, ready for editing. You can change attributes for
icon resources, as with all resources, by double-clicking the edge of the icon resource
or by pressing Alt+Enter on the keyboard.
<H3><FONT COLOR="#000077"><B>Loading an Icon</B></FONT></H3>
<P>After you have added an icon to a project, loading and displaying it requires
three lines of code. To load an icon and prepare it for display, use the <TT>LoadIcon</TT>
function:</P>
<PRE><FONT COLOR="#0066FF"><TT>HICON hIcon = AfxGetApp()->LoadIcon( IDI_LOGO );</TT>
</FONT></PRE>
<P>Because <TT>LoadIcon</TT> is a <TT>CWinApp</TT> member function, a pointer to
the application's <TT>CWinApp</TT> object must be fetched using the <TT>AfxGetApp</TT>
function.</P>
<P>After the icon has been loaded, display it by calling <TT>DrawIcon</TT>:</P>
<PRE><FONT COLOR="#0066FF"><TT>pDC->DrawIcon( 0,0, hIcon );</TT>
</FONT></PRE>
<P>The <TT>DrawIcon</TT> function is a member of the <TT>CDC</TT> class. The coordinates
and icon handle must be passed as parameters.</P>
<P>After using <TT>LoadIcon</TT>, release the icon resource by calling the <TT>DestroyIcon</TT>
function:</P>
<PRE><FONT COLOR="#0066FF"><TT>DestroyIcon( hIcon );</TT>
</FONT></PRE>
<BLOCKQUOTE>
<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>CAUTION:</B></FONT><B> </B>If you forget to call
<TT>DestroyIcon</TT>, the memory allocated for the icon isn't released.
<HR>
</BLOCKQUOTE>
<H3><FONT COLOR="#000077"><B>Changing a Program's Icon</B></FONT></H3>
<P>The icon used for a program is created by AppWizard when you initially create
the project. To change this icon, open the image editor by double-clicking the application's
icon in the resource view Icon folder.</P>
<P>After opening the icon, use the image editor tools to modify the icon as you want.
Every application written for Windows can have its icon displayed in large and small
formats. If you edit one of the icon formats, make sure you make corresponding changes
in all formats supported by the icon. To display and edit all the available formats,
click the drop-down combo box above the image editor, which displays all the supported
formats for the icon. Selecting a new format loads that version of the icon into
the image editor.</P>
<P>Every child window type also has a unique icon. You can edit that icon just as
you do the program's main icon. As discussed earlier this hour, the child window
icon is named with a shared resource identifier in the form <TT>IDR_MYAPPTYPE</TT>,
where the application is named <TT>MyApp</TT>.
<H3><FONT COLOR="#000077"><B>Retrieving Icons from Image Lists</B></FONT></H3>
<P>When an image is stored in an image list, the image list often draws the item
directly, using the MFC <TT>CImageList::Draw</TT> member function. However, you also
can have the image list create an icon based on an individual image. The <TT>CImageList::ExtractIcon</TT>
member function is used to create such an icon:</P>
<PRE><FONT COLOR="#0066FF"><TT>HICON hIcon = m_imageList.ExtractIcon( 2 );</TT>
</FONT></PRE>
<P>Using this member function is useful when several icons must be stored together.
<H3><FONT COLOR="#000077"><B>Displaying an Icon on a Button</B></FONT></H3>
<P>Another useful way to use an icon is to display it in a button. Beginning with
Windows 95, it's possible to display an icon in a button almost as easily as displaying
a text string. Use the <TT>CButton</TT> member function <TT>SetIcon</TT> to set a
button's icon. The icon must be loaded before it is used and destroyed after the
button is destroyed.
<H4><FONT COLOR="#000077">Adding New Icon Resources</FONT></H4>
<P>For this example, add two buttons to the DCTest About dialog box. These two "stop-light"
buttons work just like the traditional OK and Cancel buttons. Figure 14.1 shows the
<TT>IDI_RED</TT> icon; although you can't tell from the figure, this icon consists
of a red circle surrounded by the transparent color. Also, create a similar icon
named <TT>IDI_GREEN</TT>, using a green circle surrounded by the transparent color.</P>
<P><A NAME="01"></A><A HREF="01.htm"><B>Figure 14.1.</B> </A><BR>
<I>The new icons used in the DCTest example.</I></P>
<P>Use the two icons created earlier to label buttons in the DCTest About dialog
box. Modify the <TT>IDD_ABOUT</TT> dialog box by adding an extra button to it, as
shown in Figure 14.2.</P>
<P><A NAME="02"></A><A HREF="02.htm"><B>Figure 14.2.</B> </A><I><BR>
The About dialog box used in the Icon example.</I></P>
<P>Use the values from Table 14.1 to set the attributes for the two buttons in the
About dialog box. Use ClassWizard to add the two buttons to the <TT>CAboutDlg</TT>
class as <TT>CButton</TT> variables.
<H4><FONT COLOR="#000077">Table 14.1. Values used for button controls in the DCTest
About dialog box.</FONT></H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><B>ID</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Variable Name</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Control Type</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Attributes</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>IDOK</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>m_btnOkay</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">CButton</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Visible, Tabstop, Icon, Default</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>IDCANCEL</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>m_btnCancel</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">CButton</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Visible, Tabstop, Icon</TD>
</TR>
</TABLE>
</P>
<P>Buttons that have icon labels instead of text must have the Icon attribute set.
Review each button's Properties dialog box under the Styles tab and make sure the
Icon option is checked.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -