【问题标题】:Windows 7 Chromium Embedded Framework - override mouse clicksWindows 7 Chromium Embedded Framework - 覆盖鼠标点击
【发布时间】:2014-05-30 10:14:23
【问题描述】:

我需要忽略我的应用程序和 Chromium Embedded Framework 中的所有右键单击。

现在我在使用 WebBrowser 小部件的旧版本上运行良好,但现在切换到 CEF 浏览器后,当 CEF 浏览器处于焦点时,KeyMessageFilter 不会收到消息。第一篇相关帖子似乎说 CEF 保留了该事件并且没有将其传递给应用程序。

但是,我不明白答案。好像是Basic什么的……

这是我的 KeyMessageFilter 代码

public class KeyMessageFilter : IMessageFilter
{
    private enum KeyMessages
    {
        WM_KEYFIRST = 0x100,
        WM_KEYDOWN = 0x100,
        WM_KEYUP = 0x101,
        WM_CHAR = 0x102,
        WM_SYSKEYDOWN = 0x0104,
        WM_SYSKEYUP = 0x0105,
        WM_SYSCHAR = 0x0106,
        WM_MOUSEWHEEL = 0x20a
    }

    [DllImport("user32.dll")]
    private static extern IntPtr GetParent(IntPtr hwnd);

    // We check the events agains this control to only handle
    // key event that happend inside this control.
    Control _control;

    public KeyMessageFilter()
    { }

    public KeyMessageFilter(Control c)
    {
        _control = c;
    }

    public bool PreFilterMessage(ref Message m)
    {

        Console.WriteLine(m.Msg);

        // Filter out WM_NCRBUTTONDOWN/UP/DBLCLK
        if (m.Msg == 0xA4 || m.Msg == 0xA5 || m.Msg == 0xA6) return true;
        // Filter out WM_RBUTTONDOWN/UP/DBLCLK
        if (m.Msg == 0x204 || m.Msg == 0x205 || m.Msg == 0x206) return true;

        if (m.Msg == (int)KeyMessages.WM_MOUSEWHEEL)
        {
            if (Control.ModifierKeys == Keys.Control)
            {
                return true;
            }

        }

        if (m.Msg == (int)KeyMessages.WM_KEYDOWN)
        {
            if (_control != null)
            {
                IntPtr hwnd = m.HWnd;
                IntPtr handle = _control.Handle;
                while (hwnd != IntPtr.Zero && handle != hwnd)
                {
                    hwnd = GetParent(hwnd);
                }
                if (hwnd == IntPtr.Zero) // Didn't found the window. We are not interested in the event.
                    return false;
            }

            Keys key = (Keys)m.WParam;

            if (key.Equals(Keys.Tab))
            {
                return true;
            }

            if (Control.ModifierKeys == Keys.Control)
            {

                switch (key)
                {

                    case Keys.Oemplus:
                        return true;
                    case Keys.OemMinus:
                        return true;

                }

            }

        }
        return false;
    }
}

相关帖子

【问题讨论】:

    标签: c# windows chromium-embedded


    【解决方案1】:

    好的,这就是我的做法。

    我在 Program.cs 文件中为鼠标使用了一个低级挂钩。 然后我使用一个设置来说明我的应用程序是否集中,这使用了 Form.Activated / Deactivated 事件(否则它会在应用程序运行时吞噬计算机上的所有右键单击,糟糕的体验)。

    相关链接:Global mouse event handler

    程序.cs

     private static LowLevelMouseProc _proc = HookCallback;
        private static IntPtr _hookID = IntPtr.Zero;
    
    Main(string[] args){
    
    .......
      _hookID = SetHook(_proc);
    
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new ContainerForm());
    
    
           UnhookWindowsHookEx(_hookID);
    
    }
    
    
    
        private static IntPtr SetHook(LowLevelMouseProc proc)
        {
            using (Process curProcess = Process.GetCurrentProcess())
            using (ProcessModule curModule = curProcess.MainModule)
            {
                return SetWindowsHookEx(WH_MOUSE_LL, proc,
                    GetModuleHandle(curModule.ModuleName), 0);
            }
        }
    
        private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
    
        private static IntPtr HookCallback(
            int nCode, IntPtr wParam, IntPtr lParam)
        {
    
            if (nCode >= 0 &&
                (MouseMessages.WM_RBUTTONDOWN == (MouseMessages)wParam || MouseMessages.WM_RBUTTONUP == (MouseMessages)wParam))
            {
                //If the app has focuse swallow event
                if (Properties.Settings.Default.AppFocus) {
    
                    return new IntPtr(-1);
    
                }
                else
                {
                    return CallNextHookEx(_hookID, nCode, wParam, lParam);
    
                }
    
            }
    
            return CallNextHookEx(_hookID, nCode, wParam, lParam);
        }
    
        private const int WH_MOUSE = 7;
    
        private const int WH_MOUSE_LL = 14;
    
        private enum MouseMessages
        {
            WM_LBUTTONDOWN = 0x0201,
            WM_LBUTTONUP = 0x0202,
            WM_MOUSEMOVE = 0x0200,
            WM_MOUSEWHEEL = 0x020A,
            WM_RBUTTONDOWN = 0x0204,
            WM_RBUTTONUP = 0x0205
        }
    
        [StructLayout(LayoutKind.Sequential)]
        private struct POINT
        {
            public int x;
            public int y;
        }
    
        [StructLayout(LayoutKind.Sequential)]
        private struct MSLLHOOKSTRUCT
        {
            public POINT pt;
            public uint mouseData;
            public uint flags;
            public uint time;
            public IntPtr dwExtraInfo;
        }
    
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook,
            LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
    
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);
    
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
            IntPtr wParam, IntPtr lParam);
    
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);
    
    }
    

    Form.cs

     private void onActivated(object sender, EventArgs e)
             {
    
                 Properties.Settings.Default.AppFocus = true;
    
             }
    
             private void onDeactivated(object sender, EventArgs e)
             {
                 Properties.Settings.Default.AppFocus = false;
    
             }
    

    这可能不是最好的方法,但是我无法让 CEFBrowser 对象忽略右键单击,就像我无法让整个表单忽略右键单击,甚至是应用程序忽略(非低级)鼠标事件。这是一个完全不熟悉 Windows 的人的工作,所以不要把它当作有史以来最好的代码,但它对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      • 2014-07-26
      • 1970-01-01
      相关资源
      最近更新 更多