日期:2014-05-18 浏览次数:21057 次
private void Form1_Load(object sender, EventArgs e) { webBrowser1.DocumentCompleted += (s, e1) => { webBrowser1.Document.Body.DetachEventHandler("oncopy", OnCopy); webBrowser1.Document.Body.AttachEventHandler("oncopy", OnCopy); webBrowser1.Document.Body.DetachEventHandler("onpaste", OnPaste); webBrowser1.Document.Body.AttachEventHandler("onpaste", OnPaste); }; webBrowser1.Navigate("www.sina.com"); } void OnCopy(object sender, EventArgs e) { dynamic doc = webBrowser1.Document.DomDocument; string text = doc.selection.createRange().text; MessageBox.Show("copying text:" + text); } void OnPaste(object sender, EventArgs e) { string text = Clipboard.GetText(); Clipboard.SetData("Text", "paste text:" + text); }
------解决方案--------------------
webBrowser1.DocumentText = "<html><body></body></html>";
IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
doc.designMode = "On";
Application.DoEvents();
webBrowser1.Document.Body.DetachEventHandler("oncopy", OnCopy);
webBrowser1.Document.Body.AttachEventHandler("oncopy", OnCopy);
webBrowser1.Document.Body.DetachEventHandler("onpaste", OnPaste);
webBrowser1.Document.Body.AttachEventHandler("onpaste", OnPaste);