MFC OnCommand和OnCmdMsg

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. 

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


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. 

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

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

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值