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

效果图

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()]
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()]
void NewWindow2(ref object ppDisp, ref bool cancel);
//当高于IE6时使用
[System.Runtime.InteropServices.DispId()]
void NewWindow3(object pDisp, ref bool cancel, ref object flags, ref object URLContext, ref object URL);
} }

使用方式

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

  1. 强制所有网页链接在同一页面打开或者在TabControl中弹出新窗口

    IEwebbrowser中老生常谈的话题. 一般的解决都是通过 // webBrowser.Navigating += WebBrowser_Navigating; 注册转跳前事件 private v ...

  2. C# WPF Webbrowser 强制所有网页链接在同一页面打开

    只要搞懂Winform的  WPF稍微改一改就可以了 主类:负责跳转的 using System; using System.Collections.Generic; using System.Com ...

  3. C# Winform WebBrowser控件

    C# WinForm WebBrowser 1.主要用途:使用户可以在窗体中导航网页. 2.注意:WebBrowser 控件会占用大量资源.使用完该控件后一定要调用 Dispose 方法,以便确保及时 ...

  4. .NET4.5 WFP中用WebBrowser获取/操作网页html代码

    引言 想给自己之前写的网页小说爬虫程序更新换代,之前一直是用winform的形式写的程序,因此这一次更新打算把UI换成WPF(因为听说WPF很漂亮),顺便也以此引入WPF的学习. 那么作为网页爬虫程序 ...

  5. c# webbrowser 随机点击链接

    HtmlElementCollection hec = webBrowser1.Document.All; ; i < hec.Count; i++) { if (hec[i].GetAttri ...

  6. paip.点击每个网页链接都提示下载的解决。

    paip.点击每个网页链接都提示下载的解决.   作者Attilax  艾龙,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn. ...

  7. JavaScript 网页链接调用Android程序

    如何让网页链接实现启动Android的应用,网上有说重写WebView相关的shouldOverrideUrlLoading方法,但是这种理论上能实现,因为你的网页不是仅仅被你自己的webview来浏 ...

  8. webBrowser中操作网页元素全攻略

    原文 webBrowser中操作网页元素全攻略 1.获取非input控件的值: webBrowser1.Document.All["控件ID"].InnerText; 或webBr ...

  9. Winform WebBrowser引用IE版本问题

    做了一个Winform的项目.项目里使用了WebBrowser控件.以前一直都以为WebBrowser是直接调用的系统自带的IE,IE是呈现出什么样的页面WebBrowser就呈现出什么样的页面.其实 ...

随机推荐

  1. fullCalendar动态获取数据

    fullCalendar http://fullcalendar.io/docs/event_data/events_function $('#calendar').fullCalendar({ he ...

  2. php.ini修改php上传文件大小限制

    打开php.ini,首先找到file_uploads = on ;是否允许通过HTTP上传文件的开关.默认为ON即是开upload_tmp_dir ;文件上传至服务器上存储临时文件的地方,如果没指定就 ...

  3. python开发IO模型:阻塞&非阻塞&异步IO&多路复用&selectors

    一 IO模型介绍 为了更好地了解IO模型,我们需要事先回顾下:同步.异步.阻塞.非阻塞 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非 ...

  4. Invalid byte tag in constant pool: 19

    环境: windows 2008 server R2   ; tomcat 8.5.3 ;   jdk-1.8.0_91 故障截图: 报的就是 Invalid byte tag in constant ...

  5. Oracle 分区表-Range分区

    原文:http://www.tuicool.com/articles/MzeM7r 一.什么是分区表 Oracle提供了分区技术以支持VLDB(Very Large DataBase).分区表通过对分 ...

  6. 小记一次mysql启动失败没有日志的处理

    本来mysql好的,之前清理了一次tmp下的东西,mysql在查询的时候提示: Can't create/write to file '/tmp/ib0n3frL 然后停止启动: [root@sevc ...

  7. Python Twisted系列教程6:抽象地利用Twisted

    作者:dave@http://krondo.com/and-then-we-took-it-higher/  译者:杨晓伟(采用意译) 你可以从这里从头开始阅读这个系列. 打造可以复用的诗歌下载客户端 ...

  8. 04.CSS的继承性和层叠性

    CSS有两大特性:  继承性和层叠性 继承性 面向对象语言都会存在继承的概念 , 在面向对象语言中, 继承的特点:  继承了父类的属性和方法.  那么 css  就是在设置属性的 ,  不会牵扯到方法 ...

  9. MySQL 示例数据库sakila-db的安装

    最近在看 “高性能MySql”这本神书,发现上面很多例子采用的官方示例数据库sakila. 官方示例数据库 下载地址 http://dev.mysql.com/doc/index-other.html ...

  10. BurpSuite—-Spider模块(蜘蛛爬行)

    一.简介 Burp Spider 是一个映射 web 应用程序的工具.它使用多种智能技术对一个应用程序的内容和功能进行全面的清查. Burp Spider 通过跟踪 HTML 和 JavaScript ...