C# WinForm Webbrowser 强制所有网页链接在同一页面打开或者在TabControl中弹出新窗口(续)

导读:本篇文章讲解 C# WinForm Webbrowser 强制所有网页链接在同一页面打开或者在TabControl中弹出新窗口(续),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

 上面那个文写的如同粑粑一样

效果图

C# WinForm Webbrowser 强制所有网页链接在同一页面打开或者在TabControl中弹出新窗口(续)

 

Winfrom 中添加这个类就好了

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IE
{
    public class WebBrowserUrl : CancelEventArgs
    {
        public String Url { get; }

        public String Frame { get; }

        public WebBrowserUrl(String url, String frame) : base()
        {
            this.Url = url;
            this.Frame = frame;
        }
    }

    public class WebBrowserUrl2
    {
        public String Url { get; }

        public String Frame { get; }

        public WebBrowserUrl2(String url, String frame)
        {
            this.Url = url;
            this.Frame = frame;
        }
    }
    public class WebBrowserEvent
    {
        public bool cancel;
    }



    public class NewWebBrwser : WebBrowser
    {
        System.Windows.Forms.AxHost.ConnectionPointCookie cookie;
        NewWebBrowserEvent events;

        public event EventHandler BeforeNavigate;

     //   public event EventHandler BeforeNewWindow;

        public delegate void OnNewWindow2(WebBrowserUrl2 webPra, WebBrowserEvent cancel);

        public event  OnNewWindow2 BeforeNewWindow2;

        protected override void CreateSink()
        {
            base.CreateSink();//还是需要源
            events = new NewWebBrowserEvent(this);
            cookie = new System.Windows.Forms.AxHost.ConnectionPointCookie(this.ActiveXInstance, events, typeof(INewDWWebBrowserEvent));
        }
        protected override void DetachSink()
        {
            if (null != cookie)
            {
                cookie.Disconnect();
                cookie = null;
            }
            base.DetachSink();
        }
        public void OnBeforeNavigate(string url, string frame, out bool cancel)
        {
            var Arg = BeforeNavigate;
            WebBrowserUrl webBrowserUrl = new WebBrowserUrl(url, frame);
            Arg?.Invoke(this, webBrowserUrl);
            cancel = webBrowserUrl.Cancel;
        }
        public void OnBeforeNewWindow(string url, out bool cancel)
        {
            //var Arg = BeforeNewWindow;
            //WebBrowserUrl webBrowserUrl = new WebBrowserUrl(url, null);
            //Arg?.Invoke(this, webBrowserUrl);
            //cancel = webBrowserUrl.Cancel;
            var Arg = BeforeNewWindow2;
            WebBrowserUrl2 webBrowserUrl = new WebBrowserUrl2(url, null);
            WebBrowserEvent webBrowserEvent = new WebBrowserEvent();
            Arg?.Invoke(webBrowserUrl,webBrowserEvent);
            cancel = webBrowserEvent.cancel;
        }
        
        
    }
    public class NewWebBrowserEvent : System.Runtime.InteropServices.StandardOleMarshalObject, INewDWWebBrowserEvent
    {
        private NewWebBrwser webBrowser;

        public NewWebBrowserEvent(NewWebBrwser newWebBrowser) => webBrowser = newWebBrowser;

        public void BeforeNavigate2(object pDisp, ref object urlObject, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel)
        {
            webBrowser.OnBeforeNavigate((string)urlObject, (string)targetFrameName, out cancel);
        }
        //官方说明此 事件是低于IE6时会引发
        public void NewWindow2(ref object ppDisp, ref bool cancel)
        {

            webBrowser.OnBeforeNewWindow(((WebBrowser)ppDisp).Url.ToString(), out cancel);
        }
        //当高于IE6时使用
        public void NewWindow3(object pDisp, ref bool cancel, ref object flags, ref object URLContext, ref object URL)
        {
            webBrowser.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 INewDWWebBrowserEvent
    {
        [System.Runtime.InteropServices.DispId(250)]
        void BeforeNavigate2(object pDisp, ref object urlObject, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel);
        //官方说明此 事件是低于IE6时会引发
        [System.Runtime.InteropServices.DispId(251)]
        void NewWindow2(ref object ppDisp, ref bool cancel);
        //当高于IE6时使用
        [System.Runtime.InteropServices.DispId(273)]
        void NewWindow3(object pDisp, ref bool cancel, ref object flags, ref object URLContext, ref object URL);
    }

}

 

 使用方式

C# WinForm Webbrowser 强制所有网页链接在同一页面打开或者在TabControl中弹出新窗口(续)

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/12675.html

(0)
小半的头像小半

相关推荐

极客之家——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!