C#重写WebBrowser组件,禁止跳转到IE新窗口、脚本错误

原创地址
刚从delphi转到c#,一切重头开始。上来先做个myBrowser,结果发现无法解决跳转到新窗口问题。从网上找到奇客力大侠的《C# webBrowser禁止在新窗口打开,强制在本窗口打开》文章(http://blog.163.com/da7_1@126/blog/static/104072678201031651754880/),结果发现对重写WebBrowser一窍不通,折腾了2天,又找到百度知道里的一篇问答《在C#中如何重写控件》(http://zhidao.baidu.com/question/48991857.html?fr=qrl&cid=869&index=1&fr2=query),再几经周折才搞定。
为了纪念第一个C#程序的艰难问世,现将过程详细记录如下:
    一、在VS2008里用C#新建一个Windows窗体应用程序,起名为MyBrowser。
    二、在里面添加Label、TextBox、Button,然后整成如下图的样式,同时把各个控件的Anchor和Text归整一下,位置和名称放放好。

    三、重写WebBrowser组件,禁止跳转到IE新窗口。菜单“项目->添加类”,在模板中的“类”图标上确认一下,然后名称改为“ExtendedWebBrowser.cs”。
    (一)在右边解决方案管理器中右击“查看代码”,然后在ExtendedWebBrowser.cs代码窗口将代码修改成如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyBrowser
{
    public class ExtendedWebBrowser : System.Windows.Forms.WebBrowser
    {
        System.Windows.Forms.AxHost.ConnectionPointCookie cookie;
        WebBrowserExtendedEvents events;
        //This method will be called to give you a chance to create your own event sink
        protected override void CreateSink()
        {
            //MAKE SURE TO CALL THE BASE or the normal events won't fire
            base.CreateSink();
            events = new WebBrowserExtendedEvents(this);
            cookie = new System.Windows.Forms.AxHost.ConnectionPointCookie(this.ActiveXInstance, events, typeof(DWebBrowserEvents2));
        }
        protected override void DetachSink()
        {
            if (null != cookie)
            {
                cookie.Disconnect();
                cookie = null;
            }
            base.DetachSink();
        }
        //This new event will fire when the page is navigating
        public event EventHandler<WebBrowserExtendedNavigatingEventArgs> BeforeNavigate;
        public event EventHandler<WebBrowserExtendedNavigatingEventArgs> BeforeNewWindow;
        protected void OnBeforeNewWindow(string url, out bool cancel)
        {
            EventHandler<WebBrowserExtendedNavigatingEventArgs> h = BeforeNewWindow;
            WebBrowserExtendedNavigatingEventArgs args = new WebBrowserExtendedNavigatingEventArgs(url, null);
            if (null != h)
            {
                h(this, args);
            }
            cancel = args.Cancel;
        }
        protected void OnBeforeNavigate(string url, string frame, out bool cancel)
        {
            EventHandler<WebBrowserExtendedNavigatingEventArgs> h = BeforeNavigate;
            WebBrowserExtendedNavigatingEventArgs args = new WebBrowserExtendedNavigatingEventArgs(url, frame);
            if (null != h)
            {
                h(this, args);
            }
            //Pass the cancellation chosen back out to the events
            cancel = args.Cancel;
        }
        //This class will capture events from the WebBrowser
        class WebBrowserExtendedEvents : System.Runtime.InteropServices.StandardOleMarshalObject, DWebBrowserEvents2
        {
            ExtendedWebBrowser _Browser;
            public WebBrowserExtendedEvents(ExtendedWebBrowser browser) { _Browser = browser; }
            //Implement whichever events you wish
            public void BeforeNavigate2(object pDisp, ref object URL, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel)
            {
                _Browser.OnBeforeNavigate((string)URL, (string)targetFrameName, out cancel);
            }
            public void NewWindow3(object pDisp, ref bool cancel, ref object flags, ref object URLContext, ref object URL)
            {
                _Browser.OnBeforeNewWindow((string)URL, out cancel);
            }
        }
        [System.Runtime.InteropServices.ComImport(), System.Runtime.InteropServices.Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"),
        System.Runtime.InteropServices.InterfaceTypeAttribute(System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch),
        System.Runtime.InteropServices.TypeLibType(System.Runtime.InteropServices.TypeLibTypeFlags.FHidden)]
        public interface DWebBrowserEvents2
        {
            [System.Runtime.InteropServices.DispId(250)]
            void BeforeNavigate2(
                [System.Runtime.InteropServices.In,
                System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)] object pDisp,
                [System.Runtime.InteropServices.In] ref object URL,
                [System.Runtime.InteropServices.In] ref object flags,
                [System.Runtime.InteropServices.In] ref object targetFrameName, [System.Runtime.InteropServices.In] ref object postData,
                [System.Runtime.InteropServices.In] ref object headers,
                [System.Runtime.InteropServices.In,
                System.Runtime.InteropServices.Out] ref bool cancel);
            [System.Runtime.InteropServices.DispId(273)]
            void NewWindow3(
                [System.Runtime.InteropServices.In,
                System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)] object pDisp,
                [System.Runtime.InteropServices.In, System.Runtime.InteropServices.Out] ref bool cancel,
                [System.Runtime.InteropServices.In] ref object flags,
                [System.Runtime.InteropServices.In] ref object URLContext,
                [System.Runtime.InteropServices.In] ref object URL);
        }
    }
    public class WebBrowserExtendedNavigatingEventArgs : System.ComponentModel.CancelEventArgs
    {
        private string _Url;   //原文此处多了一个空格,注意修改之...散仙闪电注
        public string Url
        {
            get { return _Url; }
        }
        private string _Frame;  //原文此处多了一个空格,注意修改之...散仙闪电注
        public string Frame
        {
            get { return _Frame; }
        }
        public WebBrowserExtendedNavigatingEventArgs(string url, string frame)
            : base()
        {
            _Url = url;
            _Frame = frame;
        }
    }
  }
    (二)回到Form1.cs[设计]窗口,在菜单“生成”中,点“生成解决方案”。一会之后在工具箱的最上方就会出现一个新的组件“ExtendedWebBrowser”,这正是我们需要的,hehe。
    三、回到Form1.cs[设计]窗口,把ExtendedWebBrowser拖进来。
    (一)在属性窗口里调整好Anchor,使之能最大化。
    (二)双击“ScriptErrorSuppressed”,将之属性改为“True”以禁用所有的对话框,比如提示Activex下载、执行以及安全登录等对话框。当然可以参考MSDN上的代码示例(http://apps.hi.baidu.com/share/detail/379735),有的放矢:
private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    ((WebBrowser)sender).Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);
}
private void Window_Error(object sender, HtmlElementErrorEventArgs e)
{
    // Ignore the error and suppress the error dialog box. 
    e.Handled = true;
}
   (三)在ExtendedWebBrowser的事件里双击“BeforeNewWindow”并添加2行代码:
    private void extendedWebBrowser1_BeforeNewWindow(object sender, MyBrowser.WebBrowserExtendedNavigatingEventArgs e)
        {
           e.Cancel=true;
           ((ExtendedWebBrowser)sender).Navigate(e.Url);
        }
   (四)最后一步,回到Form1.cs[设计]窗口,双击Button按钮,添加代码如下:
      private void button1_Click(object sender, EventArgs e)
        {
            extendedWebBrowser1.Navigate(textBox1.Text);
        }
    
   F5运行,点几个老跳的链接,oh~yeah,成功了!
  ??,还有些不爽,输完网址后还要点登录,像IE一样敲一下回车就好了。说干就干
  (五)TextBox事件中,双击KeyPress(不要用Enter事件,试过不行),添加以下代码
  
     private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                button1_Click(null,null);
            }
        }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值