?? ch14.htm
字號:
<H4><FONT COLOR="#000077">Changes to the CAboutDlg Class</FONT></H4><P>Add two new variables to the <TT>CAboutDlg</TT> class. These variables are usedto store the handles to icons displayed on the dialog box buttons. Add the sourcecode from Listing 14.1 to the Implementation section of the <TT>CAboutDlg</TT> project.Also, add a declaration for a destructor for the <TT>CAboutDlg</TT> class, just afterthe constructor declaration.<H4><FONT COLOR="#000077">TYPE: Listing 14.1. Additions to the CAboutDlg class declaration.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>// Implementation</TT><TT>public:</TT><TT> "CAboutDlg();</TT><TT>protected:</TT><TT> HICON m_hIconOkay;</TT><TT> HICON m_hIconCancel;</TT></FONT></PRE><P>The icons are added to the dialog box's buttons when the dialog box receives the<TT>WM_INITDIALOG</TT> message. Using ClassWizard, add a message-handling functionfor <TT>WM_INITDIALOG</TT> to the <TT>CAboutDlg</TT> class. Use the default nameprovided by ClassWizard, <TT>OnInitDialog</TT>. Edit the <TT>OnInitDialog</TT> memberfunction so it looks like the code provided in Listing 14.2.<H4><FONT COLOR="#000077">TYPE: Listing 14.2. The AboutDlg::OnInitDialog member function.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>BOOL CAboutDlg::OnInitDialog()</TT><TT>{</TT><TT> CDialog::OnInitDialog();</TT><TT> CWinApp* pApp = AfxGetApp();</TT><TT> if( pApp != 0 )</TT><TT> {</TT><TT> m_hIconOkay = pApp->LoadIcon( IDI_GREEN );</TT><TT> m_hIconCancel = pApp->LoadIcon( IDI_RED );</TT><TT> ASSERT(m_hIconOkay);</TT><TT> ASSERT(m_hIconCancel);</TT><TT> m_btnOkay.SetIcon( m_hIconOkay );</TT><TT> m_btnCancel.SetIcon( m_hIconCancel );</TT><TT> }</TT><TT> return TRUE;</TT><TT>}</TT></FONT></PRE><P>The source code in Listing 14.2 loads the two stop-light icons created earlier.After the icons are loaded, the icon handles are passed to the <TT>SetIcon</TT> functionfor each of the buttons contained in the dialog box.<BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>When an icon is drawn on a button, the icon is clipped if necessary. The icon isn't scaled to fit inside the button; it is displayed "actual size." This might mean that you must experiment with the relative sizes of the icon and the button. <HR></BLOCKQUOTE><P>As the dialog box is destroyed, the icons previously loaded using <TT>LoadIcon</TT>must be destroyed. Use the source code from Listing 14.3 to create the <TT>CAboutDlg</TT>class destructor.<H4><FONT COLOR="#000077">TYPE: Listing 14.3. Using the CAboutDlg class destructorto destroy the previously loaded icons.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>CAboutDlg::"CAboutDlg()</TT><TT>{</TT><TT> DestroyIcon( m_hIconOkay );</TT><TT> DestroyIcon( m_hIconCancel );</TT><TT>}</TT></FONT></PRE><P>Compile and run the DCTest example. Figure 14.3 shows the DCTest About box withicons placed in the pushbutton controls.</P><P><A NAME="03"></A><A HREF="03.htm"><B>Figure 14.3.</B> </A><I><BR>The DCTest dialog box after adding icons to the pushbutton controls.</I><H2><FONT COLOR="#000077"><B>What Is a Cursor?</B></FONT></H2><P>A cursor is the little bitmap that moves around the screen providing feedbackabout the current mouse position. The cursor also provides other types of feedback:<UL> <LI>If the application is busy and won't accept input, most applications change the regular cursor to the hourglass cursor.<BR> <BR> <LI>If the cursor is over a window or control that accepts text input, most applications change the regular cursor to the I-beam cursor.</UL><P>The most commonly used cursors are supplied by Windows. The hourglass, I-beam,and arrow cursors are three of the more popular standard cursors. In addition, eachprogram can define cursors that you add to the application just as you do other resources.<BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>The cursor is an important part of the feedback supplied to a user of a Windows program. Changing the style of cursor is an easy way to alert the user that a change of some type has occurred. Many times, changing the cursor is the only type of feedback required. <HR></BLOCKQUOTE><H2><FONT COLOR="#000077"><B>Using Cursors in Windows Programs</B></FONT></H2><P>Most window classes have a cursor assigned to the class. In almost all cases,it's the standard arrow cursor. This means that for most default behavior, you don'thave to do anything to use a cursor; Windows provides it free of charge. However,there are some situations in which you must take control over the cursor yourself.For the examples in this hour, you create an SDI project named Cursor.<H3><FONT COLOR="#000077"><B>Creating a Cursor Resource</B></FONT></H3><P>You create a cursor image using the Developer Studio image editor, much like iconswere created earlier this hour. Figure 14.4 shows the cursor used in later examplesready for editing in the image editor.</P><P>Create the cursor shown in Figure 14.4 and name it <TT>IDC_BANG</TT>. To createa cursor resource, right-click in the resource view window and choose Insert... fromthe pop-up menu; then select Cursor from the Resource Type dialog box. The editingtools you use to create a cursor are the same ones you used to create icons earlierin this hour. The standard Windows naming convention is for cursors to have namesbeginning with <TT>IDC_</TT>.<H3><FONT COLOR="#000077"><B>Adding a Hotspot to a Cursor</B></FONT></H3><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>hotspot</I> is the actualpoint that determines the current cursor position.</P><P>Every cursor has a hotspot. The hotspot for the arrow cursor is located at thevery tip of the arrow. The default hotspot for a cursor is the upper-left cornerof the cursor. The cursor-image editor enables you to move the hotspot to a positionthat is reasonable for the cursor image.</P><P><A NAME="04"></A><A HREF="04.htm"><B>Figure 14.4.</B> </A><I><BR>The <TT>IDC_BANG</TT> cursor inside the Developer Studio image editor.</I></P><P>For example, the <TT>IDC_BANG</TT> cursor you created in the previous sectionwill not work properly if a new hotspot isn't defined. Because the current hotspotis part of the background, this cursor won't work as well for operations in whichthe mouse clicks must be accurate. One solution, as shown in Figure 14.5, is to modifythe cursor to add a well-defined hotspot to the cursor--in this case a bull's-eye,or target, in the upper-left corner of the cursor bitmap.</P><P><A NAME="05"></A><A HREF="05.htm"><B>Figure 14.5.</B> </A><I><BR>The new version of <TT>IDC_BANG</TT>, with a hotspot and a bull's-eye.</I></P><P>The hotspot control is a button located above the edited image. Click the hotspotbutton and then click the new hotspot pixel. For <TT>IDC_BANG</TT>, create a newhotspot in the center of the bull's-eye.<H3><FONT COLOR="#000077"><B>Changing a Cursor</B></FONT></H3><P>Changing the current mouse cursor is probably the most common cursor-related activityin Windows programming. The operating system sends a <TT>WM_SETCURSOR</TT> messageto a window as the mouse cursor passes over it. You can use this message to changethe cursor, or you can let Windows choose the cursor that was defined for the windowwhen it was registered.</P><P>To change the current cursor for a window, you handle the <TT>WM_SETCURSOR</TT>message. Using ClassWizard, add a message-handling function for <TT>WM</TT>_<TT>SETCURSOR</TT>to the <TT>CAboutDlg</TT> class. Listing 14.4 contains source code for <TT>OnSetCursor</TT>that changes the cursor to <TT>IDC_BANG</TT>.<H4><FONT COLOR="#000077">TYPE: Listing 14.4. Changing the cursor during WM_SETCURSOR.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>BOOL CAboutDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest,</TT><TT> UINT message)</TT><TT>{</TT><TT> // Load and set the new cursor. Return TRUE to stop</TT><TT> // further processing of this message.</TT><TT> CWinApp* pApp = AfxGetApp();</TT><TT> HICON hIconBang = pApp->LoadCursor( IDC_BANG );</TT><TT> SetCursor( hIconBang );</TT><TT> return TRUE;</TT><TT>}</TT></FONT></PRE><H3><FONT COLOR="#000077"><B>Conditionally Changing a Cursor</B></FONT></H3><P>Changing a cursor conditionally is often convenient, based on the cursor's location.Listing 14.5 is a new version of <TT>OnSetCursor</TT> that restores the arrow cursorwhen the cursor is over the dialog box's OK button.<H4><FONT COLOR="#000077">TYPE: Listing 14.5. Conditionally changing the cursor duringWM_SETCURSOR.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>BOOL CAboutDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest,</TT><TT> UINT message)</TT><TT>{</TT><TT> BOOL bReturn;</TT><TT> CRect rcBtn;</TT><TT> CPoint ptCursor;</TT><TT> //</TT><TT> // Calculate the current cursor position, and change the</TT><TT> // cursor if we're not over the OK button.</TT><TT> //</TT><TT> CWnd* pBtn = GetDlgItem( IDOK );</TT><TT> pBtn->GetWindowRect( rcBtn );</TT><TT> GetCursorPos( &ptCursor );</TT><TT> if( rcBtn.PtInRect( ptCursor ) == FALSE )</TT><TT> {</TT><TT> // Load and set the new cursor. Return TRUE to stop</TT><TT> // further processing of this message.</TT><TT> CWinApp* pApp = AfxGetApp();</TT><TT> HICON hIconBang = pApp->LoadCursor( IDC_BANG );</TT><TT> SetCursor( hIconBang );</TT><TT> bReturn = TRUE;</TT><TT> }</TT><TT> else</TT><TT> {</TT><TT> // We're over the OK button, use the default cursor.</TT><TT> bReturn = CDialog::OnSetCursor(pWnd, nHitTest, message);</TT><TT> }</TT><TT> return bReturn;</TT><TT>}</TT></FONT></PRE><P>The two key lines in Listing 14.5 retrieve the current mouse cursor position asa <TT>CPoint</TT> object. The <TT>CPoint</TT> object is tested to see whether itis inside the boundary of the OK pushbutton:</P><PRE><FONT COLOR="#0066FF"><TT>GetCursorPos( &ptCursor );</TT><TT>if( rcBtn.PtInRect( ptCursor ) == FALSE )</TT><TT>{</TT><TT> // cursor not over rectangle</TT><TT>}</TT></FONT></PRE><H3><FONT COLOR="#000077"><B>Using the Standard Cursors</B></FONT></H3><P>Windows provides 19 standard cursors for use in your programs. These cursors oftenare used by Windows. For example, the <TT>IDC_APPSTARTING</TT> cursor is displayedwhen an application is launched by Windows. Table 14.2 lists the names and descriptionsof the 19 standard cursors.<H4><FONT COLOR="#000077">Table 14.2. The standard cursors provided by Windows.</FONT></H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><B>Cursor Name</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Description</B></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_ARROW</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">Arrow cursor</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_IBEAM</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">I-beam cursor</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_WAIT</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">Hourglass cursor</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_CROSS</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">Crosshair cursor</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_UPARROW</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">Up-arrow cursor</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_SIZENWSE</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">Sizing cursor, points northwest and southeast</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_SIZENESW</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">Sizing cursor, points northeast and southwest</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_SIZEWE</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">Sizing cursor, points west and east</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_SIZENS</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">Sizing cursor, points north and south</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_SIZEALL</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">Sizing cursor, points north, south, east, and west</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_NO</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">"No" cursor (circle with a slash through it)</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_APPSTARTING</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">Application-starting cursor</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_HELP</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">Help cursor</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDI_APPLICATION</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">Application icon</TD>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -