比较常用的是重载Form的DefWndProc方法,例如截取鼠标按下的消息:
protected override void DefWndProc(ref Message m)
{
if ( m.Msg == 0x0201 )
{
MessageBox.Show(m.Msg.ToString());
}
else
{
base.DefWndProc (ref m);
}
}
还可以通过另一种办法,使用IMessageFilter 接口:
private static MessageFilter msgFliter = new MessageFilter();
在Main方法中注册消息筛选器:
Application.AddMessageFilter(msgFliter);
如果要取消注册,可以调用Application.RemoveMessageFilter方法
在这里有一个Windows的MessageID的枚举,挺有用的,好几次都忘了地址,这次写在这里好好保存
Windows Message ID constants
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
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方法,例如:{
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == 0x0201)
{
MessageBox.Show("WM_LBUTTONDOWN is: " + m.Msg);
return true;
}
return false;
}
}
private static MessageFilter msgFliter = new MessageFilter();
在Main方法中注册消息筛选器:
Application.AddMessageFilter(msgFliter);
如果要取消注册,可以调用Application.RemoveMessageFilter方法
在这里有一个Windows的MessageID的枚举,挺有用的,好几次都忘了地址,这次写在这里好好保存
Windows Message ID constants