C# Web 浏览器控件:如何捕获文档事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9110388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Web browser control: How to capture document events?
提问by Bassem
I am using WPF's WebBrowser control to load a simple web page. On this page I have an anchor or a button. I want to capture the click event of that button in my application's code behind (i.e. in C#).
我正在使用 WPF 的 WebBrowser 控件加载一个简单的网页。在此页面上,我有一个锚点或按钮。我想在我的应用程序后面的代码中(即在 C# 中)捕获该按钮的点击事件。
Is there are a way for the WebBrowser control to capture click events on the loaded page's elements?
WebBrowser 控件有没有办法捕获已加载页面元素上的单击事件?
In addition, is it possible to communicate event triggered data between the page and the WebBrowser? All of the above should be possible am I right?
此外,是否可以在页面和 WebBrowser 之间传递事件触发的数据?以上所有应该都是可能的,对吗?
- Edit: Probable solution:
- 编辑:可能的解决方案:
I have found the following link that might be a solution. I haven't tested it yet but it's worth the shot. Will update this question depending on my test results.
我发现以下链接可能是一个解决方案。我还没有测试过,但值得一试。将根据我的测试结果更新此问题。
http://support.microsoft.com/kb/312777
http://support.microsoft.com/kb/312777
- Link taken from: Source
- 链接来自:来源
采纳答案by Bassem
Ok Answer found - tested and it works:
好的答案找到了 - 经过测试并且有效:
- Add a reference from the COM tab called: Microsoft HTML Object Library
- 从 COM 选项卡添加一个名为:Microsoft HTML 对象库的引用
The following is an example code:
下面是一个示例代码:
You will need two components: WebBrowser (webBrowser1) and a TextBox (textBox1)
您将需要两个组件:WebBrowser (webBrowser1) 和一个 TextBox (textBox1)
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
webBrowser1.LoadCompleted += new LoadCompletedEventHandler(webBrowser1_LoadCompleted);
}
private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
{
mshtml.HTMLDocument doc;
doc = (mshtml.HTMLDocument)webBrowser1.Document;
mshtml.HTMLDocumentEvents2_Event iEvent;
iEvent = (mshtml.HTMLDocumentEvents2_Event)doc;
iEvent.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(ClickEventHandler);
}
private bool ClickEventHandler(mshtml.IHTMLEventObj e)
{
textBox1.Text = "Item Clicked";
return true;
}
}
回答by Steven de Salas
Here is another example.
这是另一个例子。
I was trying to inject a remote javascript file and execute some code when ready by adding a DOM element <script src="{path to remote file}" />to the header, essentially the same idea as jQuery.getScript(url, callback)..
我试图注入一个远程 javascript 文件,并在准备就绪时通过向<script src="{path to remote file}" />标头添加 DOM 元素来执行一些代码,这与jQuery.getScript(url, callback) 的想法基本相同。
The code below works fine.
下面的代码工作正常。
HtmlElementCollection head = browser.Document.GetElementsByTagName("head");
if (head != null)
{
HtmlElement scriptEl = browser.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.src = url;
element.type = "text/javascript";
head[0].AppendChild(scriptEl);
// Listen for readyState changes
((mshtml.HTMLScriptEvents2_Event)element).onreadystatechange += delegate
{
if (element.readyState == "complete" || element.readyState == "loaded")
{
Callback.execute(callbackId);
}
};
}

