从 .net(C#) 中的 Webbrowser 控件中检索选定的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/217353/
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
Retrieving Selected Text from Webbrowser control in .net(C#)
提问by Cliff
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
谢谢
采纳答案by Ash
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 的更多信息,请参阅:
回答by Jason Kealey
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.
检查是否可以在 Web 浏览器控件中注入/运行 JavaScript。使用 JavaScript,您将能够找出选择的内容并返回它。否则,我怀疑 Web 浏览器控件对其中选择的内容有任何了解。
回答by Hermano
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();
回答by username
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;
}