ON_COMMAND_RANGE用法请教!--我靠,这种问题都没人敢答??? (200分)

  • 主题发起人 主题发起人 savenight
  • 开始时间 开始时间
S

savenight

Unregistered / Unconfirmed
GUEST, unregistred user!
ON_COMMAND_RANGE(IDC_MIN, IDC_MAX, OnMyFunc)
把OnMyFunc声明成如下的形式1后,debug版的程序没有问题,release版就会出错。
OnMyFunc(WPARAM wparam, LPARAM lparam);
//★形式1★

当然我知道应该声明成如下的形式2:
OnMyFunc(UINT nID );
//★形式2★
-----------------
我的问题是出错的原因何在?
 
ON_COMMAND_RANGE
Use this macro to map a contiguous range of command IDs to a single message handler function.
ON_COMMAND_RANGE(id1, id2, memberFxn )
Parameters
id1
Command ID at the begin
ning of a contiguous range of command IDs.
id2
Command ID at the end of a contiguous range of command IDs.
memberFxn
The name of the message-handler function to which the commands are mapped.
Remarks
The range of IDs starts with id1 and ends with id2.
Use ON_COMMAND_RANGE to map a range of command IDs to one member function. Use ON_COMMAND to map a single command to a member function. Only one message-map entry can match a given command ID. That is, you can't map a command to more than one handler. For more information on mapping message ranges, see Handlers for Message-Map Ranges.
There is no automatic support for message map ranges, so you must place the macro yourself.
Example
// The code fragment below shows how to use ON_COMMAND_RANGE macro
// to map a contiguous range of command IDs to a single message
// handler function (i.e. OnFileMenuItems() is the sample below). In
// addition, it also shows how to use CheckMenuRadioItem() to check a
// selected menu item and makes it a radio item.
begin
_MESSAGE_MAP(CMainFrame, CFrameWnd)
// ...
ON_COMMAND_RANGE(ID_FILE_MENUITEM1, ID_FILE_MENUITEM3, OnFileMenuItems)
END_MESSAGE_MAP()
void CMainFrame::OnFileMenuItems(UINT nID)
{
CMenu* mmenu = GetMenu();
CMenu* submenu = mmenu->GetSubMenu(0);
submenu->CheckMenuRadioItem(ID_FILE_MENUITEM1, ID_FILE_MENUITEM3,
nID, MF_BYCOMMAND);
}
 
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.
 
duducat:我问的是原因,你贴的这一大段能解释吗?
 
谁能给个原因呀!唉,这年头。。。
 
踢什么踢,你的问题都有点莫明其妙
 
qiuyejun:
再看看我的问题,这次应该写的比较清楚了,您知道原因吗?
 
后退
顶部