开发者

How to detect Copy event of WebBrowser control?

开发者 https://www.devze.com 2023-03-16 15:13 出处:网络
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 d

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消