4

I have a WebBrowser object in a WPF Page and I'm trying to do something whenever the user interacts with the page. I have intially tried to use the events associated with the WebBrowser object but they don't seem to be firing. Below is a simplified example of what my code is trying to do:

webBrowser.MouseDown += new MouseButtonEventHandler(webBrowser_MouseDown);

with the event handler as:

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

However when I run the page and click inside the WebBrowser no message box is displayed.

Apologies, originally I had mentioned that it was a System.Controls WebBrowser rather than a Forms browser.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Farthingworth
  • 198
  • 2
  • 5
  • 9

2 Answers2

8

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

Anders Fjeldstad
  • 10,724
  • 2
  • 33
  • 50
  • This put me on the right track. Similar procedure: http://support.microsoft.com/?kbid=312777 – Farthingworth Feb 04 '10 at 03:30
  • It's also helpful to checkout: http://www.west-wind.com/weblog/posts/393.aspx if you plan to use the browser while handling events! – Farthingworth Feb 05 '10 at 02:04
  • Take a look at my post(http://stackoverflow.com/questions/30805833/inspect-element-from-my-wpf-webbrowser-using-inspect-elementsie-chrome-fir/30919955#30919955) I had the same problems for the old winforms webbrowser control(included in WPF) and the normal wpf webbrowser control. Hope it helps :) – user254197 Jun 18 '15 at 15:50
4

Add the ms html com library

Once the WebBrowser.LoadCompleted event fires try this:

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

or use some other event.

Hope this helps someone.

Mehul Mistri
  • 15,037
  • 14
  • 70
  • 94
Andrew
  • 41
  • 1