Web浏览器控件:如何捕获文档事件? [英] Web browser control: How to capture document events?

查看:159
本文介绍了Web浏览器控件:如何捕获文档事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WPF的WebBrowser控件加载一个简单的网页。在这个页面我有一个锚或一个按钮。我想捕获按钮的点击事件在我的应用程序的code的后面(即C#)。

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#).

时有WebBrowser控件捕捉点击加载页面的元素事件的方法吗?

Is there are a way for the WebBrowser control to capture click events on the loaded page's elements?

此外,是否有可能进行沟通事件触发​​页面和web浏览器之间的数据?上述所有的应该是可能的是吗?

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?


  • 编辑:可能的解决方案:

  • 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

  • Link taken from: Source

推荐答案

好吧答发现 - 测试和它的作品:

Ok Answer found - tested and it works:


  • 添加从名为COM选项卡的引用: Microsoft HTML对象库

  • Add a reference from the COM tab called: Microsoft HTML Object Library

下面是一个例子code:

The following is an example code:

您将需要两个组件:web浏览器(webBrowser1)和文本框(textBox1中)

You will need two components: WebBrowser (webBrowser1) and a 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;
    }
}

这篇关于Web浏览器控件:如何捕获文档事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆