WPF WebBrowser mouse events do not work properly

I have a WebBrowser object on a WPF page, and I'm trying to do something when the user interacts with the page. I tried using events related to the WebBrowser object, but they don't seem to fire. The following is a simplified example of what my code is trying to do:

webBrowser.MouseDown += new MouseButtonEventHandler(webBrowser_MouseDown); 

with an event handler like:

 void webBrowser_MouseDown(object sender, MouseButtonEventArgs e) { System.Windows.MessageBox.Show("Pressed"); } 

However, when I launch the page and click inside WebBrowser, the message box does not appear.

Sorry, I originally mentioned that it was a System.Controls web browser, not a form browser.

+3
source share
2 answers

Mouse events are not supported by the WebBrowser , as documented . You need to attach handlers to the DOM events provided by the document displayed in the control using the WebBrowser.Document property. This post has an example of how to do this.

+7
source

Add ms html com library

After the WebBrowser.LoadCompleted event fires, try the following:

 mshtml.HTMLDocumentEvents2_Event doc = ((mshtml.HTMLDocumentEvents2_Event)Browser.Document); doc.onmouseover += new mshtml.HTMLDocumentEvents2_onmouseoverEventHandler(doc_onmouseover); 

or use another event.

Hope this helps someone.

+2
source

All Articles