.NET WebBrowser control - get active contextmenu

家住魔仙堡 提交于 2019-12-13 04:05:37

问题


I have WebBrowser control and i want when right button is clicked and context menu appear to get the handle of that context menu.

is that possible?


回答1:


Yes.

You could reference the following code.

    //this code assumes WebBrowser object(_webBrowser) is already initiated
    //in class scope.

    //this method is needed to execute when form is loaded.
    //Register it to load event
    private void Loaded(object sender, RoutedEventArgs e)
    {
        _webBrowser.LoadCompleted += _webBrowser_LoadCompleted;
    }

    private HTMLDocumentEvents2_Event _docEvent;

    private void _webBrowser_LoadCompleted(object sender, NavigationEventArgs e)
    {
        if (_docEvent != null)
        {
            _docEvent.oncontextmenu -= new HTMLDocumentEvents2_oncontextmenuEventHandler(_docEvent_oncontextmenu);
        }
        if (_webBrowser.Document != null)
        {
            _docEvent = (HTMLDocumentEvents2_Event)_webBrowser.Document;
            _docEvent.oncontextmenu += new HTMLDocumentEvents2_oncontextmenuEventHandler(_docEvent_oncontextmenu);
        }
    }

    bool _docEvent_oncontextmenu(IHTMLEventObj pEvtObj)
    {
        //do something and determine you want whether context menu shows or not
        //if you want to shows context menu, you'll need to return true.
        return true;
    }



回答2:


If all you want to display your own contextMenu instead. I posted a solution here that works for a winforms WebBrowser control:

How do you override the ContextMenu that appears when right clicking on winforms WebBrowser Control?



来源:https://stackoverflow.com/questions/6373853/net-webbrowser-control-get-active-contextmenu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!