捕获和处理Web浏览器关闭事件

Rus*_*ail 4 c# events webbrowser-control

我正在使用Windows窗体和WebBrowser控件,我需要捕获和处理WebBrowser控件调用关闭.

我已经看了这个,但它不能正常工作,因为我需要它:http://blogs.artinsoft.net/Mrojas/archive/2009/05/21/Extended-WebBrowser-Control-Series-WebBrowser-Control-and- windowClose().ASPX

这是我收到的消息:

---------------------------
Web Browser
---------------------------
The webpage you are viewing is trying to close the window.

Do you want to close this window?
---------------------------
Yes   No   
---------------------------
Run Code Online (Sandbox Code Playgroud)

我需要捕获此事件并禁止它,然后执行一个函数.有谁知道一个简单的方法来做到这一点?

Rus*_*ail 9

在到处搜索并没有真正得到我需要的东西之后,我设法偶然发现了一个偶然的参考:

HtmlDocument htmlDocument = this.webBrowser1.Document;
htmlDocument.Window.Unload += new HtmlElementEventHandler(Window_Unload);
Run Code Online (Sandbox Code Playgroud)

在WebBrowser控件关闭和处理之前,Document确实已卸载.

void Window_Unload(object sender, HtmlElementEventArgs e)
{
    // Do your stuff here...
    // Call Method...
    // Close Form...
}
Run Code Online (Sandbox Code Playgroud)

现在这对我有用.