c# 如何使Form的内部控件(Control)响应整个Form的按键消息(Event) KeyWord: IMessageFilter
使控件继承IMessageFilter, 随后在IMessageFilter.PreFilterMessage中可以处理WM_KEYDOWN 这类Windows消息
代码类似如下:
bool IMessageFilter.PreFilterMessage(ref Message m)
{
if (m.Msg == (int)norlib.Native.WM.WM_KEYDOWN && m.WParam == (IntPtr)0x11)//0x11 VK_CONTROL
{
_controling = false;
}
else if (m.Msg == (int)norlib.Native.WM.WM_KEYUP && m.WParam == (IntPtr)0x11)//0x11 VK_CONTROL
{
_controling = true;
}
return false;
}
最后在控件的构造函数中调用: Application.AddMessageFilter(this);
代码类似如下:
public PriceLadderTractorControl()
{
InitializeComponent();
Application.AddMessageFilter(this);
......
}