OnCommand 与OnCmdMsg

来源:互联网 发布:淘宝助理打印 编辑:程序博客网 时间:2024/04/30 18:37

1、首先两者都是虚函数

CWnd::OnCommand,CCmdTarget::OnCmdMsg

2、看源码

[cpp] view plaincopyprint?
  1. BOOL CWnd::OnCommand(WPARAM wParam, LPARAM lParam)  
  2.     // return TRUE if command invocation was attempted  
  3. {  
  4.     UINT nID = LOWORD(wParam);  
  5.     HWND hWndCtrl = (HWND)lParam;  
  6.     int nCode = HIWORD(wParam);  
  7.   
  8.     // default routing for command messages (through closure table)  
  9.   
  10.     if (hWndCtrl == NULL)  
  11.     {  
  12.         // zero IDs for normal commands are not allowed  
  13.         if (nID == 0)  
  14.             return FALSE;  
  15.   
  16.         // make sure command has not become disabled before routing  
  17.         CTestCmdUI state;  
  18.         state.m_nID = nID;  
  19.         OnCmdMsg(nID, CN_UPDATE_COMMAND_UI, &state, NULL);  
  20.         if (!state.m_bEnabled)  
  21.         {  
  22.             TRACE1("Warning: not executing disabled command %d\n", nID);  
  23.             return TRUE;  
  24.         }  
  25.   
  26.         // menu or accelerator  
  27.         nCode = CN_COMMAND;  
  28.     }  
  29.     else  
  30.     {  
  31.         // control notification  
  32.         ASSERT(nID == 0 || ::IsWindow(hWndCtrl));  
  33.   
  34.         if (_afxThreadState->m_hLockoutNotifyWindow == m_hWnd)  
  35.             return TRUE;        // locked out - ignore control notification  
  36.   
  37.         // reflect notification to child window control  
  38.         if (ReflectLastMsg(hWndCtrl))  
  39.             return TRUE;    // eaten by child  
  40.   
  41.         // zero IDs for normal commands are not allowed  
  42.         if (nID == 0)  
  43.             return FALSE;  
  44.     }  
  45.   
  46. #ifdef _DEBUG  
  47.     if (nCode < 0 && nCode != (int)0x8000)  
  48.         TRACE1("Implementation Warning: control notification = $%X.\n",  
  49.             nCode);  
  50. #endif  
  51.   
  52.     return OnCmdMsg(nID, nCode, NULL, NULL);  
  53. }  

其中:OnCommand中调用OnCmdMsg。

[cpp] view plaincopyprint?
  1. BOOL CCmdTarget::OnCmdMsg(UINT nID, int nCode, void* pExtra,  
  2.     AFX_CMDHANDLERINFO* pHandlerInfo)  
  3. {  
  4. <span style="WHITE-SPACE: pre"> </span>// 省略  
  5.     return FALSE;   // not handled  
  6. }  
作用:

OnCmdMsg : 

Called by the framework to route and dispatch command messages and to handle the update of command user-interface objects. 

调用框架路由和调度命令消息和处理更新用户界面对象的命令。


OnCommand:   是响应WM_COMMAND消息

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. 

该框架调用该成员函数当用户选择一个项目从一个菜单,当一个孩子控制发送一个通知消息,或者当一个加速器击键是翻译。

[cpp] view plaincopyprint?
  1. BOOL CToolTipDlg::OnCommand(WPARAM wParam, LPARAM lParam)   
  2. {  
  3.     // TODO: Add your specialized code here and/or call the base class  
  4.     assert(wParam);  
  5.   
  6.     switch(lParam)  
  7.     {  
  8.     case WM_RBUTTONDOWN:  
  9.         AfxGetApp()->m_pMainWnd->ShowWindow(SW_SHOW);    
  10.         break;  
  11.     }  
  12.     return CDialog::OnCommand(wParam, lParam);  
  13. }  

响应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 SourcewParam (high word)wParam (low word)lParamMenu0Menu identifier (IDM_*)0Accelerator1Accelerator identifier (IDM_*)0ControlControl-defined notification codeControl identifierHandle to the control window
 


0 0