关于c#中的消息处理函数和vc中的消息处理函数区别

来源:互联网 发布:喜马拉雅录音软件下载 编辑:程序博客网 时间:2024/05/04 11:48
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

 从vc入门编程的,相信大家都很熟悉PreTranslateMessage和WindowProc两个函数,前者是预处理windows发给控件的消息,后者是处理剩余的控件消息。对于PreTranslateMessage函数,一般来说,我们是这样处理控件消息的:

BOOL test::PreTranslateMessage(MSG* pMsg) {if(pMsg->hwnd==GetSafeHwnd()){

if(pMsg->message==WM_MOUSEMOVE){m_tooltip.RelayEvent(pMsg);}} return CDialog::PreTranslateMessage(pMsg);}}

分为两层,先判断该消息是属于那个控件窗口的,再判断这个消息的类型。

对于windowproc函数,我们通常是这样处理控件消息的:LRESULT test::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) {if(message==WM_SYSCOMMAND)if(wParam==SC_RESTORE)wParam=SC_MAXIMIZE;

 return CDialog::WindowProc(message, wParam, lParam);}这个函数对控件消息的处理方法是先把消息发给当前控件,如果不处理,则发给其父窗口,如果其父窗口也不处理,则发给当前程序处理。

而在c#中,消息处理函数有所改变。对于PreProcessMessage函数:我们必须在需要预处理消息的控件中重载这个函数,而不能仅在父窗口重载这个函数。  public override bool PreProcessMessage(ref Message msg)  {      if(msg.Msg==0x101)   {         MessageBox.Show(msg.HWnd.ToString());   }   

   return base.PreProcessMessage(ref msg);    }可以看到,此函数第一步不再需要判断此消息是属于那个控件的,每个控件(包括父窗口本身),它只处理属于自己的消息,不再沿用vc中的消息机制,在某个消息发送到一个类前,允许其它类试着处理它。

 对于WndProc(跟vc中的windowproc名字稍有改变)函数来说:

 protected override void WndProc(ref Message msg)         {              if(msg.Msg==0x101)   {         MessageBox.Show(msg.HWnd.ToString());   }               base.WndProc(ref msg);        }

vc中的windowproc没有什么形式上的变化,只不过实质上,c#已不再采用消息发送顺序的机制,只要当前控件没有相应消息处理入口,则消息被丢弃。

   如果我们在c#里面想再实现在listbox控件中,点右键弹出菜单,就不能再用BOOL test::PreTranslateMessage(MSG* pMsg) { if(pMsg->hwnd==GetDlgItem(IDC_listbox1)->GetSafeHwnd()) {switch(pMsg->message) {

  case WM_LBUTTONUP:……

  } } }呵呵,那怎么办呢,^_^其实c#中,不再和vc那样只提供少数几种事件,c#中丰富的事件,已经不再经常需要程序员特制某种事件的消息处理函数,只要在listbox控件的mousedown响应函数中,区分出左右键,然后根据情况弹出不同的菜单即可!

    但比如你要自定义一个编辑框,可是想拦截某些特定的键(如delete),这时,c#丰富的事件,就不再有用了,必须要我们先自定义一个从textbox继承下来编辑框控件,然后在其preprocessmessage函数中预处理它!

public override bool PreProcessMessage(ref Message msg)  {   Keys keyCode=(Keys)(int)msg.WParam &Keys.KeyCode;   if(msg.Msg==0x101&&keyCode==Keys.Delete)   {    ……   }   

   return base.PreProcessMessage(ref msg);    }

剩余几点,我现在还没搞清楚:PreProcessMessage函数能处理的消息很有限:WM_KEYUP,WM_KEYDOWN之类的消息可以处理,可是WM_CLOSE,WM_LBUTTONDOWN等消息无法处理。但wndproc函数却可以处理它们! 不知道是微软故意屏蔽掉那些消息的预处理,还是有什么其它的蹊跷!

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击