简体   繁体   English

ChromiumWebBrowser禁用右键单击上下文菜单C#

[英]ChromiumWebBrowser Disable Right-Click Context Menu C#

How do you disable the right-click context menu in the Chromium Web Browser using C#? 如何使用C#禁用Chromium Web浏览器中的右键单击上下文菜单?

I found this Disable context menu in Chromium Embedded 3 (DCEF3) but I'm not familiar with the syntax (I'm using C#), plus I don't see an event on the CefSharp.WinForms.ChormiumWebBrowser class called OnBeforeContextMenu. 在Chromium Embedded 3(DCEF3)中找到了这个禁用上下文菜单,但我不熟悉语法(我正在使用C#),而且我没有在名为OnBeforeContextMenu的CefSharp.WinForms.ChormiumWebBrowser类上看到一个事件。

I faced the same problem but the above solution could no longer be used since the interface IMenuHandler (renamed to IContextMenuHandler ) has had a few changes and there no longer exists 我遇到了同样的问题,但由于接口IMenuHandler (重命名为IContextMenuHandler )进行了一些更改并且不再存在,因此无法再使用上述解决方案

bool OnBeforeContextMenu(IWebBrowser browser);

which has the following signature now: 现在有以下签名:

void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model);

The SO question linked in the question ( Disable context menu in Chromium Embedded 3 (DCEF3) ) has to do with pascal and inno setup script but the accepted answer adjusted for C# and ChromiumWebBrowser worked for me. 问题中链接的SO问题( 在Chromium Embedded 3(DCEF3)中禁用上下文菜单 )与pascal和inno设置脚本有关,但为C#和ChromiumWebBrowser调整的接受答案对我有用 The solution is to clear the model parameter in the implementation of OnBeforeContextMenu . 解决方案是在OnBeforeContextMenu的实现中清除模型参数。 The implementation of IContextMenuHandler can look like this: IContextMenuHandler的实现可能如下所示:

public class CustomContextHandler : IContextMenuHandler
{
    public void OnBeforeContextMenu(IWebBrowser browserControl, CefSharp.IBrowser browser, IFrame frame, IContextMenuParams parameters,
        IMenuModel model)
    {
        model.Clear();
    }

    public bool OnContextMenuCommand(IWebBrowser browserControl, CefSharp.IBrowser browser, IFrame frame, IContextMenuParams parameters,
        CefMenuCommand commandId, CefEventFlags eventFlags)
    {
        return false;
    }

    public void OnContextMenuDismissed(IWebBrowser browserControl, CefSharp.IBrowser browser, IFrame frame)
    {
    }
}

Then in the code that creates an object of the chromium web browser: 然后在创建chrome Web浏览器对象的代码中:

browser = new ChromiumWebBrowser(url);
browser.MenuHandler = new CustomContextHandler();

Ok I was able to figure this out. 好的,我能够搞清楚这一点。 The problem with the article referenced in my original question is that it uses the Chromium Embedded component. 我原始问题中引用的文章的问题是它使用了Chromium Embedded组件。 I'm not using that. 我不是那样用的。 I'm using the cefsharp chromium web browser. 我正在使用cefsharp chromium网络浏览器。 To hide the right-click context menu I found the answer in this article: https://github.com/cefsharp/CefSharp/issues/107 要隐藏右键单击上下文菜单,我在本文中找到了答案: https//github.com/cefsharp/CefSharp/issues/107

You just have to define a class that implements the IMenuHandler interface, then set the browser control MenuHandler property to this class. 您只需定义一个实现IMenuHandler接口的类,然后将浏览器控件MenuHandler属性设置为此类。 Finally in the class return a FALSE in the OnBeforeContextMenu method. 最后在类中返回OnBeforeContextMenu方法中的FALSE。 Here is the class: 这是班级:

public class CustomMenuHandler : IMenuHandler
{
    public bool OnBeforeContextMenu(IWebBrowser browser)
    {
        return false;
    }
}

Next, set the instance of the Chromium web browser's MenuHandler property to this class: 接下来,将Chromium Web浏览器的MenuHandler属性的实例设置为此类:

var browser = new ChromiumWebBrowser(string.empty);
browser.MenuHandler = new CustomMenuHandler();

Worked for me. 为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 C# 禁用 Windows 中文本框的右键单击上下文菜单? - How to disable the right-click context menu on textboxes in Windows, using C#? 无法右键单击上下文菜单条 c# - Can't right-click a context menu strip c# c#wpf在Windows的右键单击上下文菜单中设置链接 - c# wpf set link in the right-click context menu of windows 枚举文件夹/子文件夹内容,右键单击C#中系统托盘应用程序的上下文菜单 - Enumerate folder/subfolder contents to right-click context menu of system tray application in C# 使用C#为所有桌面快捷方式创建自定义右键单击上下文菜单项(Windows资源管理器) - Create custom right-click context menu item with C# for all desktop shortcuts (windows explorer) 在Windows C#中的进程中调用右键单击菜单 - Invoke right-click menu in a process in windows c# 在画布上禁止右键单击上下文菜单 - Suppressing right-click context menu on canvas 基于位置的动态右键单击菜单C# - Dynamic Right-Click Menu C# Based on Location 如何在.NET / C#中的右键菜单上绘制? - How to draw on top of the right-click menu in .NET/C#? 使用右键单击上下文菜单打开C#winform,但如何显示所选项? - C# winform opens with right-click context menu, but how do I get the selected item to show up?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM