window.close 关闭WebBrowser(高手那里转来的)测试已经成功

第一个简单应用里面讲述的是如何模拟调用当前网页的元素的事件或者赋值/取值。
这次的应用讲述的是
1:如何处理弹出新页面的事件(总是在我的浏览器里面现实新页面)
2:如何处理window.close事件,让我的浏览器页关闭
3:让html页面的js调用我的browse的函数
4:如何让我的browse调用html的js函数。

使用场景:一个web程序,让用户使用自定义浏览器来浏览,该web程序会调用浏览者机器上一些接口。
我的这个浏览器叫做AppBrowser。
关于ObjectForScripting 的介绍http://msdn2.microsoft.com/en-us/library/system.windows.forms.webbrowser.objectforscripting.aspx


首先,第一个问题。
如果只是放置一个browse在那里,在html中打开新页面的时候,他默认使用IE或者其他浏览器来打开网页。如果想要让我的browse也同时能处理所有的新开页面,就要增加一个对_NewWindow事件的处理。
        private void wb_Container_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
            AppBrowser newAB = new AppBrowser(wb_Container.Url.ToString());
            newAB.Show();
        }
在这里要注意的是
1:e.Cancel = true;是为了取消这个事件,不然又打开一个IE
2:wb_Container.Url.接受到的是新页面的参数

关于关闭浏览器
通常,如果设置了这样的js:window.close,那么,IE会自动关闭。但是我的browse却不会,至少默认的是如此的。
为了关闭我的浏览器,我需要接收这个函数。但是,很可惜,找了半天都没找到这个事件在那里处理,于是结合下一个问题,一下子解决了。(其实是半个解决,只有自己写的web程序才能处理)。

关于web调用我的浏览器的函数。
这就成了web和win的交互了,这个win就是在客户端的。以前交互的方式是写一个ActiveX控件,让web调用他,进而访问客户机器上的一些资源。现在的这种方式则是通过自己提供一个符合COM接口的自定义browse来实现。

1:我的browse必须是符合COM接口的[System.Runtime.InteropServices.ComVisibleAttribute(true)]


2:设置一个属性 this.wb_Container.ObjectForScripting = this
这样,Web中就可以这样调用了javascript:window.external.xxx('xx')。比方说上边的那个关闭窗口的调用就可以这样写:
οnclick="javascript:window.external.close();"
这个调用,其实是调用的我的browse的Close函数。这个函数是我的winForm上默认的那一个函数。调用其他函数亦然,只要是公开方法就可以。

关于如何browse调用web页面中的函数。
第一个,可以通过直接调用页面中元素的方式来实现,在我上一篇里面有所介绍。
第二个,就是可以直接访问.Document.InvokeScript函数来实现。
比如:
        public object InvokeHtmlJsScript(string scriptName,object[] objects)
        {
           return  this.wb_Container.Document.InvokeScript(scriptName, objects);
        }


简单的类
   //set this class as a COM
     [System.Runtime.InteropServices.ComVisibleAttribute(true)]
     public partial class AppBrowser : Form
     {
         public AppBrowser()
         {
             InitializeComponent();
         }
         /** <summary>
        /// which the url will be go
        /// </summary>
        /// <param name="url"></param>
       public AppBrowser(string url)
        {
           InitializeComponent();
            _url = url;
            this.wb_Container.Navigate(_url);
            this.wb_Container.ObjectForScripting = this;//set this to be the COM     handler
           
        }
        private string _url;
        public string Url
        {
            get { return this._url; }
        }
        /** <summary>
        /// open new page in the window which is also in my brower but not in IE
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void wb_Container_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
            AppBrowser newAB = new AppBrowser(wb_Container.Url.ToString());
            newAB.Show();
        }
        /** <summary>
        /// this function can be invoked by js in html
        /// like this 'javascript:window.external.ShowMessage('this is invoke from web');'
        /// </summary>
        /// <param name="msg"></param>
        public void ShowMessage(string msg)
        {
            MessageBox.Show(msg);
        }
        /** <summary>
        /// can invoke script in the html showing in the webbrowser
        /// </summary>
        /// <param name="scriptName"></param>
        /// <param name="objects"></param>
        /// <returns></returns>
        public object InvokeHtmlJsScript(string scriptName,object[] objects)
        {
           return  this.wb_Container.Document.InvokeScript(scriptName, objects);
        }
    }

后记:经过测试,终于找到了一种方法可以解决window.close的问题了(第二个问题)


    private void wb_Container_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
         {
             wb_Container.Document.Window.Unload += new HtmlElementEventHandler(Window_Unload);
         }
 
         void Window_Unload(object sender, HtmlElementEventArgs e)
         {
             if (this.wb_Container.Document == null)
                this.Close();
       }

//*****************************************//

以上为从高手哪里转载而来 经测试非常成功 总结下来就是在private void wb_Container_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
加入代码:

{
             wb_Container.Document.Window.Unload += new HtmlElementEventHandler(Window_Unload);
         }
 
         void Window_Unload(object sender, HtmlElementEventArgs e)
         {
             if (this.wb_Container.Document == null)
                this.Close();
       }

 
————————————————
版权声明:本文为CSDN博主「charlie1190」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/charlie1190/article/details/6116620

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值