1. 程式人生 > >C# WinForm Webbrowser 強制所有網頁連結在同一頁面開啟或者在TabControl中彈出新視窗(續)

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); } }

 

 使用方式