清除webbrowser控件中的选择

时间:2014-01-22 12:04:01

标签: c# winforms webbrowser-control exec

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

为此,代码段为: -

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

3 个答案:

答案 0 :(得分:3)

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

Document

否则,您始终可以using mshtml; ... IHTMLDocument2 htmlDocument = webBrowser2.Document.DomDocument as IHTMLDocument2; IHTMLSelectionObject selection = htmlDocument.selection; if (selection != null) { selection.clear(); } 到脚本URI:

Navigate

修改:将其更改为使用webBrowser2.Navigate("javascript:document.selection&&document.selection.clear();"); ,而不是Navigate

答案 1 :(得分:2)

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

答案 2 :(得分:0)

如果您想取消选择,请使用:

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

但是如果你想删除(隐藏)所选的单词:

IHTMLDocument2 htmlDocument = webBrowser2.Document.DomDocument as IHTMLDocument2;
IHTMLSelectionObject selection = htmlDocument.selection;

if (selection != null) {
    selection.clear();
}