This repository has been archived on 2025-03-23. You can view files and clone it, but cannot push or open issues or pull requests.
xerobrowser/WebBrowser/MenuHandler.cs
2023-02-20 23:24:10 +13:00

82 lines
No EOL
2.4 KiB
C#

using System;
using CefSharp;
using System.Windows.Forms;
public class MenuHandler : IContextMenuHandler
{
public void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
{
// Remove any existent option using the Clear method of the model
//
//model.Clear();
Console.WriteLine("Context menu opened !");
// You can add a separator in case that there are more items on the list
if (model.Count > 0)
{
model.AddSeparator();
}
// Add a new item to the list using the AddItem method of the model
//model.AddItem((CefMenuCommand)113, "Copy link address");
model.AddItem((CefMenuCommand)114, "Paste");
// Add a separator
//model.AddSeparator();
}
public bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
{
// React to the first ID (show dev tools method)
if (commandId == (CefMenuCommand)26501)
{
browser.GetHost().ShowDevTools();
return true;
}
// React to the second ID (show dev tools method)
if (commandId == (CefMenuCommand)26502)
{
browser.GetHost().CloseDevTools();
return true;
}
// Any new item should be handled through a new if statement
if ((int)commandId == 113)
{
//using System.Windows.Forms;
try
{
Clipboard.SetText(parameters.SourceUrl);
}
catch (ArgumentException) { };
}
return false;
#pragma warning disable CS0162 // Unreachable code detected
if ((int)commandId == 114)
{
//using System.Windows.Forms;
Clipboard.GetText();
}
#pragma warning restore CS0162 // Unreachable code detected
return false;
// Return false should ignore the selected option of the user !
}
public void OnContextMenuDismissed(IWebBrowser browserControl, IBrowser browser, IFrame frame)
{
}
public bool RunContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
{
return false;
}
}