Winform中在Form上截取消息的两种方法

王朝system·作者佚名  2006-04-10
窄屏简体版  字體: |||超大  

原文见:Winform中在Form上截取消息的两种方法

比较常用的是重载Form的DefWndProc方法,例如截取鼠标按下的消息:

protected override void DefWndProc(ref Message m)

{

if ( m.Msg == 0x0201 )

{

MessageBox.Show(m.Msg.ToString());

}

else

{

base.DefWndProc (ref m);

}

}

还可以通过另一种办法,使用IMessageFilter 接口:

public class MessageFilter : IMessageFilter

{

public bool PreFilterMessage(ref Message m)

{

if (m.Msg == 0x0201)

{

MessageBox.Show("WM_LBUTTONDOWN is: " + m.Msg);

return true;

}

return false;

}

}

然后使用Application.AddMessageFilter方法,例如:

private static MessageFilter msgFliter = new MessageFilter();

在Main方法中注册消息筛选器:

Application.AddMessageFilter(msgFliter);

如果要取消注册,可以调用Application.RemoveMessageFilter方法

在这里有一个Windows的MessageID的枚举,挺有用的,好几次都忘了地址,这次写在这里好好保存

Windows Message ID constants

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有  導航