2012-02-01 65 views
2

我被要求改造圍繞IE 9控件構建的自定義網頁瀏覽器。其中一個要求是,必須捕獲所有彈出窗口並將其重定向到新選項卡中。客戶不希望出現任何浮動的獨立窗口。如何通過window.close(javascript)關閉窗口時收到通知?

以前的開發人員實現了以下功能來捕獲這些情況並改爲打開新選項卡。

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 active x控件不是一個硬性要求。但請不要建議我僅僅爲了互聯網的純淨性而切換渲染引擎:P。

編輯:

這是我在C#中DWebBrowserEvents2接口的定義。

[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); 
    } 
+0

下面是一個示例頁面 - http://www.javascript-coder.com/files/window-popup/javascript-window -close-example1.html的。 但我實際上並沒有寫任何特定的頁面來使用,我正在編寫瀏覽器本身。 – Nathanael 2012-02-01 18:39:14

回答

4

在這裏找到了答案:http://social.msdn.microsoft.com/Forums/en/winforms/thread/1cda7799-bf65-4c66-a3bf-488656998191它爲我工作。

如果該鏈接消失,這裏是從頁答案:

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

可以請你幫我解決這個舊查詢 - 對我來說,我總是將m.WParam作爲LButtonDown {513}。如何在這裏獲取window.close()事件我無法弄清楚。 用戶將在html頁面中點擊一個按鈕,該按鈕onclick將調用JavaScript中的window.close事件? – 2016-10-07 13:34:22

相關問題