论WebBrowser捕获window.close

论WebBrowser捕获window.close 

     最近在做项目的时候用到了WebBrowser,需要在关闭网页的同时关闭WebBroser所在的WinForm界面,在网上及MSDN找到的方法都不是很好,偶然在code project中找到了比较好的方法,特意摘录再次,供大家分享。

 

实现方案:

  1. 重写 WndProc 方法
  2. 判断 WM_PARENTNOTIFY 消息
  3. 判断 WM_DESTROY 消息参数
  4. 执行事件委托方法

实现源码参考

#region Raises the Quit event when the browser window is about to be destroyed  
 
        /// <summary>  
        /// Overridden  
        /// </summary>  
        /// <param name="m">The <see cref="Message"/> send to this procedure</param>  
        [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]  
        protected override void WndProc(ref Message m)  
        {  
            if (m.Msg == (int)WindowsMessages.WM_PARENTNOTIFY)  
            {  
                //int lp = m.LParam.ToInt32();  
                int wp = m.WParam.ToInt32();  
 
                int X = wp & 0xFFFF;  
                //int Y = (wp >> 16) & 0xFFFF;  
                if (X == (int)WindowsMessages.WM_DESTROY)  
                    this.OnQuit();  
            }  
 
            base.WndProc(ref m);  
        }  
 
        /// <summary>  
        /// A list of all the available window messages  
        /// </summary>  
        enum WindowsMessages  
        {  
            WM_DESTROY = 0x2,   
            WM_PARENTNOTIFY = 0x210
        }  
 
        /// <summary>  
        /// Raises the <see cref="Quit"/> event  
        /// </summary>  
        protected void OnQuit()  
        {  
            EventHandler h = Quit;  
            if (null != h)  
                h(this, EventArgs.Empty);  
        }  
 
        /// <summary>  
        /// Raised when the browser application quits  
        /// </summary>  
        /// <remarks>  
        /// Do not confuse this with DWebBrowserEvents2.Quit... That's something else.  
        /// </remarks>  
        public event EventHandler Quit; 
 
 
        #endregion 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值