?? ch14.htm
字號(hào):
<!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"><IMGSRC="../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 learnhow 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 indicatethat 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 smallbitmap that represents another object in a Windows program.</P><P>Icons are used to represent minimized child windows in an MDI application. Iconsalso are widely used by Windows itself. When a program is minimized, its icon isdisplayed in the Windows 95 taskbar. When you're using the Explorer, the icon representingan application associated with each file is displayed next to the file's name. Windowsdisplays 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 whendetermining which icons to display. If large and small icons are available, the Exploreruses the icon resources from your application's EXE file. However, if you provideonly a large icon, the Explorer synthesizes a small icon, which usually results ina 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 ViewControls," you will use large and small icons in a list view control. Four differenttypes 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 MFCclass named <TT>CIcon</TT> dedicated to making using icons easier. Instead, you manipulatean <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 yourproject automatically. You can use the Developer Studio image editor to edit or createnew icons for your project.</P><P>To open the image editor, open the ResourceView in the project workspace and thenopen the Icon folder. Double-click any icon resource contained in the folder to openthe editor. In an MDI application created by AppWizard, two icon resources will bedefined 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 youare working.</P><P>The color palette is displayed whenever you are editing an image resource. Thecolor palette consists of several colored boxes. To change the color of the currentdrawing tool, click the color you want. There are two special color icons in thecolor 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 cornerof the color palette. Switch to the transparent color by clicking the small videodisplay icon with a green screen, and switch to the reverse video color by clickingthe 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 folderin the resource view, and select Insert Icon from the pop-up menu; this opens theimage editor with a blank icon, ready for editing. You can change attributes foricon resources, as with all resources, by double-clicking the edge of the icon resourceor 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 requiresthree 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 tothe 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 coordinatesand 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 createthe project. To change this icon, open the image editor by double-clicking the application'sicon 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 smallformats. If you edit one of the icon formats, make sure you make corresponding changesin 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 supportedformats for the icon. Selecting a new format loads that version of the icon intothe image editor.</P><P>Every child window type also has a unique icon. You can edit that icon just asyou do the program's main icon. As discussed earlier this hour, the child windowicon 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 itemdirectly, using the MFC <TT>CImageList::Draw</TT> member function. However, you alsocan 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 withWindows 95, it's possible to display an icon in a button almost as easily as displayinga text string. Use the <TT>CButton</TT> member function <TT>SetIcon</TT> to set abutton's icon. The icon must be loaded before it is used and destroyed after thebutton 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 consistsof a red circle surrounded by the transparent color. Also, create a similar iconnamed <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 dialogbox. Modify the <TT>IDD_ABOUT</TT> dialog box by adding an extra button to it, asshown 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 theAbout 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 DCTestAbout 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 theIcon option is checked.
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -