1. In some case, you can set the Form's keypreview = Ture.2. Override the ProcessCmdKey of the Form, for example:
 
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
if (keyData == Keys.Enter)
{
MessageBox.Show("Enter pressed");
return true;
}
return base.ProcessCmdKey(ref msg,keyData);
}

解决方案 »

  1.   

    要求是我的控件自身就具有这项功能,
    只要哪个Form加入了我的控件,就已经有此能力了,
    所以我只能对我的控件进行加工,不可以去修改Form的。多谢班兄,还请多指教!
      

  2.   

    能不能这样,在你的的控件中,加上对Parent的事件,如:
    Parent.KeyUp += new KeyEventHandler(Parent_KeyUp);private void Parent_KeyUp(object sender,KeyEventArgs e)
    {
       // 截获代码放在这
    }不过这个方法好像不能实现截获,只是将Parent按钮消息的实现,父窗口还是会处理这个按键的事件,只是讨论讨论 :)
      

  3.   

    如果用钩子,那么在哪里设置钩子(SetWindowsHookEx),又在哪里释放钩子(UnhookWindowsHookEx)?而且怎么接着把消息传给本来应该接收的控件?
      

  4.   

    use Hook http://expert.csdn.net/Expert/topic/1325/1325259.xml?temp=.3917047
      

  5.   

    比较正宗的方法就是Application.AddMessageFilter()这个方法,具体看MSDN.
    其实你回头想想Splitter不就是你的功能么,只不过它是鼠标事件你是键盘事件
      

  6.   

    public class MyFilter: IMessageFilter{
      public bool PreFilterMessage(ref Message m){
        if(m.msg == [keyDown]) DoSomething();
        return true;
      }
    }
    Application.AddFilterMessage(new MyFilter());
      

  7.   

    public class MyFilter: IMessageFilter{
    #region Implementation of IMessageFilter
    public bool PreFilterMessage(ref System.Windows.Forms.Message m) {
    if(m.Msg == 0x0100){
    MessageBox.Show("sdf");
    }
    return false;
    }

    #endregion
    }
    Application.AddMessageFilter(new MyFilter());
      

  8.   

    哦,return 个true 就可以了! 呵呵!!
      

  9.   

    顺便再问一下这个 m.Msg == 0x0100是怎么来的?
    如果是别的消息这个值应该是多少?
    还有如果按下的是组合键应该怎么判断?
      

  10.   

    0x0100是WM_KEYDOWN
    你可以去查一查API的相关资料组合我想至少有两种方法可以判断
    明天我把它贴出来
      

  11.   

    我就哪你前几天问的shift+alt+ctl来做示例吧
    另外说一下shift、ctrl、alt不会触发WM_KEYDOWN的消息的第一种:用static变量的方法
    static bool ShiftStat=false;
    static bool AltStat=false;
    static bool CtrlStat=false;public bool PreFilterMessage(ref Message m)
    {
    bool rtnValue=false;
    try
    {
    if (m.WParam==(IntPtr)16) //Shift键的状态
    if (((uint)m.LParam & 0x80000000) == 0) ShiftStat=true;
    else ShiftStat=false; if (m.WParam==(IntPtr)17) //Ctrl键的状态
    if (((uint)m.LParam & 0x80000000) == 0) CtrlStat=true;
    else CtrlStat=false; if (m.WParam==(IntPtr)18) //Alt键的状态
    if (((uint)m.LParam & 0x80000000) == 0) AltStat=true;
    else AltStat=false; if (ShiftStat & CtrlStat & AltStat)
    MessageBox.Show("Shift+Ctrl+Alt");
      

  12.   

    第二中方法更方便了:[DllImport("user32")]
    public static extern int GetAsyncKeyState (int vKey);bool ShiftStat=false;
    bool AltStat=false;
    bool CtrlStat=false;ShiftStat=(GetAsyncKeyState(16)!=0);
    CtrlStat=(GetAsyncKeyState(17)!=0);
    AltStat=(GetAsyncKeyState(18)!=0);WParam对应的虚拟键值可到这里查:
    ms-help://MS.VSCC/MS.MSDNVS.2052/winui/vkeys_529f.htm
      

  13.   

    现在又有问题了:
    当我的焦点移到别的Form上的时候,这个钩子还在乱抢消息,
    应该什么时候加载钩子,什么时候除掉它呢?
      

  14.   

    用m.hwnd和你的窗口handler比较
    到这里来看一下
    我做了一个示例
    http://expert.csdn.net/Expert/topic/1370/1370567.xml?temp=.26289
      

  15.   

    已经另开了一个帖子给分,见:
    http://expert.csdn.net/Expert/topic/1382/1382276.xml?temp=.1018488
    欢迎大家参加讨论!