我正在尝试在我的C#winforms应用程序中托管WebBrowser控件,以便打开Customer的网站。客户的网站经常尝试通过Javascript代码关闭WebBrowser。如何阻止我的WebBrowser关闭?
我正在实现ExtendedWebBrowser类,如下所示:
// A delegate type for hooking up close notifications.
public delegate void ClosingEventHandler(object sender, EventArgs e);
// We need to extend the basic Web Browser because when a web page calls
// "window.close()" the containing window isn't closed.
public class ExtendedWebBrowser : WebBrowser
{
// Define constants from winuser.h
private const int WM_PARENTNOTIFY = 0x210;
private const int WM_DESTROY = 2;
public event ClosingEventHandler Closing;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_PARENTNOTIFY:
if (!DesignMode)
{
if (m.WParam.ToInt32() == WM_DESTROY)
{
Closing(this, EventArgs.Empty);
}
}
DefWndProc(ref m);
break;
default:
base.WndProc(ref m);
break;
}
}
}
答案 0 :(得分:1)
如Microsoft文档here中所述,当您处理WM_PARENTNOTIFY消息时,您只需要获取wParam的LOWORD值即可检查WM_DESTROY消息。您应该这样过滤值:
if ((wParam.ToInt32() & 0x0000FFFF) == WM_DESTROY)
从历史上看,“字”是16位,因此wParam是包含两个16位值的“双字”(dword)。第一个值存储在两个较低的字节中,第二个存储在两个较高的字节中。