如果从网站javascript C#关闭浏览器,请避免关闭WebBrowser控件>> [

问题描述 投票:0回答:1
我正在尝试在我的C#winforms应用程序中托管WebBrowser控件,以便打开客户网站。客户的网站经常尝试通过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; } } }

我正在尝试在我的C#winforms应用程序中托管WebBrowser控件,以便打开客户网站。客户的网站经常尝试通过Javascript代码关闭WebBrowser。怎么可以...
c# winforms webbrowser-control
1个回答
0
投票
如Microsoft文档here中所述,当您处理WM_PARENTNOTIFY消息时,您仅需要采用wParam的LOWORD值来检查WM_DESTROY消息。您应该像这样过滤值:
© www.soinside.com 2019 - 2024. All rights reserved.