问题
I have a form with webbrowser control ,where i am coping all the TEXT(not html) data to clipboard
For this the code snippet is :-
webBrowser2.Document.ExecCommand("SelectAll", false, null);
webBrowser2.Document.ExecCommand("Copy", false, null);
I have written the above code under webBrowser2_DocumentCompleted .
The problem is that the webpage in webbrowserControl appears with selection. I want to clear that selection after copy operation.
Is there a way to do this or a command such as
webBrowser2.Document.ExecCommand("ClearSelection", false, null); //This doesn't work
回答1:
If you import the Microsoft.mshtml library (C:\Program Files\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll), you can use the selection property of the web-browser's Document:
using mshtml;
...
IHTMLDocument2 htmlDocument = webBrowser2.Document.DomDocument as IHTMLDocument2;
IHTMLSelectionObject selection = htmlDocument.selection;
if (selection != null) {
selection.clear();
}
Otherwise, you could always Navigate to a script URI:
webBrowser2.Navigate("javascript:document.selection&&document.selection.clear();");
Edit: Changed it to use Navigate, instead of InvokeScript.
回答2:
I was able to use the Refresh method of the browser control to do the same thing. (i.e. webBrowser2.Refresh())
回答3:
If you want to unselect the selection then use:
htmlDocument.ExecCommand("Unselect", false, Type.Missing);
But if you want to remove (hide) a word selected:
IHTMLDocument2 htmlDocument = webBrowser2.Document.DomDocument as IHTMLDocument2;
IHTMLSelectionObject selection = htmlDocument.selection;
if (selection != null) {
selection.clear();
}
来源:https://stackoverflow.com/questions/21282437/clearing-the-selection-in-a-webbrowser-control