C# window.close(javascript)关闭窗口时如何通知我?
我被要求重新安装一个围绕IE9控件构建的自定义web浏览器。其中一个要求是必须捕获所有弹出窗口并将其重定向到新选项卡。客户不希望出现任何浮动的独立窗口 以前的开发人员实现了以下功能来捕获这些案例并打开一个新选项卡C# window.close(javascript)关闭窗口时如何通知我?,c#,javascript,webbrowser-control,C#,Javascript,Webbrowser Control,我被要求重新安装一个围绕IE9控件构建的自定义web浏览器。其中一个要求是必须捕获所有弹出窗口并将其重定向到新选项卡。客户不希望出现任何浮动的独立窗口 以前的开发人员实现了以下功能来捕获这些案例并打开一个新选项卡 public void NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl) { BrowserExtendedNavigating
public void NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl)
{
BrowserExtendedNavigatingEventArgs args = new BrowserExtendedNavigatingEventArgs(ppDisp, new Uri(bstrUrl), null, (UrlContext)dwFlags);
_Browser.OnStartNewWindow(args);
Cancel = args.Cancel;
ppDisp = args.AutomationObject;
}
当其中一个打开的窗口希望能够从javascript调用window.close时,就会出现问题
我希望我能使用这个功能
public void WindowClosing(bool isChildWindow, ref bool cancel)
检测javascript调用并关闭选项卡。但是这个函数永远不会被调用。javascript函数本身似乎可以工作。选项卡的内容将被清除。如何检测此场景并关闭选项卡
请注意,IE9主动x控制并非硬性要求。但是请不要建议我仅仅为了互联网的纯洁性而切换渲染引擎
编辑:
下面是我在C#中对DWebBrowserEvents2接口的定义
在这里找到了一个答案:这对我很有效
如果该链接消失,以下是该页面的答案:
// 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;
}
}
}
//Then your containing class has the following:
private ExtendedWebBrowser browserControl;
this.browserControl.Closing +=new ClosingEventHandler(browserControl_Closing);
private void browserControl_Closing(object sender, EventArgs e)
{
this.Close();
}
这是一个示例页-。但实际上我并没有编写任何要使用的特定页面,我是在编写浏览器本身。你能帮我解决这个老问题吗?对我来说,我总是把m.WParam作为LButtonDown{513}。如何在这里获取window.close()事件我无法理解。用户将在html页面中单击一个按钮,该按钮onclick将在javascript中调用window.close事件?
// 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;
}
}
}
//Then your containing class has the following:
private ExtendedWebBrowser browserControl;
this.browserControl.Closing +=new ClosingEventHandler(browserControl_Closing);
private void browserControl_Closing(object sender, EventArgs e)
{
this.Close();
}