//捕获Form中按下何键,要重载这个事件
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message
msg, System.Windows.Forms.Keys keyData)
{
if(keyData == Keys.Enter )
{
MessageBox.Show (keyData.ToString()); 
}
return base.ProcessCmdKey(ref msg, keyData);
}

解决方案 »

  1.   

    首先,创建一个WinHotKey类,如下
    public class WinHotKey
    {
        [DllImport("user32.dll",SetLastError=true)]
        public static extern bool RegisterHotKey(
    IntPtr hWnd, //窗口句柄
    int id,
    KeyModifiers fsModifiers,
    Keys vk
        );    [DllImport("user32.dll",SetLastError=true)]
        public static extern bool UnregisterHotKey(
    IntPtr hWnd,
    int id
        );    [Flags()]
        public enum KeyModifiers
        {
    None = 0,
    Alt = 1,
    Control =2,
    Shift = 4,
    Windows = 8
        }    public WinHotKey()
        {
    //
    // TODO: 在此处添加构造函数逻辑
    //
        }
    }然后,在程序中这样调用
    //快捷键定义
    private bool key_Ctrl = false;
    private bool key_Shift = false;
    private bool key_Alt = false;
    private bool key_Windows = false;
    private Keys  key_other; public void SetHotKey(bool bCtrl,bool bShift,bool bAlt,bool bWindows,Keys nowKey)
    {
    try
    {
    this.key_Alt = bAlt;
    this.key_Ctrl = bCtrl;
    this.key_Shift = bShift;
    this.key_Windows = bWindows;
    this.key_other = nowKey;

    WinHotKey.KeyModifiers modifier = WinHotKey.KeyModifiers.None; if( this.key_Ctrl )
    modifier |= WinHotKey.KeyModifiers.Control;
    if(this.key_Alt )
    modifier |= WinHotKey.KeyModifiers.Alt;
    if(this.key_Shift)
    modifier |= WinHotKey.KeyModifiers.Shift;
    if(this.key_Windows)
    modifier |= WinHotKey.KeyModifiers.Windows;

    WinHotKey.RegisterHotKey(Handle,100,modifier,nowKey);
    }
    catch
    {
    //login.ShowMessage("快捷键定义错误!");
    }
    } //激活热键
    protected override void WndProc(ref Message m )
    {
    const int WM_HOTKEY = 0x0312; 

    switch(m.Msg)
    {
    case WM_HOTKEY:
    {
    //如果有新消息,弹出消息
    if( ReceiveNewMessage == true)
    {
    for(int i=0;i<this.manInforList.Count;i++)
    {
    ManInfor searchMan = (ManInfor)this.manInforList[i];
    if( searchMan.manInforID.Equals( getFriendID))
    {
    searchMan.Clicked = true;
    searchMan.P2PShow();
    break;
    }
    }
    }
    else
    {
    this.Show();
    this.TopMost = true;
    this.panel_Main.Refresh();
    this.WindowState = System.Windows.Forms.FormWindowState.Normal;
    }
    }
    break;

    base.WndProc(ref m );
    }
      

  2.   

    这是一个通用类:using System;
    namespace ChatClient
    {
    public delegate void HotkeyEventHandler(int HotKeyID);
    /// <summary>
    /// System wide hotkey wrapper. 
    ///  
    /// Robert Jeppesen
    /// Send bugs to [email protected]
    /// </summary>
    public class Hotkey : System.Windows.Forms.IMessageFilter
    {
    System.Collections.Hashtable keyIDs = new System.Collections.Hashtable();
    IntPtr hWnd;
    /// <summary>
    /// Occurs when a hotkey has been pressed.
    /// </summary>
    public event HotkeyEventHandler OnHotkey;
    public enum KeyFlags
    {
    MOD_ALT = 0x1,
    MOD_CONTROL = 0x2,
    MOD_SHIFT = 0x4,
    MOD_WIN = 0x8
    }
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern UInt32 RegisterHotKey( IntPtr hWnd, UInt32 id, 
    UInt32 fsModifiers, UInt32 vk);
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern UInt32 UnregisterHotKey( IntPtr hWnd, UInt32 id); 
    [System.Runtime.InteropServices.DllImport("kernel32.dll")]
    public static extern UInt32 GlobalAddAtom( String lpString );
    [System.Runtime.InteropServices.DllImport("kernel32.dll")]
    public static extern UInt32 GlobalDeleteAtom( UInt32 nAtom );
    /// <summary>
    /// Constructor.  Adds this instance to the MessageFilters so that this class can raise Hotkey events
    /// </summary>
    /// <param name="hWnd">A valid hWnd, i.e. form1.Handle</param>
    public Hotkey(IntPtr hWnd)
    {
    this.hWnd = hWnd;
    System.Windows.Forms.Application.AddMessageFilter(this);
    }
    /// <summary>
    /// Register a system wide hotkey.
    /// </summary>
    /// <param name="hWnd">form1.Handle</param>
    /// <param name="Key">Your hotkey</param>
    /// <returns>ID integer for your hotkey. Use this to know which hotkey was pressed.</returns>
    public int RegisterHotkey(System.Windows.Forms.Keys Key, KeyFlags keyflags)
    {
    UInt32 hotkeyid = GlobalAddAtom(System.Guid.NewGuid().ToString());
    RegisterHotKey( (IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)Key);
    keyIDs.Add(hotkeyid, hotkeyid);
    return (int)hotkeyid;
    }
    /// <summary>
    /// Unregister hotkeys and delete atoms.
    /// </summary>
    public void UnregisterHotkeys()
    {
    System.Windows.Forms.Application.RemoveMessageFilter(this);
    foreach (UInt32 key in keyIDs.Values)
    {
    UnregisterHotKey(hWnd, key); 
    GlobalDeleteAtom(key);
    }
    }
    public bool PreFilterMessage(ref System.Windows.Forms.Message m) 
    {
    if (m.Msg == 0x312) /*WM_HOTKEY*/
    {
    if(OnHotkey != null) 
    {
    foreach (UInt32 key in keyIDs.Values)
    {
    if((UInt32)m.WParam == key)
    {
    OnHotkey((int)m.WParam);
    return true;
    }
    }
    }
    }
    return false;
    }
    }
    }程序中的调用如下:
    1、创建并设置热键为Ctrl+1
    Hotkey hotkey;
    int Hotkey1;
    hotkey = new Hotkey(this.Handle);
    Hotkey1 = hotkey.RegisterHotkey(System.Windows.Forms.Keys.D1, Hotkey.KeyFlags.MOD_CONTROL);
    hotkey.OnHotkey += new HotkeyEventHandler(OnHotkey);
    2、按键监视函数public void OnHotkey(int HotkeyID)
    {
    if(HotkeyID == Hotkey1)
    {
    this.Visible = true;
    }
    else
    {
    this.Visible = false;
    }
    }
      

  3.   

    hxhbluestar(贺星河) , 你太太厉害了。谢谢。