📄 ch10.htm
字号:
bar when the menu item is highlighted.
</UL>
<H3><FONT COLOR="#000077"><B>Adding a Message-Handling Function</B></FONT></H3>
<P>After adding a menu item to the application's menu, the next step is to add a
message-handling function to handle the new menu item. As discussed in Hour 8, ClassWizard
is used to create message-handling functions for MFC-based Windows programs. To add
a message-handling function for the <TT>ID_FILE_HELLO</TT> menu item, follow these
steps:
<DL>
<DD>1. Open ClassWizard by pressing Ctrl+W or by right-clicking in a source code
window and selecting ClassWizard from the menu.<BR>
<BR>
2. Select the Message Maps tab and select the class that will handle the message
from the Class Name combo box--in this case, <TT>CMainFrame</TT>.<BR>
<BR>
3. Select the object that is generating the message from the Object ID list box--in
this case, <TT>ID_FILE_HELLO</TT>. Two message-handling functions are displayed in
the Messages list box.<BR>
<BR>
4. Select the <TT>COMMAND</TT> message from the Messages list box and click the Add
Function button. Accept the default name suggested by ClassWizard for the function
name--<TT>OnFileHello</TT>.<BR>
<BR>
5. Click OK to close ClassWizard.
</DL>
<P>Edit the <TT>CMainFrame::OnFileHello</TT> function so that it looks like the function
provided in Listing 10.1.
<H4><FONT COLOR="#000077">TYPE: Listing 10.1. The CMainFrame::OnFileHello message-handling
function.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CMainFrame::OnFileHello()</TT>
<TT>{</TT>
<TT> AfxMessageBox( "Hello from the File menu" );</TT>
<TT>}</TT></FONT></PRE>
<BLOCKQUOTE>
<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>These basic steps
are used to add all the menu items used in examples for the remaining hours in this
book. The Developer Studio tools are so easy to use that adding a new menu item will
be second nature in no time.
<HR>
</BLOCKQUOTE>
<H2><FONT COLOR="#000077"><B>Creating a Shortcut Menu</B></FONT></H2>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>shortcut menu</I>, sometimes
called a pop-up or context menu, is displayed by right-clicking on a window. Shortcut
menus provide a list of commonly used actions.</P>
<P>Creating a shortcut menu is similar to modifying an existing menu except that
a new menu resource must be created as the first step. Most shortcut menus are displayed
in response to the <TT>WM_CONTEXTMENU</TT> message, which is sent when the right
mouse button is pressed.
<H3><FONT COLOR="#000077"><B>Creating the Resource</B></FONT></H3>
<P>Use the Developer Studio resource editor to create the context menu. To create
the new menu resource, use one of the following techniques:
<UL>
<LI>Select Resource from the Insert menu and then select Menu from the Insert Resource
dialog box.<BR>
<BR>
<LI>Right-click the Menu folder in the ResourceView and then select Insert Menu from
the pop-up menu.
</UL>
<P>Both of these methods opens a new menu resource for editing. Add a dummy caption
for the first top-level item on the menu bar. This caption is not displayed by the
menu; it is used only as a placeholder.</P>
<P>Open the Properties dialog box for the menu resource by double-clicking the edge
of the menu resource, and change the resource ID to <TT>ID_POPUP</TT>. Using the
values from Table 10.1, add three menu items under the dummy label.
<H4><FONT COLOR="#000077">Table 10.1. Menu items added to the ID_POPUP menu resource.</FONT></H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><B>Menu ID</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Caption</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>ID_LIONS</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">&Lions</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>ID_TIGERS</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">&Tigers</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>ID_BEARS</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">&Bears</TD>
</TR>
</TABLE>
<H3><FONT COLOR="#000077"><B>Adding Message-Handling Functions</B></FONT></H3>
<P>The new context menu is displayed when a right mouse click is detected on the
application's view. After a menu item has been selected, a message is displayed at
the menu's location, similar to the message displayed in the MouseTst example from
Hour 8.</P>
<P>You must add two new variables to the <TT>CMenuView</TT> class: a <TT>CString</TT>
variable that stores the message and a <TT>CPoint</TT> variable that stores the location
of the pop-up menu. Add the source code provided in Listing 10.2 to the <TT>CMenuView</TT>
class after the <TT>//Implementation</TT> comment.
<H4><FONT COLOR="#000077">TYPE: Listing 10.2. New member variables for the CMenuView
class.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>// Implementation</TT>
<TT>protected:</TT>
<TT> CPoint m_ptMsg;</TT>
</FONT></PRE>
<P><TT>CString m_szMsg;</TT> The constructor for <TT>CMenuView</TT> must initialize
the <TT>m_ptMsg</TT> variable. Edit the constructor for <TT>CMenuView</TT>, found
in <TT>MenuView.cpp</TT>, so it looks like the source code in Listing 10.3.
<H4><FONT COLOR="#000077">TYPE: Listing 10.3. The constructor for CMenuView.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>CMenuView::CMenuView()</TT>
<TT>{</TT>
<TT> m_ptMsg = CPoint(0,0);</TT>
</FONT></PRE>
<P><TT>}</TT> The <TT>CMenuView::OnDraw</TT> member function resembles the <TT>OnDraw</TT>
member function from <TT>CMouseTestView</TT> in Hour 8. Both functions use the <TT>TextOut</TT>
function to display a message at a certain point in the view. Edit the <TT>CMenuView::OnDraw</TT>
function so that it looks like the function provided in Listing 10.4. You must remove
a few lines of AppWizard-supplied code.
<H4><FONT COLOR="#000077">TYPE: Listing 10.4. The CMenuView::OnDraw member function.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CMenuView::OnDraw(CDC* pDC)</TT>
<TT>{</TT>
<TT> pDC->TextOut( m_ptMsg.x, m_ptMsg.y, m_szMsg );</TT>
</FONT></PRE>
<P><TT>}</TT>
<H3><FONT COLOR="#000077"><B>Trapping Messages</B></FONT></H3>
<P>Use ClassWizard to add four new message-handling functions to the <TT>CMenuView</TT>
class: three message-handling functions for the new menu items and one message-handling
function to detect the right-click from the mouse button. The steps used to add the
message-handling functions are similar to the ones used earlier when modifying an
existing menu, except these messages are handled by the <TT>CMenuView</TT> class.
<DL>
<DD>1. Open ClassWizard by pressing Ctrl+W or right-clicking in a source code window
and selecting ClassWizard from the menu.<BR>
<BR>
2. Select the Message Maps tab and select the class that will handle the message
from the Class Name combo box--in this case, <TT>CMenuView</TT>.<BR>
<BR>
3. Select the object that is generating the message from the Object ID list box--in
this case, use one of the values from Table 10.2.<BR>
<BR>
4. Select a message from the Messages list box and click the Add Function button.
Accept the default name suggested by ClassWizard for the function name.<BR>
<BR>
5. Repeat this process for all entries in Table 10.2.<BR>
<BR>
6. Click OK to close ClassWizard.
</DL>
<H4><FONT COLOR="#000077">Table 10.2. Values used to create message-handling functions.</FONT></H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><B>Object ID</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Message</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Function</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>CMenuView</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>WM_CONTEXTMENU</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>OnContextMenu</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>ID_LIONS</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>COMMAND</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>OnLions</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>ID_TIGERS</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>COMMAND</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>OnTigers</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>ID_BEARS</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>COMMAND</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>OnBears</TT></TD>
</TR>
</TABLE>
</P>
<P>The source code for the <TT>CMenuView::OnContextMenu</TT> message-handling function
is provided in Listing 10.5.
<H4><FONT COLOR="#000077">TYPE: Listing 10.5. Popping up a menu when a right mouse
button is clicked.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CMenuView::OnContextMenu(UINT nFlags, CPoint point)</TT>
<TT>{</TT>
<TT> CMenu zooMenu;</TT>
<TT> // Store popup point, and convert to client coordinates</TT>
<TT> // for the drawing functions.</TT>
<TT> m_ptMsg = point;</TT>
<TT> ScreenToClient( &m_ptMsg );</TT>
<TT> zooMenu.LoadMenu( ID_POPUP );</TT>
<TT> CMenu* pPopup = zooMenu.GetSubMenu( 0 );</TT>
<TT> pPopup->TrackPopupMenu( TPM_LEFTALIGN|TPM_RIGHTBUTTON,</TT>
<TT> point.x,</TT>
<TT> point.y,</TT>
<TT> this );</TT>
</FONT></PRE>
<P><TT>}</TT> When a right mouse click is detected, the <TT>WM_CONTEXTMENU</TT> message
is sent to the application and the MFC framework calls the <TT>OnContextMenu</TT>
message handler. The <TT>OnContextMenu</TT> function creates a <TT>CMenu</TT> object
and loads the <TT>ID_POPUP</TT> menu resource. The floating menu is displayed by
calling <TT>GetSubMenu</TT> and <TT>TrackPopupMenu</TT>.</P>
<P>The <TT>GetSubMenu</TT> function is used to skip past the dummy menu item at the
top of the <TT>ID_POPUP</TT> menu resource. The <TT>GetSubMenu</TT> function returns
a temporary pointer to the pop-up menu. Calling <TT>TrackPopupMenu</TT> causes the
pop-up menu to be displayed and the menu item selection to automatically follow the
mouse cursor.</P>
<P>The source code for handling menu selection messages sent to the <TT>CMenuView</TT>
class is provided in Listing 10.6.
<H4><FONT COLOR="#000077">TYPE: Listing 10.6. Message-handling functions for floating
menu items.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CMenuView::OnLions()</TT>
<TT>{</TT>
<TT> m_szMsg = "Lions are out";</TT>
<TT> InvalidateRect( NULL );</TT>
<TT>}</TT>
<TT>void CMenuView::OnTigers()</TT>
<TT>{</TT>
<TT> m_szMsg = "Tigers are afoot";</TT>
<TT> InvalidateRect( NULL );</TT>
<TT>}</TT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -