【问题标题】:Clearing the selection in a webbrowser control清除网络浏览器控件中的选择
【发布时间】:2014-01-22 12:04:00
【问题描述】:

我有一个带有 webbrowser 控件的表单,我将所有 TEXT(不是 html)数据复制到剪贴板

为此,代码 sn-p 是:-

webBrowser2.Document.ExecCommand("SelectAll", false, null);
webBrowser2.Document.ExecCommand("Copy", false, null);

上面的代码我已经在 webBrowser2_DocumentCompleted 下写好了。

问题是 webbrowserControl 中的网页出现了选择。我想在复制操作后清除该选择。

有没有办法做到这一点或诸如

之类的命令
 webBrowser2.Document.ExecCommand("ClearSelection", false, null);  //This doesn't work

【问题讨论】:

    标签: c# winforms webbrowser-control exec


    【解决方案1】:

    如果您导入 Microsoft.mshtml 库 (C:\Program Files\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll),您可以使用网络浏览器的 Documentselection 属性:

    using mshtml;
    
    ...
    
    IHTMLDocument2 htmlDocument = webBrowser2.Document.DomDocument as IHTMLDocument2;
    
    IHTMLSelectionObject selection = htmlDocument.selection;
    
    if (selection != null) {
        selection.clear();
    }
    

    否则,您始终可以将 Navigate 指向脚本 URI:

    webBrowser2.Navigate("javascript:document.selection&&document.selection.clear();");
    

    编辑:将其更改为使用Navigate,而不是InvokeScript

    【讨论】:

    • 第二种方法在我的机器上不起作用,网络浏览器变为空白。
    • 不,它没有。 Web 浏览器仅加载母版页内容,其余所有内容均为空白
    • htmlDocument.execCommand("UNSELECT", false, Type.Missing);为我工作。我在ssicom.org/js/x277333.htm 上找到了这个。
    • @anurag 很高兴知道!谢谢。
    【解决方案2】:

    我可以使用浏览器控件的 Refresh 方法来做同样的事情。 (即webBrowser2.Refresh()

    【讨论】:

    • 这正是我所需要的。 :)
    • 刷新是恕我直言,与清除选择不同。即使它清除了选择。
    【解决方案3】:

    如果要取消选择,请使用:

    htmlDocument.ExecCommand("Unselect", false, Type.Missing);
    

    但是如果你想移除(隐藏)一个选中的单词:

    IHTMLDocument2 htmlDocument = webBrowser2.Document.DomDocument as IHTMLDocument2;
    IHTMLSelectionObject selection = htmlDocument.selection;
    
    if (selection != null) {
        selection.clear();
    }
    

    【讨论】:

    • 至少在我的 C# 项目中“取消选择”有效。 myDoc.execCommand("取消选择", false, null);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 2012-10-30
    • 2018-03-17
    • 1970-01-01
    相关资源
    最近更新 更多