0

I'm trying to disable the right click(well context menu) on a PDF file over a webbrowser UIElement. But no matter what it never calls to the desired handler (same behavior with a webbrowser loading usual html, but not with other UIElements).

    public override UIElement Play()
    {
        base.Play();

        //It will be represented in a Webbrowser element
        WebBrowser element = new WebBrowser();

        element.ContextMenuOpening +=new ContextMenuEventHandler(element_ContextMenuOpening);

        //navigate to the current path of the file in the HDD, adding some parameters to avoid the Adobe Reader pannels to be shown
        element.Source = new Uri(path+"#toolbar=0&navpanes=0");
        return element;
    }

    public void element_ContextMenuOpening(Object obj, ContextMenuEventArgs e)
    {
        Console.WriteLine("CONTEXT MENU PDF");
    }

The "CONTEXT MENU PDF" line is never printed. I have also tried with MouseDown, but remains the same.

EDIT 1: Thanks to MephestoKhaan I managed to make this work with Webs over webbrowser. For PDF it should be something similar and I'm still looking for the correct class to cast the Webbrowser.document object to.

    public override UIElement Play()
    {
        base.Play();

        //the element will be represented in a webbroser
        element = new WebBrowser();
        //load the web indicated by the path (url)
        element.NavigateToString(Path);
        element.Source = new Uri(Path);

        //disable context menu
        mshtml.HTMLDocumentEvents2_Event iEvent;
        iEvent = (mshtml.HTMLDocumentEvents2_Event) element.Document;
        iEvent.oncontextmenu += new mshtml.HTMLDocumentEvents2_oncontextmenuEventHandler(iEvent_oncontextmenu); 


        return element;
    }

    bool iEvent_oncontextmenu(mshtml.IHTMLEventObj e)
    {
        return false;
    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
HiddenDev
  • 183
  • 4
  • 9

2 Answers2

1

Well, MephestoKhaan's is the correct approach for elements over webbrowsers (in it's example: webs) but after struggling with Acrobat Reader over Webbrowser I changed to Foxit Reader.

with Foxit I can disable all menus and right click events directly on preferences and open it in fullscreen (not a webbrowser but a different window). It's not exactly what I was asking but solved my deppest problem (no context menu on a PDF viewer).

For the records> If you want to have buttons or some custom interface over a window use a Popup and set the windows to be over everything.

HiddenDev
  • 183
  • 4
  • 9
0

Try with this: http://support.microsoft.com/?kbid=312777 (from here WPF WebBrowser Mouse Events not working as expected. It seems that the WPF itself doesn´t manage the controls, so you have to bound the handler to the document.

Community
  • 1
  • 1
  • Thanks, this worked great with webpages, but still no luck for PDF over webbrowser. I need to find the correct class to cast the .Document and then register the handler – HiddenDev Jul 27 '12 at 10:19