WPF WebBrowser鼠标事件未按预期工作
我在WPF页面中有一个WebBrowser对象,每当用户与页面交互时我都会尝试做某事。 我最初尝试使用与WebBrowser对象关联的事件,但它们似乎没有被触发。 下面是我的代码尝试执行的简化示例:
webBrowser.MouseDown += new MouseButtonEventHandler(webBrowser_MouseDown);
使用事件处理程序:
void webBrowser_MouseDown(object sender, MouseButtonEventArgs e) { System.Windows.MessageBox.Show("Pressed"); }
但是,当我运行页面并在WebBrowser内部单击时,不会显示任何消息框。
道歉,最初我曾提到它是一个System.Controls WebBrowser而不是Forms浏览器。
根据文档 , WebBrowser
控件不支持鼠标事件。 您需要使用WebBrowser.Document
属性将处理程序连接到控件中显示的文档提供的DOM事件。 这篇文章有一个如何做到这一点的例子。
添加ms html com库
一旦WebBrowser.LoadCompleted事件触发,请尝试以下操作:
mshtml.HTMLDocumentEvents2_Event doc = ((mshtml.HTMLDocumentEvents2_Event)Browser.Document); doc.onmouseover += new mshtml.HTMLDocumentEvents2_onmouseoverEventHandler(doc_onmouseover);
或使用其他一些事件。
希望这有助于某人。