1、首先两者都是虚函数
CWnd::OnCommand,CCmdTarget::OnCmdMsg
2、看源码
BOOL CWnd::OnCommand(WPARAM wParam, LPARAM lParam)
// return TRUE if command invocation was attempted
{
UINT nID = LOWORD(wParam);
HWND hWndCtrl = (HWND)lParam;
int nCode = HIWORD(wParam);
// default routing for command messages (through closure table)
if (hWndCtrl == NULL)
{
// zero IDs for normal commands are not allowed
if (nID == 0)
return FALSE;
// make sure command has not become disabled before routing
CTestCmdUI state;
state.m_nID = nID;
OnCmdMsg(nID, CN_UPDATE_COMMAND_UI, &state, NULL);
if (!state.m_bEnabled)
{
TRACE1("Warning: not executing disabled command %d\n", nID);
return TRUE;
}
// menu or accelerator
nCode = CN_COMMAND;
}
else
{
// control notification
ASSERT(nID == 0 || ::IsWindow(hWndCtrl));
if (_afxThreadState->m_hLockoutNotifyWindow == m_hWnd)
return TRUE; // locked out - ignore control notification
// reflect notification to child window control
if (ReflectLastMsg(hWndCtrl))
return TRUE; // eaten by child
// zero IDs for normal commands are not allowed
if (nID == 0)
return FALSE;
}
#ifdef _DEBUG
if (nCode < 0 && nCode != (int)0x8000)
TRACE1("Implementation Warning: control notification = $%X.\n",
nCode);
#endif
return OnCmdMsg(nID, nCode, NULL, NULL);
}
其中:OnCommand中调用OnCmdMsg。
BOOL CCmdTarget::OnCmdMsg(UINT nID, int nCode, void* pExtra,
AFX_CMDHANDLERINFO* pHandlerInfo)
{
// 省略
return FALSE; // not handled
}
作用:
OnCmdMsg :
Called by the framework to route and dispatch command messages and to handle the update of command user-interface objects.
调用框架路由和调度命令消息和处理更新用户界面对象的命令。
The framework calls this member function when the user selects an item from a menu, when a child control sends a notification message, or when an accelerator keystroke is translated.
该框架调用该成员函数当用户选择一个项目从一个菜单,当一个孩子控制发送一个通知消息,或者当一个加速器击键是翻译。
BOOL CToolTipDlg::OnCommand(WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
assert(wParam);
switch(lParam)
{
case WM_RBUTTONDOWN:
AfxGetApp()->m_pMainWnd->ShowWindow(SW_SHOW);
break;
}
return CDialog::OnCommand(wParam, lParam);
}
响应WM_COMMAND消息 做出相应 的处理
一个项目中使用的代码
BOOL CGameControl::OnCommand(WPARAM wParam, LPARAM lParam)
{
int id = LOWORD(wParam); //IDC_CONTROL_HALL+m_gameSerId.RoomID
CMainFrame *pMainFrame = theApp.GetMainFrame();
if(HIWORD(wParam)==BN_DOUBLECLICKED)
{
if(id == DLGBOTTOM_ID_ROOM) // 按键ID
{
if(m_gameSerId.RoomID>0)
pMainFrame->PostMessage(WM_GAME_CONTROL,ROOMSVR,1);
}
}
return CDialog::OnCommand(wParam, lParam);
其中相关:
Message Source | wParam (high word) | wParam (low word) | lParam |
---|---|---|---|
Menu | 0 | Menu identifier (IDM_*) | 0 |
Accelerator | 1 | Accelerator identifier (IDM_*) | 0 |
Control | Control-defined notification code | Control identifier | Handle to the control window |