Windows 7 Chromium Embedded Framework - 覆盖鼠标点击

时间:2014-05-30 10:14:24

标签: c# windows chromium-embedded

我需要忽略我的应用程序和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;
    }
}

相关文章

1 个答案:

答案 0 :(得分:0)

好的,我就是这样做的。

我在Program.cs文件中使用了一个低级别的鼠标钩子。 然后,我使用了一个设置来说明我的应用程序是否关注或不使用Form.Activated / Deactivated事件(否则它将在应用程序运行时吞下计算机上的所有右键单击,BAD EXPERIENCE)。

相关链接:Global mouse event handler

Program.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完全陌生的人的解决方案,所以不要把它作为有史以来最好的代码,但它对我有用。