从检索Web浏览器控件选定的文本在.NET(C#) [英] Retrieving Selected Text from Webbrowser control in .net(C#)

查看:91
本文介绍了从检索Web浏览器控件选定的文本在.NET(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图找出如何检索用户在我的WebBrowser控件选定的文字和通过MSDN和其他资源挖掘后,有没有运气,所以我想知道如果有一种方法可以真正做到这一点。也许我只是错过了一些东西。

I've been trying to figure out how to retrieve the text selected by the user in my webbrowser control and have had no luck after digging through msdn and other resources, So I was wondering if there is a way to actually do this. Maybe I simply missed something.

我AP preciate这方面的任何帮助或资源。

I appreciate any help or resources regarding this.

感谢

推荐答案

您需要使用WebBrowser控件的Document.DomDocument财产和施放此在Microsoft.mshtml互操作程序集提供的IHTMLDocument2接口。这使您可以访问整个DOM的是提供给真正的Javascript在IE中运行。

You need to use the Document.DomDocument property of the WebBrowser control and cast this to the IHtmlDocument2 interface provided in the Microsoft.mshtml interop assembly. This gives you access to the full DOM as is available to Javascript actually running in IE.

要做到这一点,首先需要添加一个引用到您的项目到Microsoft.mshtml装配通常在C:\\ Program Files文件\\ Microsoft.NET \\主互操作程序集\\ Microsoft.mshtml.dll。可能有多个,确保您选择这条道路的参考。

To do this you first need to add a reference to your project to the Microsoft.mshtml assembly normally at "C:\Program Files\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll". There may be more than one, make sure you choose the reference with this path.

然后获取当前的文本选择,例如:

Then to get the current text selection, for example:

using mshtml;

...

    IHTMLDocument2 htmlDocument = webBrowser1.Document.DomDocument as IHTMLDocument2;

    IHTMLSelectionObject currentSelection= htmlDocument.selection;

    if (currentSelection!=null) 
    {
        IHTMLTxtRange range= currentSelection.createRange() as IHTMLTxtRange;

        if (range != null)
        {
            MessageBox.Show(range.text);
        }
    }

有关从.NET应用程序访问整个DOM的更多信息,请参见:

For more information on accessing the full DOM from a .NET application, see:

的IHTMLDocument2接口参考

这篇关于从检索Web浏览器控件选定的文本在.NET(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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