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

效果图

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. 1147 Heaps

    1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...

  2. 为工具箱添加CSKin选项卡

    如何使用CSKin 项目的引用→右键→添加; 找到SCKin.dll; 添加引用 工具箱新建一个选项卡; 工具箱的空白处→右键→添加选项卡→SKinControl, 将刚才的CSKin.dll 直接拖 ...

  3. python学习(十六) 测试

    测试驱动开发. 16.1 先测试,后编码 16.1.1 精确的需求说明 16.1.2 为改变而计划 16.1.3 测试的4个步骤 16.2 测试工具 16.2.1 doctest 16.2.2 uni ...

  4. oracle 11g R2 标准版 64位linux安装

    安装环境:Redhat es 5.5 64位 ,系统内存8G,swap 10G ,oracle 11G R2 标准版 一,Oracle 安装前的准备检查一下包,必须全部安装:binutils-2.17 ...

  5. .net 连接ORACLE中文显示乱码解决方案

    FYI由于历史的原因,早期的oracle没有中文字符集(如oracle6.oracle7.oracle7.1),但有的用户从那时起就使用数据库了, 并用US7ASCII字符集存储了中文,或是有的用户在 ...

  6. 使用CallableStatement接口调用存储过程

    直接上下代码: package com.learn.jdbc.chap07; import java.sql.CallableStatement; import java.sql.Connection ...

  7. ClientDataSet1.LoadFromStream

    lStream3.Position := 0;        lDataSet.LoadFromStream(lStream3);

  8. 【原创】基于UDP广播的局域网Web Window Service日志跟踪小工具

           一直感觉Web开发或者windows服务的日志跟踪调试不是很方便          特别是在生产环境服务器上面          目前一般的解决方案是通过各种日志工具把错误信息和调试信息 ...

  9. UIRect中的Anchor组件

    [UIRect中的Anchor组件] Anchor用于实现粘着功能,寄存于UIRect类中.Anchor的类型有三种: 1.None:不使用跟随功能. 2.Unified:四条边使用相同的Target ...

  10. Select2 的使用

    实现这个下拉列表框 下载这两个官网上的CSS,JS 官网地址 https://select2.org/getting-started/installation 我自己存的高速下载地址 http://y ...