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); } }
使用方式
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现