📄 ch11.htm
字号:
<P> <DT></DT> <DD><B>5. </B>Search for the line BEGIN_MESSAGE_MAP and add the lines in Listing 11.2 just after it. It's important that this code be between the BEGIN_MESSAGE_MAP line and the //{{AFX_MSG_MAP line. If these commands are between the //{{AFX_MSG_MAP and //}}AFX_MSG_MAP lines, then the Class Wizard will remove or corrupt them. <P></DL><H4>LISTING 11.2. THE EVENT-HANDLER MESSAGE MAP ENTRIES IN Day11Doc.cpp.</H4><PRE> 1: ////////////////////////////////////////////////////////////////////// 2: // CDay11Doc 3: 4: IMPLEMENT_DYNCREATE(CDay11Doc, CDocument) 5: 6: BEGIN_MESSAGE_MAP(CDay11Doc, CDocument) 7: ON_COMMAND_RANGE(ID_COLOR_BLACK, ID_COLOR_WHITE, OnColorCommand) 8: ON_UPDATE_COMMAND_UI_RANGE(ID_COLOR_BLACK, ID_COLOR_WHITE, ÂOnUpdateColorUI) 9: //{{AFX_MSG_MAP(CDay11Doc)10: // NOTE - the ClassWizard will add and remove mapping macros Âhere.11: // DO NOT EDIT what you see in these blocks of generated Âcode!12: //}}AFX_MSG_MAP13: END_MESSAGE_MAP()14: 15: const COLORREF CDay11Doc::m_crColors[8] = {16: RGB( 0, 0, 0), // Black17: RGB( 0, 0, 255), // Blue18: .19: .</PRE><PRE>20: .</PRE><P><DL> <DT></DT> <DD><B>6. </B>Scroll to the bottom of the file and add the two event message handler functions in Listing 11.3. <P></DL><H4>LISTING 11.3. THE COLOR MENU EVENT-HANDLER FUNCTIONS.</H4><PRE> 1: void CDay11Doc::OnColorCommand(UINT nID) 2: { 3: // Set the current color 4: m_nColor = nID - ID_COLOR_BLACK; 5: } 6: 7: void CDay11Doc::OnUpdateColorUI(CCmdUI* pCmdUI) 8: { 9: // Determine if the menu entry should be checked10: pCmdUI->SetCheck(GetColor() == pCmdUI->m_nID ? 1 : 0);</PRE><PRE>11: }</PRE><P>In Listing 11.1, the two function declarations that you added are specified asevent message handlers by the afx_msg function type declarations. These type of functiondeclarations need to have protected access. Otherwise, they are virtually identicalto any other class member function declaration.</P><P>In Listing 11.2, the two message map entries, ON_COMMAND_RANGE and ON_UPDATE_COMMAND_UI_RANGE,are standard message map entries, but the Class Wizard does not support or understandthem. If you examine the message map entries from the previous day's applications,you will notice that there are ON_COMMAND and ON_UPDATE_COMMAND_UI message map entries.These macros have two arguments, the message ID and the event-handler function namethat should be called for the event message. These new message map entries functionin the same way, but they have two event ID arguments instead of one. The two eventID arguments mark the two ends of a range of event IDs that should be passed to thefunction specified. These two event IDs should be the first and last menu entriesyou created when building the color menu.</P><BLOCKQUOTE> <P><HR><STRONG>NOTE:</STRONG> The message map is a mechanism used by Visual C++ and MFC to easily specify event messages and the functions that should be called to handle the event. These message-map commands are converted by the Visual C++ compiler into a fast and efficient map for calling the appropriate event functions when a message is received by the application. Whenever you add a function through the Class Wizard, you are not only adding the function to the code, but you are also adding an entry into the message map for that class.<HR></BLOCKQUOTE><P>When you use the ON_COMMAND_RANGE message-map entry, the event message ID is automaticallypassed as an argument to the event-handler function. This allows you to create thefunction in Listing 11.3 to handle the color selection event messages. If you compileand run your application at this point, you should find that the color selectionfunctionality is all working just as it did yesterday, as shown in Figure 11.3.</P><P><A HREF="javascript:popUp('11fig03.gif')"><B>FIGURE 11.3.</B></A><B> </B><I>Runningthe MDI application.</I></P><P><I></I><H2><A NAME="Heading6"></A>Adding a Context Menu</H2><P>In most Windows applications, you can right-click the mouse and what is knownas a context menu, or pop-up menu, appears. Back on Day 6, "Creating Menus forYour Application," you implemented a simple pop-up menu. However, there is amechanism for creating and using these context menus when Windows thinks that themenu should be opened. This process allows you to add context menus that behave moreconsistently with other Windows applications (and if Microsoft changes how the contextmenus are triggered with a new version of Windows, yours will still behave accordingto the Windows standard).</P><P>An event message WM_CONTEXTMENU is passed to the event queue when the right mousebutton is released or when the context menu button is pressed (if you have a newerWindows-enabled keyboard with the context menu button). If you place an event-handlerfunction on the WM_CONTEXTMENU event message, you can display a pop-up menu withconfidence that you are showing it at the appropriate time.</P><P>To add the context menu to your application, you create a new menu for use asthe context menu. To do this, follow these steps:</P><P><DL> <DT></DT> <DD><B>1. </B>In the Resource View tab on the workspace pane, right-click the Menu folder. <P> <DT></DT> <DD><B>2. </B>Select Insert Menu from the pop-up menu (or should I say context menu). <P> <DT></DT> <DD><B>3. </B>Select the new menu (still in the workspace pane), open its properties dialog, and name the menu IDR_CONTEXTMENU. <P> <DT></DT> <DD><B>4. </B>In the Menu Designer, specify the top-level menu caption as a single space. This causes Visual C++ to add the first entry in the drop-down portion of the menu. <P> <DT></DT> <DD><B>5. </B>In the first drop-down menu entry, specify the caption as &Width and check the Pop-up check box. (This causes the ID combo box to be disabled and an arrow to display beside the caption, along with another menu entry to the right of the menu entry you are modifying.) <P> <DT></DT> <DD><B>6. </B>Do not add any menu entries into the Width cascading menu at this time (that is left for an exercise at the end of the chapter). Instead, select the menu entry below the Width entry and open its properties dialog. Specify the caption as &Colors and check the Pop-up check box. <P> <DT></DT> <DD><B>7. </B>In the colors cascading menu, add the color menu entries as you did for the IDR_DAY11TYPE menu, using the same property settings. You can select the ID from the drop-down list of IDs, if you would rather search for them instead of type. When you finish, your menu should look like the one in Figure 11.4. <P> <DT></DT> <DD><B>8. </B>Select the Class View tab in the workspace pane. <P> <DT></DT> <DD><B>9. </B>Select the CDay11View class. Open the Class Wizard by selecting View|ClassWizard from the menu. <P></DL><P><A HREF="javascript:popUp('11fig04.gif')"><B>FIGURE 11.4.</B></A><B> </B><I>Thecontext menu design.</I></P><P><I></I><DL> <DT><I></I></DT> <DD><B>10. </B>Add a function for the WM_CONTEXTMENU event message on the CDay11View class. <P> <DT></DT> <DD><B>11. </B>Edit the function, adding the code in Listing 11.4. <P></DL><H4>LISTING 11.4. THE CDay11View OnContextMenu FUNCTION.</H4><PRE> 1: void CDay11View::OnContextMenu(CWnd* pWnd, CPoint point) 2: { 3: // TODO: Add your message handler code here 4: 5: /////////////////////// 6: // MY CODE STARTS HERE 7: /////////////////////// 8: 9: CMenu menu;10: 11: // Load the context menu12: menu.LoadMenu(IDR_CONTEXTMENU);13: // Get the first sub menu (the real menu)14: CMenu *pContextMenu = menu.GetSubMenu(0);15: 16: // Display the context menu for the user17: pContextMenu->TrackPopupMenu(TPM_LEFTALIGN | 18: TPM_LEFTBUTTON | TPM_RIGHTBUTTON,19: point.x, point.y, AfxGetMainWnd());20: 21: ///////////////////////22: // MY CODE ENDS HERE23: ///////////////////////</PRE><PRE>24: }</PRE><P>This code should all look familiar to you from what you learned on Day 6. If youcompile and run your application now, you should be able to click your right mousebutton on the child window and change your drawing color from the context menu thatopened, as shown in Figure 11.5.</P><P><A HREF="javascript:popUp('11fig05.gif')"><B>FIGURE 11.5.</B></A><B> </B><I>Usingthe context menu to change drawing colors.</I></P><P><I></I><H2><A NAME="Heading7"></A>Summary</H2><P>That wasn't too bad; was it? After yesterday, you probably needed the easy daytoday, along with all the review of what you did yesterday to help it all sink in.But you did get to learn some new things today. You learned about MDI applications,what they are, and how they differ from SDI applications. You learned how you couldtake a series of menus and use a single event-handler function for all of them. Youalso learned how you can create a menu specifically for use as a pop-up context menuand how you can integrate it into an MDI application.</P><P><H2><A NAME="Heading8"></A>Q&A</H2><DL> <DT></DT> <DD><B>Q Because it's basically the same code to create an MDI or SDI application, why would I want to create an SDI application? Why wouldn't I want to make all my applications MDI applications?</B> <P> <DT><B></B></DT> <DD><B>A</B> It depends on the application and how it's going to be used. You probably use both types of applications on a daily basis. If you are writing a memo or working on a spreadsheet, you are probably using an MDI application. If you are browsing the World Wide Web, your Web browser is most likely an SDI application. A simple text editor such as Notepad would probably be more difficult for the user as an MDI style application, but as an SDI application, it's just about right (for the task it handles). Certain applications make more sense implemented as an SDI application than as an MDI application. You need to think through how your application is going to be used and determine which model it's more suited for. <P> <DT></DT> <DD><B>Q Some entries on my color menu are changing to the wrong color. How can I determine the problem?</B> <P> <DT><B></B></DT> <DD><B>A</B> The problem is that the color menu IDs are probably not in sequential order or are out of order. You can check them by right-clicking on the Day11 resources in the Resource View tab of the workspace pane. Select Resource Symbols from the pop-up menu to display a list of the IDs and the numbers assigned to them in alphabetical order. Start with the Black ID and make sure that the numbers increase by 1 without skipping any numbers. Be sure to check these IDs in the order that the colors appear on the menu (and in the color table in the Day11Doc.cpp file), not in the alphabetical order in which they are displayed in this list. If you find some errors, you have to close Visual C++ and open the Resource.h file in a text editor to renumber the IDs correctly. Once you make the corrections (be sure to delete any duplicates), save your corrections, restart Visual C++, and recompile your application. The color menu should work correctly. <P></DL><H2><A NAME="Heading9"></A>Workshop</H2><P>The Workshop provides quiz questions to help you solidify your understanding ofthe material covered and exercises to provide you with experience in using what you'velearned. The answers to the quiz questions and exercises are provided in AppendixB, "Answers."</P><P><H3><A NAME="Heading10"></A>Quiz</H3><DL> <DT></DT> <DD><B>1. </B>What are the five base classes that are used in MDI applications? <P> <DT></DT> <DD><B>2. </B>Why do you have to place the ON_COMMAND_RANGE message map entry outside the section maintained by the Class Wizard? <P> <DT></DT> <DD><B>3. </B>What argument does ON_COMMAND_RANGE pass to the event function? <P> <DT></DT> <DD><B>4. </B>What event message should you use to display a pop-up menu? <P></DL><H3><A NAME="Heading11"></A>Exercise</H3><P>Add the pull-down and context menus for the width, using the same pen widths asyesterday.</P><H1></H1><CENTER><P><HR><A HREF="../ch10/ch10.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch12/ch12.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> <BR><BR></P><P>© <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. Allrights reserved.</CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -