📄 apb.htm
字号:
as &Width. Close the properties dialog. <P> <DT></DT> <DD><B>18. </B>Add submenu entries below the Width top-level menu. Specify the submenus in order, setting their properties as specified in Table B.1. <P></DL><H4>TABLE B.1. MENU PROPERTY SETTINGS.</H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"><I>Object</I></TD> <TD ALIGN="LEFT"><I>Property</I></TD> <TD ALIGN="LEFT"><I>Setting</I></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Menu Entry</TD> <TD ALIGN="LEFT">ID</TD> <TD ALIGN="LEFT">ID_WIDTH_VTHIN</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT">Caption</TD> <TD ALIGN="LEFT">&Very Thin</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Menu Entry</TD> <TD ALIGN="LEFT">ID</TD> <TD ALIGN="LEFT">ID_WIDTH_THIN</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT">Caption</TD> <TD ALIGN="LEFT">Thi&n</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Menu Entry</TD> <TD ALIGN="LEFT">ID</TD> <TD ALIGN="LEFT">ID_WIDTH_MEDIUM</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT">Caption</TD> <TD ALIGN="LEFT">&Medium</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Menu Entry</TD> <TD ALIGN="LEFT">ID</TD> <TD ALIGN="LEFT">ID_WIDTH_THICK</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT">Caption</TD> <TD ALIGN="LEFT">Thic&k</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Menu Entry</TD> <TD ALIGN="LEFT">ID</TD> <TD ALIGN="LEFT">ID_WIDTH_VTHICK</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT">Caption</TD> <TD ALIGN="LEFT">Very &Thick</TD> </TR></TABLE></P><DL> <DT></DT> <DD><B>19. </B>Open the Class Wizard. Select the CDay10Doc in the Class Name combo box. <P> <DT></DT> <DD><B>20. </B>Add functions for both the COMMAND and UPDATE_COMMAND_UI event messages for all the width menu entries. <P> <DT></DT> <DD><B>21. </B>After you add the final menu entry function, click Edit Code. <P> <DT></DT> <DD><B>22. </B>Edit the Very Thin menu functions as in Listing B.15. <P></DL><H4>LISTING B.15. THE VERY THIN MENU FUNCTIONS.</H4><PRE> 1: void CDay10Doc::OnWidthVthin() 2: { 3: // TODO: Add your command handler code here 4: 5: /////////////////////// 6: // MY CODE STARTS HERE 7: /////////////////////// 8: 9: // Set the current width to Very Thin10: m_nColor = ID_WIDTH_VTHIN - ID_WIDTH_VTHIN;11: 12: ///////////////////////13: // MY CODE ENDS HERE14: ///////////////////////15: }16: 17: void CDay10Doc::OnUpdateWidthVthin(CCmdUI* pCmdUI)18: {19: // TODO: Add your command update UI handler code here20: 21: ///////////////////////22: // MY CODE STARTS HERE23: ///////////////////////24: 25: // Determine if the Very Thin menu entry should be checked26: pCmdUI->SetCheck(GetWidth() == ID_WIDTH_VTHIN ? 1 : 0);27: 28: ///////////////////////29: // MY CODE ENDS HERE30: ///////////////////////</PRE><PRE>31: }</PRE><DL> <DT></DT> <DD><B>23. </B>Edit the Thin menu functions as in Listing B.16. Edit the remaining menu functions in the same way, substituting their menu IDs for ID_WIDTH_THIN. <P></DL><H4>LISTING B.16. THE THIN MENU FUNCTIONS.</H4><PRE> 1: void CDay10Doc::OnWidthThin() 2: { 3: // TODO: Add your command handler code here 4: 5: /////////////////////// 6: // MY CODE STARTS HERE 7: /////////////////////// 8: 9: // Set the current width to Thin10: m_nColor = ID_WIDTH_THIN - ID_WIDTH_VTHIN;11: 12: ///////////////////////13: // MY CODE ENDS HERE14: ///////////////////////15: }16: 17: void CDay10Doc::OnUpdateWidthThin(CCmdUI* pCmdUI)18: {19: // TODO: Add your command update UI handler code here20: 21: ///////////////////////22: // MY CODE STARTS HERE23: ///////////////////////24: 25: // Determine if the Thin menu entry should be checked26: pCmdUI->SetCheck(GetWidth() == ID_WIDTH_THIN ? 1 : 0);27: 28: ///////////////////////29: // MY CODE ENDS HERE30: ///////////////////////</PRE><PRE>31: }</PRE><P><H2><A NAME="Heading31"></A>Day 11</H2><H3>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>The CWinApp-derived class, the CMDIFrameWnd-derived class, the CMDIChildWnd-derived class, the CDocument-derived class, and the CView-derived class. <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>The Class Wizard doesn't understand the ON_COMMAND_RANGE message map entry and thus would either remove or corrupt it. <P> <DT></DT> <DD><B>3. </B>What argument does ON_COMMAND_RANGE pass to the event function? <P> <DT></DT> <DD>The ID of the event message. <P> <DT></DT> <DD><B>4. </B>What event message should you use to display a pop-up menu? <P> <DT></DT> <DD>WM_CONTEXTMENU. <P></DL><H3>Exercise</H3><P>Add the pull-down and context menus for the width, using the same pen widths asyesterday.</P><P>Follow these steps:</P><DL> <DT></DT> <DD><B>1. </B>Add the Width handling code as in yesterday's exercise. <P> <DT></DT> <DD><B>2. </B>Add the Width menu entries using the same settings as yesterday. <P> <DT></DT> <DD><B>3. </B>Open the Day11Doc.h header file. <P> <DT></DT> <DD><B>4. </B>Scroll down toward the bottom of the header file until you find the protected section where the AFX_MSG message map is declared (search for //{{AFX_MSG(CDay11Doc)). <P> <DT></DT> <DD><B>5. </B>Add the function declarations in Listing B.17 before the line that you searched for. (The string that you searched for is the beginning marker for the Class Wizard maintained message map. Anything you place between it and the end marker, //}}AFX_MSG, is likely to be removed or corrupted by the Class Wizard.) <P></DL><H4>LISTING B.17. THE EVENT-HANDLER DECLARATIONS IN DayllDoc.H.</H4><PRE>... 1: #ifdef _DEBUG 2: virtual void AssertValid() const; 3: virtual void Dump(CDumpContext& dc) const; 4: #endif 5: 6: protected: 7: 8: // Generated message map functions 9: protected:10: afx_msg void OnColorCommand(UINT nID);11: afx_msg void OnWidthCommand(UINT nID);12: afx_msg void OnUpdateColorUI(CCmdUI* pCmdUI);13: afx_msg void OnUpdateWidthUI(CCmdUI* pCmdUI);14: //{{AFX_MSG(CDay11Doc)15: // NOTE - the ClassWizard will add and remove member functions //here.16: // DO NOT EDIT what you see in these blocks of generated //code !17: //}}AFX_MSG18: DECLARE_MESSAGE_MAP()19: private:20: UINT m_nColor;21: CObArray m_oaLines;</PRE><PRE>22: };</PRE><DL> <DT></DT> <DD><B>6. </B>Open the Day11Doc.cpp source-code file. <P> <DT></DT> <DD><B>7. </B>Search for the line BEGIN_MESSAGE_MAP and add the lines in Listing B.18 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 B.18. 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_COMMAND_RANGE(ID_WIDTH_VTHIN, ID_WIDTH_VTHICK, OnWidthCommand) 9: ON_UPDATE_COMMAND_UI_RANGE(ID_COLOR_BLACK, ID_COLOR_WHITE, ÂOnUpdateColorUI)10: ON_UPDATE_COMMAND_UI_RANGE(ID_WIDTH_VTHIN, ID_WIDTH_VTHICK, ÂOnUpdateWidthUI)11: //{{AFX_MSG_MAP(CDay11Doc)12: // NOTE - the ClassWizard will add and remove mapping macros //here.13: // DO NOT EDIT what you see in these blocks of generated //code!14: //}}AFX_MSG_MAP15: END_MESSAGE_MAP()16: 17: const COLORREF CDay11Doc::m_crColors[8] = {18: RGB( 0, 0, 0), // Black19: RGB( 0, 0, 255), // Blue..</PRE><P>.</P><DL> <DT></DT> <DD><B>8. </B>Scroll to the bottom of the file and add the two event message handler functions in Listing B.19. <P></DL><H4>LISTING B.19. THE WIDTH MENU EVENT HANDLER FUNCTIONS.</H4><PRE> 1: void CDay11Doc::OnWidthCommand(UINT nID) 2: { 3: // Set the current width 4: m_nWidth = nID - ID_WIDTH_VTHIN; 5: } 6: 7: void CDay11Doc::OnUpdateWidthUI(CCmdUI* pCmdUI) 8: { 9: // Determine if the menu entry should be checked10: pCmdUI->SetCheck(GetWidth() == pCmdUI->m_nID ? 1 : 0);</PRE><PRE>11: }</PRE><DL> <DT></DT> <DD><B>9. </B>Open the IDR_CONTEXTMENU in the Menu Designer. <P> <DT></DT> <DD><B>10. </B>In the Width cascading menu, add the width menu entries just like 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. <P></DL><H2><A NAME="Heading34"></A>Day 12</H2><H3>Quiz</H3><DL> <DT></DT> <DD><B>1. </B>How do you tie a toolbar button to a menu entry that triggers that same function? <P> <DT></DT> <DD>Give the toolbar button the same object ID as the menu entry. <P> <DT></DT> <DD><B>2. </B>How do you make sure that a toolbar can be docked with the frame window? <P> <DT></DT> <DD>Both must have docking enabled on the same sides (using the EnableDocking function) in the OnCreate function of the frame class. <P> <DT></DT> <DD><B>3. </B>How can you remove the Num Lock status indicator from the status bar? <P> <DT></DT> <DD>Remove the ID_INDICATOR_NUM from the indicators table near the top of the main frame source code file. <P> <DT></DT> <DD><B>4. </B>Why do you have to edit the resource file to add a combo box to a toolbar? <P> <DT></DT> <DD>You need to add a separator to the toolbar as a placeholder in the toolbar. The toolbar designer will do its best to prevent you from adding the separators, assuming that they are a mistake. <P></DL><H3>Exercises</H3><DL> <DT></DT> <DD><B>1. </B>Add another pane to the status bar to display the current width selected. <P> <DT></DT> <DD>Add an entry to the strings table with an ID of ID_INDICATOR_WIDTH and a caption of VERY THICK. <P> <DT></DT> <DD>Add another entry to the status bar indicators table at the beginning of CMainFrame.cpp: <P></DL><PRE>static UINT indicators[] ={ ID_SEPARATOR, // status line indicator ID_INDICATOR_WIDTH, ID_INDICATOR_COLOR, ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL,</PRE><PRE>};</PRE><DL> <DT></DT> <DD>Add a new member function to the CToolbarDoc class. Specify the function type as afx_msg void, the function definition as OnUpdateIndicatorWidth (CCmdUI *pCmdUI), and the access as protected. Edit the function as follows: <P></DL><PRE>void CToolbarDoc::OnUpdateIndicatorWidth(CCmdUI *pCmdUI){ CString strWidth; // What is the current width? switch (m_nWidth) { case 0: // Very Thin strWidth = "VERY THIN"; break; case 1: // Thin strWidth = "THIN"; break; case 2: // Medium strWidth = "MEDIUM"; break; case 3: // Thick strWidth = "THICK"; break; case 4: // Very Thick strWidth = "VERY THICK"; break; } // Enable the status bar pane pCmdUI->Enable(TRUE); // Set the text of the status bar pane // to the current width pCmdUI->SetText(strWidth);</PRE><PRE>}</PRE><DL> <DT></DT> <DD>Edit the CToolbarDoc message map, adding the ON_UPDATE_COMMAND_UI message handler entry as follows: <P></DL><PRE>////////////////////////////////////////////////////////////////// CToolbarDocIMPLEMENT_DYNCREATE(CToolbarDoc, CDocument)BEGIN_MESSAGE_MAP(CToolbarDoc, CDocument) ON_UPDATE_COMMAND_UI(ID_INDICATOR_WIDTH,ÂOnUpdateIndicatorWidth) ON_UPDATE_COMMAND_UI(ID_INDICATOR_COLOR,ÂOnUpdateIndicatorColor) //{{AFX_MSG_MAP(CToolbarDoc) ON_UPDATE_COMMAND_UI(ID_WIDTH_VTHIN, OnUpdateWidthVthin).. ON_COMMAND(ID_WIDTH_VTHIN, OnWidthVthin) //}}AFX_MSG_MAP</PRE><PRE>END_MESSAGE_MAP()</PRE><DL> <DT></DT> <DD><B>2. </B>Add a button to the main toolbar that can be used to toggle the color toolbar on and off, as in Figure 12.7. <P> <DT></DT> <DD>Open the IDR_MAINFRAME toolbar in the toolbar designer. Paint an icon for the blank button at the end of the toolbar. Double-click the button to open its
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -