为什么,this.Hide()后,热键就不响应了呢 ?

解决方案 »

  1.   

    没有消息到来using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Collections;
    using System.Windows.Forms;public class C_全局热键 : IMessageFilter
    {
        public event Action<int> OnHotkey;
        public enum KeyFlags
        {
            MOD_ALT = 0x1,
            MOD_CONTROL = 0x2,
            MOD_SHIFT = 0x4,
            MOD_WIN = 0x8
        }    class NativeMethods
        {
            private NativeMethods() { }        #region WIN32 API        [DllImport("user32.dll")]
            public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk);        [DllImport("user32.dll")]
            public static extern UInt32 UnregisterHotKey(IntPtr hWnd, UInt32 id);        [DllImport("kernel32.dll")]
            public static extern UInt32 GlobalAddAtom(String lpString);        [DllImport("kernel32.dll")]
            public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom);        #endregion
        }    Hashtable keyIDs = new Hashtable();
        IntPtr hWnd;    public C_全局热键(IntPtr hWnd)
        {
            this.hWnd = hWnd;
            Application.AddMessageFilter(this);
        }    public int RegisterHotkey(Keys Key, KeyFlags keyflags)
        {
            UInt32 hotkeyid = NativeMethods.GlobalAddAtom(System.Guid.NewGuid().ToString());
            NativeMethods.RegisterHotKey((IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)Key);
            keyIDs.Add(hotkeyid, hotkeyid);
            return (int)hotkeyid;
        }    public void UnregisterHotkeys()
        {
            Application.RemoveMessageFilter(this);
            foreach (UInt32 key in keyIDs.Values)
            {
                NativeMethods.UnregisterHotKey(hWnd, key);
                NativeMethods.GlobalDeleteAtom(key);
            }
        }
        public bool PreFilterMessage(ref Message m)
        {
            if (m.Msg ==  0x0312)
            {                            //这里断点没有到
                if (OnHotkey != null)
                {
                    foreach (UInt32 key in keyIDs.Values)
                    {
                        if ((UInt32)m.WParam == key)
                        {
                            OnHotkey((int)m.WParam);
                            return true;
                        }
                    }
                }
            }        return false;
        }
    }
      

  2.   

    隐藏窗体:
     this.WindowState = FormWindowState.Minimized;
                this.ShowInTaskbar = false;
      

  3.   


            protected override void WndProc(ref Message m)
            {
                const int WM_HOTKEY = 0x0312;
                //按快捷键 
                switch (m.Msg)
                {
                    case WM_HOTKEY:
                        switch (m.WParam.ToInt32())
                        {
                            case 100:    //按下的是F7
                                CopyPosition();
                                break;                        default:
                                break;
                        }
                        break;
                }            base.WndProc(ref m);
            }
      

  4.   

    this.Hide() 在Net的源码中只是一个
      public void Hide()
            {
                this.Visible = false;
            }
    可能隐藏了后,对于P/Invoke的某些方法不起作用了。
      

  5.   

    问题的正解是:
     //这里因为 this.ShowInTaskbar=false 会改变窗体句柄值,因此需要再次注册热键,不信?Debug this.Handle
    因为句柄改变要重新注册一下热键