using System.Windows.Forms; using CefSharp; using CefSharp.WinForms; namespace prBrowser { internal class ContextMenuHandler : IContextMenuHandler { private const int ShowDevTools = 26501; private const int CloseDevTools = 26502; private const int SaveImageAs = 26503; private const int SaveAsPdf = 26504; private const int SaveLinkAs = 26505; private const int CopyLinkAddress = 26506; private const int OpenLinkInNewTab = 26507; private const int CloseTab = 40007; private const int RefreshTab = 40008; private const int DeleteCache = 62535; private readonly MainForm myForm; private string lastSelText = ""; public ContextMenuHandler(MainForm form) { myForm = form; } public void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model) { model.Clear(); lastSelText = parameters.SelectionText; if (parameters.SelectionText.CheckIfValid()) { model.AddItem(CefMenuCommand.Copy, "复制"); model.AddItem(CefMenuCommand.Paste, "粘贴"); model.AddSeparator(); } else { model.AddItem(CefMenuCommand.Paste, "粘贴"); model.AddSeparator(); } if (parameters.LinkUrl != "") { model.AddItem((CefMenuCommand)26507, "在新标签页中打开连接"); model.AddItem((CefMenuCommand)26506, "复制连接"); model.AddSeparator(); } if (parameters.HasImageContents && parameters.SourceUrl.CheckIfValid()) { model.AddItem((CefMenuCommand)26503, "图片另存为"); } if (parameters.SelectionText != null) { } model.AddItem((CefMenuCommand)26501, "开发者工具"); model.AddItem(CefMenuCommand.ViewSource, "查看源代码"); model.AddItem((CefMenuCommand)62535, "强力清除缓存(会关闭浏览器)"); model.AddSeparator(); model.AddItem((CefMenuCommand)40008, "刷新标签页"); model.AddItem((CefMenuCommand)40007, "关闭标签页"); } public bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags) { if (commandId == (CefMenuCommand)26501) { browser.ShowDevTools(); } if (commandId == (CefMenuCommand)26502) { browser.CloseDevTools(); } if (commandId == (CefMenuCommand)26503) { browser.GetHost().StartDownload(parameters.SourceUrl); } if (commandId == (CefMenuCommand)26505) { browser.GetHost().StartDownload(parameters.LinkUrl); } if (commandId == (CefMenuCommand)26507) { ChromiumWebBrowser newBrowser = myForm.AddNewBrowserTab(parameters.LinkUrl, focusNewTab: false, browser.MainFrame.Url); } if (commandId == (CefMenuCommand)26506) { Clipboard.SetText(parameters.LinkUrl); } if (commandId == (CefMenuCommand)40007) { myForm.InvokeOnParent(delegate { myForm.CloseActiveTab(); }); } if (commandId == (CefMenuCommand)40008) { myForm.InvokeOnParent(delegate { myForm.RefreshActiveTab(); }); } if (commandId == (CefMenuCommand)62535) { Application.Exit(); } return false; } 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; } } }