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()]
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中弹出新窗口(续)的更多相关文章
- 强制所有网页链接在同一页面打开或者在TabControl中弹出新窗口
IEwebbrowser中老生常谈的话题. 一般的解决都是通过 // webBrowser.Navigating += WebBrowser_Navigating; 注册转跳前事件 private v ...
- C# WPF Webbrowser 强制所有网页链接在同一页面打开
只要搞懂Winform的 WPF稍微改一改就可以了 主类:负责跳转的 using System; using System.Collections.Generic; using System.Com ...
- C# Winform WebBrowser控件
C# WinForm WebBrowser 1.主要用途:使用户可以在窗体中导航网页. 2.注意:WebBrowser 控件会占用大量资源.使用完该控件后一定要调用 Dispose 方法,以便确保及时 ...
- .NET4.5 WFP中用WebBrowser获取/操作网页html代码
引言 想给自己之前写的网页小说爬虫程序更新换代,之前一直是用winform的形式写的程序,因此这一次更新打算把UI换成WPF(因为听说WPF很漂亮),顺便也以此引入WPF的学习. 那么作为网页爬虫程序 ...
- c# webbrowser 随机点击链接
HtmlElementCollection hec = webBrowser1.Document.All; ; i < hec.Count; i++) { if (hec[i].GetAttri ...
- paip.点击每个网页链接都提示下载的解决。
paip.点击每个网页链接都提示下载的解决. 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn. ...
- JavaScript 网页链接调用Android程序
如何让网页链接实现启动Android的应用,网上有说重写WebView相关的shouldOverrideUrlLoading方法,但是这种理论上能实现,因为你的网页不是仅仅被你自己的webview来浏 ...
- webBrowser中操作网页元素全攻略
原文 webBrowser中操作网页元素全攻略 1.获取非input控件的值: webBrowser1.Document.All["控件ID"].InnerText; 或webBr ...
- Winform WebBrowser引用IE版本问题
做了一个Winform的项目.项目里使用了WebBrowser控件.以前一直都以为WebBrowser是直接调用的系统自带的IE,IE是呈现出什么样的页面WebBrowser就呈现出什么样的页面.其实 ...
随机推荐
- php去除html
代码如下 //清除html function clearhtml($str){ $str = trim($str); $str = strip_tags($str,""); $st ...
- 从零开始搭建包含多个子系统的Vue工程项目
本文以windows为例,介绍支持多个子系统的Vue工程项目的搭建过程,相对于单一系统的工程,多个子系统引入了如下一些问题: 项目目录结构设计 打包结果设计:每个子系统可以独立发布上线 多布局实现:多 ...
- Deep Learning 学习笔记(3):Linear Regression 数据的预处理
为了获得良好的收敛,在进行梯度下降前,我们可以对数据进行预处理. 目标是使得数据大小在同一个数据数量级上,均值为零. 一般将数据放缩到(-1,1)区间, 我们可以对数据进行如下操作: 其中u1是数据的 ...
- python----python使用mysql
Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymql pymsql是Python中操作MySQL的模块,在windows中的安装: pip ...
- Unable to correct problems, you have held broken package
其实这篇接着上文(一),主要是解决samba安装的问题,中间又是一路曲折.不过这个问题也算是比较典型,有必要记录一下. #apt-get install smb* 安装失败.其实顺利的话,直接一条这样 ...
- 【283】ArcMap 中河流字体设置
左斜字体的设置 1. 右键属性设置如下,将字体角度如下设置,并点击改变样式的按钮 2. 首先设置颜色如下,然后设置加粗斜体,最后勾选 CJK character orientation 的复选框 C ...
- MySQL的blob类型
MySQL中的Blob类型 MySQL中存放大对象的时候,使用的是Blob类型.所谓的大对象指的就是图片,比如jpg.png.gif等格式的图片,文档,比如pdf.doc等,以及其他的文件.为了在数据 ...
- collections、time和datetime模块
主要内容: 一.collections模块 二.time模块 三.datetime模块 1️⃣ collection模块 1.什么是collections模块.干什么用? collections模块 ...
- saltstack系列(二)——zmq应答模式
python zeromq介绍 1.ZeroMQ并不是一个对socket的封装,不能用它去实现已有的网络协议. 2.有自己的模式,不同于更底层的点对点通讯模式. 3.有比tcp协议更高一级的协议(当然 ...
- java基础之io流总结一:io流概述
IO流概念: 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.io流是实现输入和输出的基础,可以方便的实现数据的输入和输出操作. IO流的分类: 根据处理数据类型的不同分为:字符流 ...