Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture the mouse right click event of a web browser control

I would like to select all when a user right clicks on my web browser control.

I am developing a win forms app, and use web browsers to display my information, because i can use html to style the words.

The right click context menu is not working for me. The options on it are all irrelevant to my app.

But the context menu after a select has been made i want to keep, the copy, cut, paste options.

I can already select all:

getCurrentBrowser().Document.ExecCommand("SelectAll", true, null);

I would just like to do it in the right click event of the web browser?

like image 287
Pomster Avatar asked Jul 10 '12 06:07

Pomster


3 Answers

Handle MouseDown event:

webBrowser.Document.MouseDown += new HtmlElementEventHandler(Document_MouseDown);

and make sure user pressed Right button, then select all:

void Document_MouseDown(object sender, HtmlElementEventArgs e)
{
    if(e.MouseButtonsPressed == MouseButtons.Right)
    {
        webBrowser.Document.ExecCommand("SelectAll", true, null);
    }
}
like image 192
Ria Avatar answered Nov 18 '22 10:11

Ria


This article shows how you can replace the context menu of the Web Browser with your own.

Alternatively, if you execute the following Javascript from within the Web Browser, it will disable the default right-click context menu:

    document.oncontextmenu=new Function("return false")

If you are using WinForms rather than WPF, you can set the IsWebBrowserContextMenuEnabled to false to prevent the IE context menu, in which case it will use the ContextMenu you supply on the WebBrowser control.

WPF doesn't have the same property exposed on the Web Browser, so it may not be so easy. In this case you may have to use WindowsFormsHost to host the WinForms Web Browser in WPF.

like image 20
Michael Avatar answered Nov 18 '22 09:11

Michael


This works :)

When the context menu shows select all is running pushing out the contextmenu i want, with the copy, paste, cut and so on.

private void webCompareSQL_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (webCompareSQL.Document != null)
            {
                webCompareSQL.Document.ContextMenuShowing += DocMouseClick;
            }
        }
        void DocMouseClick(object sender, HtmlElementEventArgs e)
        {
            webCompareSQL.Document.ExecCommand("SelectAll", true, null);
        }
like image 2
Pomster Avatar answered Nov 18 '22 11:11

Pomster