从.net(C#)中的Webbrowser控件中检索所选文本

时间:2022-11-11 23:25:28

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.

我一直在试图找出如何检索用户在我的webbrowser控件中选择的文本,并且在通过msdn和其他资源挖掘后没有运气,所以我想知道是否有办法实际执行此操作。也许我只是错过了什么。

I appreciate any help or resources regarding this.

我感谢任何关于此的帮助或资源。

Thanks

4 个解决方案

#1


44  

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.

您需要使用WebBrowser控件的Document.DomDocument属性并将其强制转换为Microsoft.mshtml互操作程序集中提供的IHtmlDocument2接口。这使您可以访问实际运行在IE中的Javascript的完整DOM。

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.

为此,首先需要将项目引用添加到Microsoft.mshtml程序集中,通常位于“C:\ Program Files \ Microsoft.NET \ Primary Interop Assemblies \ Microsoft.mshtml.dll”。可能有多个,请确保选择此路径的引用。

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);
        }
    }

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

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

#2


1  

Just in case anybody is interested in solution that doesn't require adding a reference to mshtml.dll:

万一有人对解决方案感兴趣,不需要添加对mshtml.dll的引用:

private string GetSelectedText()
{
    dynamic document = webBrowser.Document.DomDocument;
    dynamic selection = document.selection;
    dynamic text = selection.createRange().text;
    return (string)text;
}

#3


0  

And if You just use the technique bellow?

如果你只是使用下面的技术?

//Copy selected text to clipboard

//将所选文本复制到剪贴板

        Clipboard.Clear();
        SendKeys.SendWait("^(c)");

//Get selected text from clipboard

//从剪贴板中获取所选文本

        string strClip = Clipboard.GetText().Trim();
        Clipboard.Clear();

#4


-1  

I'm assuming you have a WinForms application which includes a control that opens a website.

我假设您有一个WinForms应用程序,其中包含一个打开网站的控件。

Check to see if you can inject/run JavaScript inside your webbrowser control. Using JavaScript, you would be able to find out what was selected and return it. Otherwise, I doubt the web browser control has any knowledge of what is selected inside it.

检查是否可以在webbrowser控件中注入/运行JavaScript。使用JavaScript,您将能够找到所选内容并将其返回。否则,我怀疑Web浏览器控件是否知道其中选择了什么。

#1


44  

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.

您需要使用WebBrowser控件的Document.DomDocument属性并将其强制转换为Microsoft.mshtml互操作程序集中提供的IHtmlDocument2接口。这使您可以访问实际运行在IE中的Javascript的完整DOM。

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.

为此,首先需要将项目引用添加到Microsoft.mshtml程序集中,通常位于“C:\ Program Files \ Microsoft.NET \ Primary Interop Assemblies \ Microsoft.mshtml.dll”。可能有多个,请确保选择此路径的引用。

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);
        }
    }

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

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

#2


1  

Just in case anybody is interested in solution that doesn't require adding a reference to mshtml.dll:

万一有人对解决方案感兴趣,不需要添加对mshtml.dll的引用:

private string GetSelectedText()
{
    dynamic document = webBrowser.Document.DomDocument;
    dynamic selection = document.selection;
    dynamic text = selection.createRange().text;
    return (string)text;
}

#3


0  

And if You just use the technique bellow?

如果你只是使用下面的技术?

//Copy selected text to clipboard

//将所选文本复制到剪贴板

        Clipboard.Clear();
        SendKeys.SendWait("^(c)");

//Get selected text from clipboard

//从剪贴板中获取所选文本

        string strClip = Clipboard.GetText().Trim();
        Clipboard.Clear();

#4


-1  

I'm assuming you have a WinForms application which includes a control that opens a website.

我假设您有一个WinForms应用程序,其中包含一个打开网站的控件。

Check to see if you can inject/run JavaScript inside your webbrowser control. Using JavaScript, you would be able to find out what was selected and return it. Otherwise, I doubt the web browser control has any knowledge of what is selected inside it.

检查是否可以在webbrowser控件中注入/运行JavaScript。使用JavaScript,您将能够找到所选内容并将其返回。否则,我怀疑Web浏览器控件是否知道其中选择了什么。