如何覆盖右键单击 WebView2 控件时出现的上下文菜单?

How do you override the ContextMenu that appears when right clicking on WebView2 Control?

如何覆盖右键单击 WebView2 控件时出现的上下文菜单?

当您右键单击 WebView2 控件时,会出现带有“刷新”、“另存为”等选项的标准上下文菜单。

如何让我自己的 ContextMenuStrip 出现,而​​不是在单击鼠标右键时出现?

我们还没有完全支持自定义上下文菜单,但是我们有 a feature request tracking it. In the interim you may be able to work around this using the work around described in that feature request issue

基本上解决方法是使用 document.body 的上下文菜单事件来拦截通常的上下文菜单处理并实现您自己的处理。您可以使用 window.chrome.webview.postMessage 将上下文菜单事件发送到您的本机代码以创建本机上下文菜单,或者您可以在 HTML/JS.

中实现上下文菜单

抱歉,这不是一个简单的解决方案。如果你愿意,你可以将你自己的评论添加到功能请求中,让我们了解你的场景等,以便在 WebView2 中使用上下文菜单。谢谢!

更新(现在代码会在右键单击时显示您的上下文菜单,并在您单击任意位置时隐藏它):

您可以将以下javascript注入到您的网页(它订阅了“contextmenu”事件和“mousedown”事件):

document.addEventListener('contextmenu', function (event)
{
    let jsonObject =
    {
        Key: 'contextmenu',
        Value:
        {
            X: event.screenX,
            Y: event.screenY
        }
    };
    window.chrome.webview.postMessage(jsonObject);
});

document.addEventListener('mousedown', function (event)
{
    let jsonObject =
    {
        Key: 'mousedown',
        Value:
        {
            X: event.screenX,
            Y: event.screenY
        }
    };
    window.chrome.webview.postMessage(jsonObject);
});

保存在文件中最简单(我称之为'Javascript1.js')。

要使用 'CoreWebView2' 实例,必须初始化 WebView2 控件,订阅 'CoreWebView2InitializationCompleted' 可以解决这个问题。

要注入您的 javascript,您可以从文件中加载它并使用 AddScriptToExecuteOnDocumentCreatedAsync 注入它。

您需要禁用默认上下文菜单。这是通过将 AreDefaultContextMenusEnabled 属性 设置为 false.

来完成的

那么你需要订阅WebMessageReceived事件,并处理这两个事件。为此,创建一个包含 'Key' 和 'Value' 的结构,以反序列化从 javascript 代码发送的 JSON 字符串。

显示带有事件的整个表单的 C# 代码

using Newtonsoft.Json;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        struct JsonObject
        {
            public string Key {get; set;}
            public PointF Value{get; set;}

        private async void WebView21_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
        {
            webView21.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
            string script = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, @"Javascript1.js"));
            await webView21.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(script);
        }

        private void WebView21_WebMessageReceived(object sender, Microsoft.Web.WebView2.Core.CoreWebView2WebMessageReceivedEventArgs e)
        {
            JsonObject jsonObject = JsonDeserializer.Deserialize<JsonObject>(e.WebMessageAsJson);
            switch (jsonObject.Key)
            {
                case "contextmenu":
                    contextMenuStrip1.Show(Point.Truncate(jsonObject.Value));
                    break;
                case "mousedown":
                    contextMenuStrip1.Hide();
                    break;
            }
        }
    }
}

可以在这里找到高级版本: