Xilium.CefGlue右键菜单定制复制、前进、返回、调试工具等功能

代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Xilium.CefGlue.Demo.Browser
{
    internal sealed class WebContextMenuHandler : CefContextMenuHandler
    {
        private readonly WebBrowser _core;
        public WebContextMenuHandler(WebBrowser core)
        {
            _core = core;
        }
        protected override void OnBeforeContextMenu(CefBrowser browser, CefFrame frame, CefContextMenuParams state, CefMenuModel model)
        {
            if (model.Count > 0)
            {
                model.Clear();
            }
            model.AddItem(100, "复制");
            model.AddSeparator();
            model.AddItem(200,"返回");
            model.AddItem(300, "前进");
            model.AddItem(350, "刷新");
            model.AddSeparator();
            model.AddItem(400, "调试工具");

            if (state.SelectionText != null)
            {
                model.SetEnabled(100, true);
            }
            else
            {
                model.SetEnabled(100, false);
            }
            if (browser.CanGoBack)
            {
                model.SetEnabled(200, true);
            }
            else
            {
                model.SetEnabled(200, false);
            }
            if (browser.CanGoForward)
            {
                model.SetEnabled(300, true);
            }
            else
            {
                model.SetEnabled(300, false);
            }
        }
        protected override bool OnContextMenuCommand(CefBrowser browser, CefFrame frame, CefContextMenuParams state, int commandId, CefEventFlags eventFlags)
        {
            switch (commandId)
            {
                case 100:
                    Clipboard.SetText(state.SelectionText);
                    return true;
                case 200:
                    browser.GoBack();
                    return true;
                case 300:
                    browser.GoForward();
                    return true;
                case 350:
                    browser.ReloadIgnoreCache();
                    return true;
                case 400:
                    //browser;
                    var host = browser.GetHost();
                    var wi = CefWindowInfo.Create();
                    wi.SetAsPopup(IntPtr.Zero, "DevTools");
                    host.ShowDevTools(wi, new DevToolsWebClient(), new CefBrowserSettings(), new CefPoint(0, 0));
                    return true;
                default:  // Allow default handling, if any.
                    return false;
            }
        }
        private class DevToolsWebClient : CefClient
        {
        }
    }
}

 

你可能感兴趣的:(Xilium.CefGlue右键菜单定制复制、前进、返回、调试工具等功能)