0

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
TreantBG
  • 1,192
  • 6
  • 25
  • 44

2 Answers2

1

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?

Community
  • 1
  • 1
blak3r
  • 16,066
  • 16
  • 78
  • 98
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;
    }
Jin-Wook Chung
  • 4,196
  • 1
  • 26
  • 45
  • Error here: docEvent = (HTMLDocumentEvents2_Event)_webBrowser.Document; Cannot convert type 'System.Windows.Forms.HtmlDocument' to 'mshtml.HTMLDocumentEvents2_Event – TreantBG Jun 16 '11 at 16:15
  • Oops! sorry for annoying. I mentioned the WPF WebBrowser. At Winform WebBrowser, you could use _webBrowser.Document.DomDocument. – Jin-Wook Chung Jun 16 '11 at 16:38
  • You'll need to use the DocumentCompleted event in WinForm intead of the LoadCompleted event in WPF. – Jin-Wook Chung Jun 16 '11 at 16:47
  • 1
    but now how to get the items of that context menu? – TreantBG Jun 16 '11 at 18:23
  • I'll be glad if I can answer this question also. However, accessing the context menu is not easy one, because a base of the WebBrowser in WinForm is not a managed object. – Jin-Wook Chung Jun 16 '11 at 18:50