禁用WebBrowser上下文菜单

时间:2012-06-07 18:54:00

标签: c# .net wpf webbrowser-control

如何禁用WPF WebBrowser-Control的标准上下文菜单?

1 个答案:

答案 0 :(得分:1)

使用mshtml;

    private mshtml.HTMLDocumentEvents2_Event documentEvents;

在构造函数或xaml中设置LoadComplete事件:

   webBrowser.LoadCompleted += webBrowser_LoadCompleted;

然后在该方法中创建新的webbrowser文档对象并查看可用属性并创建新事件,如下所示:

private void webBrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
    documentEvents = (HTMLDocumentEvents2_Event)webBrowserChat.Document; // this will access the events properties as needed
    documentEvents.oncontextmenu += webBrowserChat_ContextMenuOpening;
}

private bool webBrowserChat_ContextMenuOpening(IHTMLEventObj pEvtObj)
{
    return false; // ContextMenu wont open
    // return true;  ContextMenu will open
    // Here you can create your custom contextmenu or whatever you want
}