Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在webbrowser控件中获取选定文本_C#_Wpf_Webbrowser Control - Fatal编程技术网

C# 如何在webbrowser控件中获取选定文本

C# 如何在webbrowser控件中获取选定文本,c#,wpf,webbrowser-control,C#,Wpf,Webbrowser Control,我可以通过以下方式获得WPF中webbrowser控件的选定文本: IHTMLDocument2 doc1 = webBrowser.Document as IHTMLDocument2; IHTMLDocument3 doc = webBrowser.Document as IHTMLDocument3; IHTMLSelectionObject currentSelection = doc1.selection; if (doc1.sele

我可以通过以下方式获得WPF中webbrowser控件的选定文本:

IHTMLDocument2 doc1 = webBrowser.Document as IHTMLDocument2;
        IHTMLDocument3 doc = webBrowser.Document as IHTMLDocument3;

        IHTMLSelectionObject currentSelection = doc1.selection;

        if (doc1.selection.type == "Text")
        {
            IHTMLTxtRange range = (IHTMLTxtRange)doc1.selection.createRange();
        }
这很好,如果我将range.text的值设置为其他值,则会更改文本的值。我唯一的问题是在Gmail这样的网页上,有一些WYSIWYG编辑器,selection.type总是“无”。我怀疑这是因为文本编辑器在技术上是一个子文档。我不知道如何找到子文档并检查文本是否被选中。有人能帮我吗?谢谢

您可以检查是否为框架并具有属性(
document.activeElement.contentWindow!=null
),然后可以使用
contentWindow.document
访问框架的内部
文档。递归执行此操作,直到找到带有
document.selection!=空

为了说明这一点:

main.html:

显示:

这是可编辑的

<!DOCTYPE html>
<body>
<iframe src="iframe.html"></iframe>
</body>
<!DOCTYPE html>
<head>
<script>
window.onload = function()
{
    window.focus();
    document.execCommand("SelectAll", false);
}
</script>
</head>
<body contentEditable="true">
This is editable
</body>
var text = this.wb.Document.InvokeScript("eval", new object[] {
    "document.activeElement.contentWindow.document.selection.createRange().text" });
MessageBox.Show(text.ToString());