2

我正在我的 Windows 应用程序中实现一个Webkit 浏览器控件

我需要使用一个自定义上下文菜单(右键单击),它只有复制/剪切/粘贴作为其选项,而不管右键单击什么元素。我需要一步一步来了解如何实现它

4

1 回答 1

1

为 自定义上下文菜单WebKitBrowser假设您获得对的引用,WebViewClass然后IWebUIDelegate通过调用该setUIDelegate()方法为其设置 a 。

void MyWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    var webView = this.GetWebView() as WebKit.Interop.WebViewClass;
    webView.setUIDelegate(new MyWebUIDelegate(this));
}

IWebUIDelegate实现中,您可以拦截该contextMenuItemsForElement方法并触发浏览器上下文菜单的显示。

这是一个工作示例:

public partial class Form1 : Form
{
    MyWebBrowser webKitBrowser;

    public Form1()
    {
        InitializeComponent();
        webKitBrowser = new MyWebBrowser();
        webKitBrowser.Dock = DockStyle.Fill;
        this.Controls.Add(webKitBrowser);
        webKitBrowser.Navigate("http://www.google.com");
    }
}

class MyContextMenu : ContextMenu
{
    public MyContextMenu()
    {
        var cutMenuItem = new MenuItem("Cut");
        var copyMenuItem = new MenuItem("Copy");
        var pasteMenuItem = new MenuItem("Paste");

        cutMenuItem.Click += cutMenuItem_Click;

        MenuItems.Add(cutMenuItem);
        MenuItems.Add(copyMenuItem);
        MenuItems.Add(pasteMenuItem);
    }

    void cutMenuItem_Click(object sender, EventArgs e)
    {
        //TODO: implement functionality
        MessageBox.Show("Cut was selected");
    }
}

class MyWebBrowser : WebKitBrowser
{

    public event EventHandler ShowContextMenu = new EventHandler(OnFireShowContextMenu);

    public MyWebBrowser()
    {
        DocumentCompleted += MyWebBrowser_DocumentCompleted;
        var myContextMenu = new MyContextMenu();
        ContextMenu = myContextMenu;
    }

    void MyWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        var webView = this.GetWebView() as WebKit.Interop.WebViewClass;
        webView.setUIDelegate(new MyWebUIDelegate(this));
    }

    public static void OnFireShowContextMenu(object sender, EventArgs e)
    {
        var webBrowser = (Control)sender;
        var webView = (WebKit.Interop.WebViewClass)((MyWebBrowser)sender).GetWebView();
        var originalPoint = webBrowser.PointToScreen(new Point(0, 0));
        var currentPoint = new Point(Cursor.Position.X - originalPoint.X, Cursor.Position.Y - originalPoint.Y);

        ((WebKitBrowser)sender).ContextMenu.Show((Control)sender, currentPoint);
    }

    public void FireShowContextMenu()
    {
        this.ShowContextMenu(this, null);
    }

}

class MyWebUIDelegate : IWebUIDelegate
{
    private MyWebBrowser owner;

    public MyWebUIDelegate(MyWebBrowser browser)
    {
        this.owner = browser;
    }

    //trigger the browser's FireShowContextMenu() method
    public int contextMenuItemsForElement(WebView sender, CFDictionaryPropertyBag element, int defaultItemsHMenu)
    {
        owner.FireShowContextMenu();
        return defaultItemsHMenu;
    }

    //return 1, true
    public int hasCustomMenuImplementation()
    {
        return 1;
    }

    //the rest of the IWebUIDelegate interface implementation
}

为了更深入地了解,您可能想要研究一些其他自定义,例如open-webkit-sharp

于 2013-01-27T15:57:40.240 回答