WPF WebBrowser Mouse Events not working as expected

早过忘川 提交于 2019-11-26 10:00:17

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/2189510/wpf-webbrowser-mouse-events-not-working-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!