📄 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 used
to store the handles to icons displayed on the dialog box buttons. Add the source
code 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 after
the 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 function
for <TT>WM_INITDIALOG</TT> to the <TT>CAboutDlg</TT> class. Use the default name
provided by ClassWizard, <TT>OnInitDialog</TT>. Edit the <TT>OnInitDialog</TT> member
function 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> function
for 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 destructor
to 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 with
icons 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 feedback
about 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, each
program 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't
have 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 icons
were created earlier this hour. Figure 14.4 shows the cursor used in later examples
ready for editing in the image editor.</P>
<P>Create the cursor shown in Figure 14.4 and name it <TT>IDC_BANG</TT>. To create
a cursor resource, right-click in the resource view window and choose Insert... from
the pop-up menu; then select Cursor from the Resource Type dialog box. The editing
tools you use to create a cursor are the same ones you used to create icons earlier
in this hour. The standard Windows naming convention is for cursors to have names
beginning 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 actual
point that determines the current cursor position.</P>
<P>Every cursor has a hotspot. The hotspot for the arrow cursor is located at the
very tip of the arrow. The default hotspot for a cursor is the upper-left corner
of the cursor. The cursor-image editor enables you to move the hotspot to a position
that 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 section
will not work properly if a new hotspot isn't defined. Because the current hotspot
is part of the background, this cursor won't work as well for operations in which
the mouse clicks must be accurate. One solution, as shown in Figure 14.5, is to modify
the 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 hotspot
button and then click the new hotspot pixel. For <TT>IDC_BANG</TT>, create a new
hotspot 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 activity
in Windows programming. The operating system sends a <TT>WM_SETCURSOR</TT> message
to a window as the mouse cursor passes over it. You can use this message to change
the cursor, or you can let Windows choose the cursor that was defined for the window
when 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 cursor
when the cursor is over the dialog box's OK button.
<H4><FONT COLOR="#000077">TYPE: Listing 14.5. Conditionally 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> 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 as
a <TT>CPoint</TT> object. The <TT>CPoint</TT> object is tested to see whether it
is 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 often
are used by Windows. For example, the <TT>IDC_APPSTARTING</TT> cursor is displayed
when an application is launched by Windows. Table 14.2 lists the names and descriptions
of 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 + -