I have a Windows Forms application whi开发者_StackOverflowch contains a WebBrowser control. Now I want to handle the Copy event (Context menu -> Copy as well as Ctrl+C) of the WebBrowser control and do some of my own processing in that event.
How can I achieve this?
As far as handling Control+C, you can wireup to the WebBrowser's PreviewKeyDown event and do:
private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
if (e.KeyData.ToString() == "C, Control") {
Debug.WriteLine("You pressed Control + C");
// Handle here
}
}
If you have your own context menu on the web browser with a Control + C copy item, handle its click event.
Another way that I found out was this:
private void myBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
myBrowser1.Document.GetElementsByTagName("body")[0].AttachEventHandler("oncopy", SayHello);
}
public void SayHello(object obj,EventArgs e)
{
MessageBox.Show("Hello"); //Do your stuff here.
}
This triggers even for Context menu copy event.
精彩评论