THE EVENT-HANDLER DECLARATIONS IN Day11Doc.h.
.
.
.
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 OnUpdateColorUI(CCmdUI* pCmdUI);
12: //{{AFX_MSG(CDay11Doc)
13: // NOTE - the ClassWizard will add and remove member functions Âhere.
14: // do
NOT EDIT what you see in these blocks of generated Âcode !
15: //}}AFX_MSG
16: DECLARE_MESSAGE_MAP()
17: private:
18: UINT m_nColor;
19: CObArray m_oaLines;
20: };
4. Open the Day11Doc.cpp source-code file.
5. 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.
LISTING 11.2. THE EVENT-HANDLER MESSAGE MAP ENTRIES IN Day11Doc.cpp.
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_MAP
13: END_MESSAGE_MAP()
14:
15: const COLORREF CDay11Doc::m_crColors[8] = {
16: RGB( 0, 0, 0), // Black
17: RGB( 0, 0, 255), // Blue
18: .
19: .
20: .
6. Scroll to the bottom of the file and add the two event message handler functions in Listing 11.3.
LISTING 11.3. THE COLOR MENU EVENT-HANDLER FUNCTIONS.
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 checked
10: pCmdUI->SetCheck(GetColor() == pCmdUI->m_nID ? 1 : 0);
11: }
In Listing 11.1, the two function declarations that you added are specified as event message handlers by the afx_msg function type declarations. These type of function declarations need to have protected access. Otherwise, they are virtually identical to any other class member function declaration.
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 Wizarddo
es not support or understand them. 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 name that should be called for the event message. These new message map entries function in the same way, but they have two event ID arguments instead of one. The two event ID arguments mark the two ends of a range of event IDs that should be passed to the function specified. These two event IDs should be the first and last menu entries you created when building the color menu.
--------------------------------------------------------------------------------
NOTE: 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.