右键单击WebView2控件时,如何覆盖出现的ContextMenu? [英] How do you override the ContextMenu that appears when right clicking on WebView2 Control?
问题描述
如何覆盖右键单击WebView2控件时显示的ContextMenu?
How do you override the ContextMenu that appears when right clicking on WebView2 Control?
右键单击WebView2控件时,将显示带有诸如刷新",另存为"等选项的标准上下文菜单.
When you right click on WebView2 control, the standard context menu with options such as "Refresh", "Save as", etc. appears.
如何使自己的ContextMenuStrip出现,而不是在单击鼠标右键时出现?
How do I make my own ContextMenuStrip appear instead that can appear during Right mouse button click ?
推荐答案
更新(现在,代码在右键单击时显示了上下文菜单,在单击任意位置时将其隐藏了):
Update (now the code shows your context menu on right click and hides it when you click anywhere):
您可以将以下 javascript
注入 到您的网页中(它订阅了" contextmenu
"事件和" mousedown
'事件):
You can inject the following javascript
into your webpage (it subscribes to the 'contextmenu
' event and the 'mousedown
' event):
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").
It's easiest to save it in a file (I call it 'Javascript1.js').
要使用'CoreWebView2'实例,必须初始化 WebView2
控件,订阅'CoreWebView2InitializationCompleted'即可解决此问题.
To work with the 'CoreWebView2' instance, the WebView2
control must be initialized, subscribing to 'CoreWebView2InitializationCompleted' solves that.
要注入您的JavaScript,可以从文件中加载它,然后使用 AddScriptToExecuteOnDocumentCreatedAsync
进行注入.
To inject your javascript, you can load it from the file and use AddScriptToExecuteOnDocumentCreatedAsync
to inject it.
您需要禁用默认上下文菜单.这是通过将 AreDefaultContextMenusEnabled
属性设置为 false
来完成的.
You need to disable default context menu. This is done by setting AreDefaultContextMenusEnabled
property to false
.
然后,您需要预订 WebMessageReceived
事件并处理这两个事件.为此,请创建一个具有键"和值"的结构,以反序列化从javascript代码发送的JSON字符串.
Then you need to subscribe to the WebMessageReceived
event and handle the two events. To do that, create a structure with a 'Key' and a 'Value' to deserialize the JSON string sent from javascript code.
C#代码,显示带有事件的整个表单:
C# code that shows the whole form with events:
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;
public PointF Value;
}
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 = JsonConvert.DeserializeObject<JsonObject>(e.WebMessageAsJson);
switch (jsonObject.Key)
{
case "contextmenu":
contextMenuStrip1.Show(Point.Truncate(jsonObject.Value));
break;
case "mousedown":
contextMenuStrip1.Hide();
break;
}
}
}
}
这篇关于右键单击WebView2控件时,如何覆盖出现的ContextMenu?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!