1.为chromiumWebBrower组件指定菜单处理类1
chromiumWebBrowser1.MenuHandler = new MenuHandler();
2.MenuHandler类实现如下
public class MenuHandler : IContextMenuHandler
{
public void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
{
//清除其他菜单项,这里不清除会出现奇怪的空白项
model.Clear();
//判断是否有选中文字,选中了才显示复制剪切
if (!String.IsNullOrEmpty(parameters.SelectionText))
{
model.AddItem(CefMenuCommand.Cut, "剪切");
model.AddItem(CefMenuCommand.Copy, "复制");
}
//判断是否在编辑框中,在才显示粘贴
if (parameters.IsEditable)
{
model.AddItem(CefMenuCommand.Paste, "粘贴");
}
//上面可以按自己需要来控制显示项以及添加其他项
// Optionally handle the context menu model in here. See https://bitbucket.org/chromiumembedded/cef/wiki/ContextMenu.md
// For example, you can modify the model or disable it completely.
}
public bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
{
// Handle the menu command
//处理菜单命令,CefMenuCommand下枚举的不需要额外处理,只需要对自定义的命令进行处理就行
return false;
}
public void OnContextMenuDismissed(IWebBrowser browserControl, IBrowser browser, IFrame frame)
{
// Optionally handle the context menu dismissal.
var webBrowser = (ChromiumWebBrowser)browserControl;
webBrowser.Dispatcher.Invoke(() =>
{
webBrowser.ContextMenu = null;
});
}
public bool RunContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
{
return false;
}
}
感谢博主,喝杯咖啡~
还没有人发表评论