1

我需要一个处理全局热键的 activeX:Shift+B

当我从 IE 调用此 ActiveX 时,下面的代码应该执行此操作。

我看到结果RegisterHotKey为真,这意味着热键已经注册好。

但我没有看到任何消息到达 ThreadPreprocessMessage 方法。为什么?

namespace Kosmala.Michal.ActiveXTest{
    public class ActiveXObject : NativeWindow, IDisposable {
        public const int WM_HOTKEY = 0x0312;
        private IntPtr pFoundWindow ;

        public ActiveXObject(){
            System.Windows.MessageBox.Show("constructor<<");
            Process[] processes = Process.GetProcessesByName("iexplore");
            foreach (Process p in processes){
                pFoundWindow = p.MainWindowHandle;
            }
            System.Windows.MessageBox.Show("pFoundWindow:" + pFoundWindow);
            SetupHotKey(pFoundWindow);
            ComponentDispatcher.ThreadPreprocessMessage += ComponentDispatcher_ThreadPreprocessMessage;
            System.Windows.MessageBox.Show("constructor>>");
        }

        void ComponentDispatcher_ThreadPreprocessMessage(ref MSG msg, ref bool handled){
            System.Windows.MessageBox.Show("inside handler");
            if (msg.message == WM_HOTKEY){
                System.Windows.MessageBox.Show("inside handler");
            }
        }

        private void SetupHotKey(IntPtr handle){
            bool res = RegisterHotKey(handle, GetType().GetHashCode(), 0x0004, 0x42); //Shift + b
            System.Windows.MessageBox.Show("SetupHotKey res:"+res);
        }

        public void Dispose(){
            UnregisterHotKey(_host.Handle, GetType().GetHashCode());
        }

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
}
4

2 回答 2

1

您正在尝试在属于另一个进程的窗口中安装热键。RegisterHotKey()的文档说:

此函数不能将热键与另一个线程创建的窗口相关联。

它甚至不适用于同一进程中的多个线程,因此它不可能与来自其他进程的线程一起使用。

于 2010-11-26T23:29:10.160 回答
0

RegisterHotKey() 仅适用于应用程序热键,即特定窗口上的快捷键。全局热键的注册方式不同 - 请参阅热键控制

Code Project 上还有一个项目System Hot Keys可以在 .NET 中执行此操作。

于 2010-11-27T03:47:07.577 回答