none
关于webbrowser捕获js关闭ie事件 RRS feed

  • 问题

  • 例如:1.html

    <script type='text/avascript'>window.close();</scritp>

    在我的webbrowser contorl 里怎么捕获这个关闭事件。谢谢

    2014年2月24日 11:13

答案

  • Hi,

    Which version of webbrowser are you using? And I suggest that you better post the issue to winForm or wpf forum where you can get better answers. In addition, you can refer the workaround:

    possible workaround may be to host WebBrowser ActiveX Control directly via AxHost class, in which case WindowClosing gets fire correctly. Example:

    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace WebBrowserApp
    {
        // MainForm
    
        public partial class MainForm : Form 
        {
            WebBrowser webBrowser;
    
            public MainForm()
            {
                InitializeComponent();
                InitBrowser();
    
                if (this.webBrowser.Document == null && this.webBrowser.ActiveXInstance == null)
                    throw new ApplicationException ("Unable to initialize WebBrowser ActiveX control.");
    
                var ax = (SHDocVw.WebBrowser)this.webBrowser.ActiveXInstance;
                ax.NewWindow2 += (ref object ppDisp, ref bool Cancel) =>
                {
                    var popup = new RawBrowserPopup();
                    popup.Visible = true;
                    ppDisp = popup.WebBrowser.ActiveXInstance;
                };
    
                this.Load += (s, e) =>
                {
                    this.webBrowser.DocumentText = "<a target=\"_blank\" href=\"javascript:'<button onclick=\\'window.close()\\'>Close</button>'\">Go</a>";
                };
            }
    
            // create a WebBrowser instance
            void InitBrowser()
            {
                this.webBrowser = new WebBrowser();
                this.webBrowser.Dock = DockStyle.Fill;
                this.Controls.Add(this.webBrowser);
                this.webBrowser.Visible = true;
            }
        }
    
        // RawWebBrowser
    
        public class RawWebBrowser : AxHost
        {
            public RawWebBrowser()
                : base("8856f961-340a-11d0-a96b-00c04fd705a2")
            {
            }
    
            public event EventHandler Initialized;
    
            protected override void AttachInterfaces()
            {
                if (this.Initialized != null)
                    this.Initialized(this, new EventArgs());
            }
    
            public object ActiveXInstance
            {
                get
                {
                    return base.GetOcx();
                }
            }
        }
    
        // RawBrowserPopup
    
        public class RawBrowserPopup : Form
        {
            RawWebBrowser webBrowser;
    
            public RawWebBrowser WebBrowser
            {
                get { return this.webBrowser; }
            }
    
            public RawBrowserPopup()
            {
                this.webBrowser = new RawWebBrowser();
    
                this.webBrowser.Initialized += (s, e) =>
                {
                    var ax = (SHDocVw.WebBrowser)this.webBrowser.ActiveXInstance;
                    ax.NewWindow2 += (ref object ppDisp, ref bool Cancel) =>
                    {
                        var popup = new RawBrowserPopup();
                        popup.Visible = true;
                        ppDisp = popup.WebBrowser.ActiveXInstance;
                    };
    
                    ax.WindowClosing += (bool IsChildWindow, ref bool Cancel) =>
                    {
                        Cancel = true;
                        this.Close();
                    };
                };
    
                this.webBrowser.Dock = DockStyle.Fill;
                this.Controls.Add(this.webBrowser);
                this.webBrowser.Visible = true;
            }
        }
    }
    

    For more information, you can refer here

    http://stackoverflow.com/questions/20205688/web-browser-control-detect-when-js-window-close-is-called

    #Extended .NET 2.0 WebBrowser Control

    http://www.codeproject.com/Articles/13598/Extended-NET-2-0-WebBrowser-Control#GoalClose


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    2014年2月25日 2:30
  • 非常感谢您的答案,其中第一个问题我改进了一下,

    dynamic o=web.ReflectGetProperty("AxIWebBrowser2");
                ppDisp = o.Application;
                o.RegisterAsBrowser = true;
                CreateNewTab(web);

    这个可以实现弹出窗口并实现窗口之间元素访问

    关于第二个问题,正如你所答复的内容,根据的代码,我把它参与到我的代码里面 像这样

    dynamic o = webBrowser.ReflectGetProperty("AxIWebBrowser2");
                SHDocVw.WebBrowser ax = (SHDocVw.WebBrowser)o.Application;
                ax.WindowClosing += ax_WindowClosing;

    但是如果换成AxSHDocVw.AxWebBrowser而不使用SHDocVw.WebBrowser 无法实现WindowClosing事件,虽然她们使用的com都是(8856f961-340a-11d0-a96b-00c04fd705a2)这个我就有点模糊。不过真的感谢你的帮助,也感谢的建议,我的程序是WPF今后我会在WPF主题提问。

    2014年2月25日 19:08

全部回复

  • void webBrowser_Loaded(object sender, RoutedEventArgs e)
            {
                HwndSource hwnd = PresentationSource.FromVisual(this) as HwndSource;
                if (hwnd != null)
                {
                    hwnd.AddHook(HwndSourceHook);
                }
                //throw new NotImplementedException();
            }
            public IntPtr HwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
            {
                if (msg == (int)WindowsMessages.WM_PARENTNOTIFY)
                {
                    //无法获取Wm_DESTORY消息
                }
                return IntPtr.Zero;
            }

    那么如果可以获取请问怎么做,或者用其他方式请问怎么写

    2014年2月24日 13:11
  • Hi,

    Which version of webbrowser are you using? And I suggest that you better post the issue to winForm or wpf forum where you can get better answers. In addition, you can refer the workaround:

    possible workaround may be to host WebBrowser ActiveX Control directly via AxHost class, in which case WindowClosing gets fire correctly. Example:

    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace WebBrowserApp
    {
        // MainForm
    
        public partial class MainForm : Form 
        {
            WebBrowser webBrowser;
    
            public MainForm()
            {
                InitializeComponent();
                InitBrowser();
    
                if (this.webBrowser.Document == null && this.webBrowser.ActiveXInstance == null)
                    throw new ApplicationException ("Unable to initialize WebBrowser ActiveX control.");
    
                var ax = (SHDocVw.WebBrowser)this.webBrowser.ActiveXInstance;
                ax.NewWindow2 += (ref object ppDisp, ref bool Cancel) =>
                {
                    var popup = new RawBrowserPopup();
                    popup.Visible = true;
                    ppDisp = popup.WebBrowser.ActiveXInstance;
                };
    
                this.Load += (s, e) =>
                {
                    this.webBrowser.DocumentText = "<a target=\"_blank\" href=\"javascript:'<button onclick=\\'window.close()\\'>Close</button>'\">Go</a>";
                };
            }
    
            // create a WebBrowser instance
            void InitBrowser()
            {
                this.webBrowser = new WebBrowser();
                this.webBrowser.Dock = DockStyle.Fill;
                this.Controls.Add(this.webBrowser);
                this.webBrowser.Visible = true;
            }
        }
    
        // RawWebBrowser
    
        public class RawWebBrowser : AxHost
        {
            public RawWebBrowser()
                : base("8856f961-340a-11d0-a96b-00c04fd705a2")
            {
            }
    
            public event EventHandler Initialized;
    
            protected override void AttachInterfaces()
            {
                if (this.Initialized != null)
                    this.Initialized(this, new EventArgs());
            }
    
            public object ActiveXInstance
            {
                get
                {
                    return base.GetOcx();
                }
            }
        }
    
        // RawBrowserPopup
    
        public class RawBrowserPopup : Form
        {
            RawWebBrowser webBrowser;
    
            public RawWebBrowser WebBrowser
            {
                get { return this.webBrowser; }
            }
    
            public RawBrowserPopup()
            {
                this.webBrowser = new RawWebBrowser();
    
                this.webBrowser.Initialized += (s, e) =>
                {
                    var ax = (SHDocVw.WebBrowser)this.webBrowser.ActiveXInstance;
                    ax.NewWindow2 += (ref object ppDisp, ref bool Cancel) =>
                    {
                        var popup = new RawBrowserPopup();
                        popup.Visible = true;
                        ppDisp = popup.WebBrowser.ActiveXInstance;
                    };
    
                    ax.WindowClosing += (bool IsChildWindow, ref bool Cancel) =>
                    {
                        Cancel = true;
                        this.Close();
                    };
                };
    
                this.webBrowser.Dock = DockStyle.Fill;
                this.Controls.Add(this.webBrowser);
                this.webBrowser.Visible = true;
            }
        }
    }
    

    For more information, you can refer here

    http://stackoverflow.com/questions/20205688/web-browser-control-detect-when-js-window-close-is-called

    #Extended .NET 2.0 WebBrowser Control

    http://www.codeproject.com/Articles/13598/Extended-NET-2-0-WebBrowser-Control#GoalClose


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    2014年2月25日 2:30
  • 非常感谢您的答案,其中第一个问题我改进了一下,

    dynamic o=web.ReflectGetProperty("AxIWebBrowser2");
                ppDisp = o.Application;
                o.RegisterAsBrowser = true;
                CreateNewTab(web);

    这个可以实现弹出窗口并实现窗口之间元素访问

    关于第二个问题,正如你所答复的内容,根据的代码,我把它参与到我的代码里面 像这样

    dynamic o = webBrowser.ReflectGetProperty("AxIWebBrowser2");
                SHDocVw.WebBrowser ax = (SHDocVw.WebBrowser)o.Application;
                ax.WindowClosing += ax_WindowClosing;

    但是如果换成AxSHDocVw.AxWebBrowser而不使用SHDocVw.WebBrowser 无法实现WindowClosing事件,虽然她们使用的com都是(8856f961-340a-11d0-a96b-00c04fd705a2)这个我就有点模糊。不过真的感谢你的帮助,也感谢的建议,我的程序是WPF今后我会在WPF主题提问。

    2014年2月25日 19:08