MFC利用CWnd::OnCommand提前处理消息

利用CWnd::OnCommand提前处理消息

  先说下CWnd::OnCommand的含义,引用自MSDN:
  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.
  virtual BOOL OnCommand(    WPARAM wParam,    LPARAM lParam
);
  此函数完成了对菜单、通告消或加速键的响应处理。

  为什么要提这个功能,因为在指定命令的响应函数时,是具体到某一个类中,比如在view类中增加一个对菜单项的响应函数,此时某个菜单项的响应和处理就是在view类的范围中进行处理的。
  中消息的路由中,只要中view类的上层中没有处理相应菜单项的函数,就会默认路由到view中,所以这就有了OnCommand这个函数的需求。可以在view类的上层进行动态的处理。
  下面具体举例如下:

 

 

BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
int MenuCmdId=LOWORD(wParam);//取出菜单的命令ID。The low-order word of wParam identifies the command ID of the menu item, control, or accelerator
CMenu2View *pView = (CMenu2View*)GetActiveView();//获得
if(MenuCmdId>=IDM_PHONE1 && MenuCmdId<IDM_PHONE1+pView->m_strArray.GetSize())
{
CClientDC dc(pView);
dc.TextOut(0,0,pView->m_strArray.GetAt(MenuCmdId-IDM_PHONE1));

return TRUE;
}
return CFrameWnd::OnCommand(wParam, lParam);
}

单击 Button 会同时引发 Command 和 Click 事件,
Command 事件通常用于与特定的命令名(如 Sort)关联时,可以在一个网页上创建多个 Button 控件,指定不同的 CommandName,然后在同一个事件处理程序中(OnCommand),分别处理不同 Button

<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>

<script runat="server">

void CommandBtn_Click(Object sender, CommandEventArgs e)
{

switch(e.CommandName)
{

case "Sort":

// Call the method to sort the list.
Sort_List((String)e.CommandArgument);
break;

case "Submit":

// Display a message for the Submit button being clicked.
Message.Text = "You clicked the Submit button";

// Test whether the command argument is an empty string ("").
if((String)e.CommandArgument == "")
{
// End the message.
Message.Text += ".";
}
else
{
// Display an error message for the command argument.
Message.Text += ", however the command argument is not recogized.";
}
break;

default:

// The command name is not recognized. Display an error message.
Message.Text = "Command name not recogized.";
break;

}

}

void Sort_List(string commandArgument)
{

switch(commandArgument)
{

case "Ascending":

// Insert code to sort the list in ascending order here.
Message.Text = "You clicked the Sort Ascending button.";
break;

case "Descending":

// Insert code to sort the list in descending order here.
Message.Text = "You clicked the Sort Descending button.";
break;

default:

// The command argument is not recognized. Display an error message.
Message.Text = "Command argument not recogized.";
break;

}

}

OnCommand是响应WM_COMMAND消息的,一般是响应控件和菜单的命令消息时使用。

在使用LRESULT CALLBACK WndProc(HWND hWnd,UINT nMsg, WPARAM wParam, LPARAM lParam) 时,有2个参数,“WParam”和“LParam”,这2个参数可以给我们消息处理所需要的重要信息,下面我们就要讲到这2个参数的名字的“来源”。
在以前,电脑还是16位的时候,每条消息能附带2个数据,就是“WParam”和“LParma”,“WParam”只是一个16位的数据,也就是“word”数据类型,所以我们叫它“W”;而“LParam”却是一个32位的数据,也就是“Long”数据类型,所以我们叫它“L”。当使用它们的时候,我们把句柄和整数传递给“WParam”,而把指针传递给“LParam”。
这是最早的时候我们这样给它们命名了,然后当电脑发展到32位时,“WParam”也升级到了32位,虽然“W”还是表示“word”,但是它已经不是word类型了。(tury注:查阅MSDN,在.net framework类库中,Message类的2个属性都被定义成了IntPtr类型,再看IntPtr类的介绍:类型被设计成整数,其大小适用于特定平台。即是说,此类型的实例在 32 位硬件和操作系统中将是 32 位,在 64 位硬件和操作系统上将是 64 位。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值