如何在 window.close (javascript) 关闭窗口时收到通知? [英] How can I be notified when a window is closed by window.close (javascript)?

查看:22
本文介绍了如何在 window.close (javascript) 关闭窗口时收到通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求改装一个围绕 IE 9 控件构建的自定义网络浏览器.要求之一是必须捕获所有弹出窗口并将其重定向到新选项卡.客户不希望出现任何浮动的独立窗口.

I've been asked to retro-fit a custom web-browser built around the IE 9 control. One of the requirements was that all pop-up windows must be caught and redirected into a new tab. The customer didn't want any floating stand-alone windows to appear.

之前的开发人员实现了以下功能来捕获这些情况并改为打开一个新选项卡.

The previous developer implemented the following function to capture these cases and open a new tab instead.

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 时,就会出现问题.

The problem occurs when one of these opened windows expects to be able to call window.close from javascript.

我希望我可以使用这个功能

I was hoping I could use this function

public void WindowClosing(bool isChildWindow, ref bool cancel)

检测 javascript 调用并关闭选项卡.但是这个函数永远不会被调用.javascript 函数本身似乎可以工作.选项卡的内容被清除.如何检测这种情况并关闭标签页?

to detect the javascript call and close the tab. But this function is never called. The javascript function itself seems to work. The contents of the tab are cleared. How can I detect this scenario and close the tab?

请注意,IE9 active x 控件不是硬性要求.但是请不要建议我仅仅为了互联网的纯洁性而切换渲染引擎 :P.

Note that the IE9 active x control is not a hard requirement. But please don't suggest I switch rendering engines solely for the sake of the purity of the internet :P.

这是我在 C# 中对 DWebBrowserEvents2 接口的定义.

Here is my definition of the DWebBrowserEvents2 interface in C#.

    [ComImport, TypeLibType((short)0x1010), InterfaceType((short)2), Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D")]
    public interface DWebBrowserEvents2
    {
        [DispId(0x111)]
        void NewWindow3(
            [In, Out, MarshalAs(UnmanagedType.IDispatch)] ref object ppDisp, 
            [In, Out] ref bool Cancel,
            [In] uint dwFlags, 
            [In, MarshalAs(UnmanagedType.BStr)] string bstrUrlContext, 
            [In, MarshalAs(UnmanagedType.BStr)] string bstrUrl);

        [DispId(253)]
        void OnQuit();

        [DispId(263)]
        void WindowClosing(
            [In] bool isChildWindow,
            [In, Out] ref bool cancel);

        [DispId(283)]
        void WindowStateChanged(
            [In] uint dwFlags,
            [In] uint dwVAlidFlagsMask);

        [DispId(271)]
        void NavigateError(
            [In, MarshalAs(UnmanagedType.IDispatch)] object pDisp,
            [In] ref object URL, [In] ref object frame,
            [In] ref object statusCode, [In, Out] ref bool cancel);
    }

推荐答案

在这里找到答案:http://social.msdn.microsoft.com/Forums/en/winforms/thread/1cda7799-bf65-4c66-a3bf-488656998191 并且它适用于我.

Found an answer here: http://social.msdn.microsoft.com/Forums/en/winforms/thread/1cda7799-bf65-4c66-a3bf-488656998191 and it worked for me.

如果链接消失了,这是该页面的答案:

In case that link goes away, here's the answer from that page:

// 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();
}

这篇关于如何在 window.close (javascript) 关闭窗口时收到通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆