Windows 7 Chromium嵌入式框架-覆盖鼠标点击

本文关键字:覆盖 鼠标 框架 Chromium 嵌入式 Windows | 更新日期: 2023-09-27 18:05:58

我需要忽略我的应用程序和Chromium嵌入式框架中的所有右键单击。

现在我在使用WebBrowser小部件的旧版本上工作得很好,但现在切换到CEF浏览器后,KeyMessageFilter在CEF浏览器处于焦点时不会得到消息。第一个相关的帖子似乎说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;
    }
}

相关文章
  • 在winAPi: Stack Overflow问题中忽略键使用Chromium嵌入式框架覆盖鼠标

  • 忽略键:堆栈溢出问题如何检测当前按下的键?

Windows 7 Chromium嵌入式框架-覆盖鼠标点击

我是这样做的。

我在Program.cs文件中为鼠标使用了一个低级钩子。然后我使用一个设置来说明我的应用是否聚焦,这使用了表单。激活/停用事件(否则它会在应用程序运行时吞下计算机上的所有右键,糟糕的体验)。

相关链接:全局鼠标事件处理程序

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的人的工作,所以不要认为这是最好的代码,但它对我来说是有效的。