CEFsharp 鼠标单击在浏览器中不起作用

如何解决CEFsharp 鼠标单击在浏览器中不起作用

我的要求是显示一个没有边框的 Win Form。我想允许无边框拖动控件。所以我从这个 https://csharp.hotexamples.com/examples/CefSharp.WinForms.Example/ChromeWidgetMessageInterceptor/-/php-chromewidgetmessageinterceptor-class-examples.html 实现了 ChromeWidgetMessageInterceptor。但是在实现这个之后它可以被拖动。浏览器有 X 按钮。当我点击它时它不起作用。当我点击网页元素时,它应该自动响应。没有任何鼠标点击事件在浏览器中起作用。此外,如果我将边框放在 Form 上。在浏览器内单击鼠标可以正常工作。 JS 或 HTML 文件没有问题。 我尝试了简单的 google.com 但不起作用,如果我点击搜索按钮。 下面是代码。


   public class Form1{
    private ChromiumWebBrowser mBrowserView; 
    private int mMinWidth = 250;
    private int mMinHeight = 350;
    private bool formClosed;
    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;
    private bool isDragging;
    ChromeWidgetMessageInterceptor chromeWidgetMessageInterceptor;
    private IntPtr browserHandle;
    private ILog mLog;

    /// <summary>
    /// Sends the message.
    /// </summary>
    /// <param name="hWnd">The h WND.</param>
    /// <param name="Msg">The MSG.</param>
    /// <param name="wParam">The w parameter.</param>
    /// <param name="lParam">The l parameter.</param>
    /// <returns></returns>
    [DllImportAttribute("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd,int Msg,int wParam,int lParam);
    /// <summary>
    /// Releases the capture.
    /// </summary>
    /// <returns></returns>
    [DllImportAttribute("user32.dll")]
    public static extern bool ReleaseCapture();

    /// <summary>
    /// Posts the message.
    /// </summary>
    /// <param name="hWnd">The h WND.</param>
    /// <param name="Msg">The MSG.</param>
    /// <param name="wParam">The w parameter.</param>
    /// <param name="lParam">The l parameter.</param>
    /// <returns></returns>
    [return: MarshalAs(UnmanagedType.Bool)]
    [DllImport("user32.dll",SetLastError = true)]
    private static extern bool PostMessage(IntPtr hWnd,uint Msg,IntPtr wParam,IntPtr lParam);

    /// <summary>
    /// Initializes a new instance of the <see cref="PopUpControl"/> class.
    /// </summary>
    public Form1()
    {
        InitializeComponent();
        this.FormBorderStyle = FormBorderStyle.None;
    }


    /// <summary>
    /// Displays the pop up control.
    /// </summary> 
    /// <param name="url">The URL.</param>
    public void DisplayBrowser()
    {
         
        if (mBrowserView == null)
        {
            mBrowserView = new ChromiumWebBrowser(url);
            mBrowserView.Dock = DockStyle.Fill;
            this.Controls.Add(mBrowserView);
            mBrowserView.HandleCreated += MBrowserView_HandleCreated;
            mBrowserView.IsBrowserInitializedChanged += MBrowserView_IsBrowserInitializedChanged; ;
            mBrowserView.MouseDown += MBrowserView_MouseDown; ;
            mBrowserView.MouseUp += MBrowserView_MouseUp;
            mBrowserView.MouseMove += MBrowserView_MouseMove;
        }
        else
        {
            mBrowserView.Load(url);
        }
    }

    /// <summary>
    /// Handles the MouseMove event of the MBrowserView control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
    private void MBrowserView_MouseMove(object sender,MouseEventArgs e)
    {
        if (isDragging)
            this.Location = Cursor.Position;
    }

    /// <summary>
    /// Handles the MouseUp event of the MBrowserView control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
    private void MBrowserView_MouseUp(object sender,MouseEventArgs e)
    {
        isDragging = false;

    }

    /// <summary>
    /// Handles the MouseDown event of the MBrowserView control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
    private void MBrowserView_MouseDown(object sender,MouseEventArgs e)
    {
        isDragging = true;
    }

    /// <summary>
    /// Handles the HandleCreated event of the MBrowserView control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
    private void MBrowserView_HandleCreated(object sender,EventArgs e)
    {
        browserHandle = ((ChromiumWebBrowser)mBrowserView).Handle;
    }

    /// <summary>
    /// Handles the IsBrowserInitializedChanged event of the MBrowserView control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
    private void MBrowserView_IsBrowserInitializedChanged(object sender,EventArgs e)
    {
        if (mBrowserView.IsBrowserInitialized)
        {
            SetupMessageInterceptor();
            mBrowserView.ShowDevTools();
        }
    }



    /// <summary>
    /// Setups the message interceptor.
    /// </summary>
    private void SetupMessageInterceptor()
    {
        if (chromeWidgetMessageInterceptor != null)
        {
            chromeWidgetMessageInterceptor.ReleaseHandle();
            chromeWidgetMessageInterceptor = null;
        }

        Task.Run(async () =>
        {
            try
            {
                while (true)
                {
                    IntPtr chromeWidgetHostHandle;
                    if (ChromeWidgetHandleFinder.TryFindHandle(browserHandle,out chromeWidgetHostHandle))
                    {
                        chromeWidgetMessageInterceptor = new ChromeWidgetMessageInterceptor((Control)mBrowserView,chromeWidgetHostHandle,message =>
                        {
                            const int WM_MOUSEACTIVATE = 0x0021;
                            const int WM_NCLBUTTONDOWN = 0x00A1;
                            const int WM_DESTROY = 0x0002;
                            const int WM_LBUTTONDOWN = 0x0201;
                            const int WM_LBUTTONUP = 0x0202;
                            // Render process switch happened,need to find the new handle
                            if (message.Msg == WM_DESTROY)
                            {
                                SetupMessageInterceptor();
                                return;
                            }

                            if (message.Msg == WM_MOUSEACTIVATE)
                            {
                                // The default processing of WM_MOUSEACTIVATE results in MA_NOACTIVATE,// and the subsequent mouse click is eaten by Chrome.
                                // This means any .NET ToolStrip or ContextMenuStrip does not get closed.
                                // By posting a WM_NCLBUTTONDOWN message to a harmless co-ordinate of the
                                // top-level window,we rely on the ToolStripManager's message handling
                                // to close any open dropdowns:
                                // http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/ToolStripManager.cs,1249
                                var topLevelWindowHandle = message.WParam;
                                PostMessage(topLevelWindowHandle,WM_NCLBUTTONDOWN,IntPtr.Zero,IntPtr.Zero);
                            }
                            //Forward mouse button down message to browser control
                            else
                            {
                                PostMessage(browserHandle,(uint)message.Msg,message.WParam,message.LParam);
                                mLog.Error("Message when clicked  WM_LBUTTONDOWN");
                            }


                            // The ChromiumWebBrowserControl does not fire MouseEnter/Move/Leave events,because Chromium handles these.
                            // However we can hook into Chromium's messaging window to receive the events.
                            //
                            const int WM_MOUSEMOVE = 0x0200;
                            const int WM_MOUSELEAVE = 0x02A3;

                            switch (message.Msg)
                            {
                                case WM_MOUSEMOVE:
                                    mLog.Error("Message when clicked Mouse move WM_MOUSEMOVE");
                                    break;
                                case WM_MOUSELEAVE:
                                    mLog.Error("Message when clicked Mouse WM_MOUSELEAVE");
                                    break;
                                default:
                                    mLog.Error("Message when clicked not falling in any case"+message.Msg.ToString());
                                    break;

                            }
                        });

                        break;
                    }
                    else
                    {
                        // Chrome hasn't yet set up its message-loop window.
                        await Task.Delay(10);
                    }
                }
            }
            catch
            {
                // Errors are likely to occur if browser is disposed,and no good way to check from another thread
            }
        });
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res