只要搞懂Winform的  WPF稍微改一改就可以了

主类:负责跳转的

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using static System.Windows.Forms.AxHost; namespace Test
{ 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 WebbrowserOnNewWindow
{
static readonly BindingFlags info =
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.FlattenHierarchy |
BindingFlags.CreateInstance; private WebBrowser web;
public WebbrowserOnNewWindow(WebBrowser webBrowser)
{
web = webBrowser ?? throw new Exception();
Excetue();
}
//跳转参考这个
public delegate void OnNewWindow(WebBrowserUrl2 webBrowserUrl, WebBrowserEvent webBrowserEvent); public event OnNewWindow BeforeNewWidnow; private void Excetue()
{ var propertyAxiWebbrowser = web.GetType().GetProperty("AxIWebBrowser2", info);
var axIWebBrowser2 = propertyAxiWebbrowser.GetValue(web, null);
var webBrowserEvent = new NewWebBrowserEvent(this);
var cookie = new ConnectionPointCookie(axIWebBrowser2, webBrowserEvent, typeof(INewDWWebBrowserEvent));
} public void OnBeforeNewWindow(string url, out bool cancel)
{
var Arg = BeforeNewWidnow;
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 WebbrowserOnNewWindow web;
public NewWebBrowserEvent(WebbrowserOnNewWindow webbrowserOnNewWindow) => web = webbrowserOnNewWindow; public void BeforeNavigate2(object pDisp, ref object urlObject, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel)
{
//WPF这个和Winform差不多 没写
//webBrowser.OnBeforeNavigate((string)urlObject, (string)targetFrameName, out cancel);
}
//官方说明此 事件是低于IE6时会引发
public void NewWindow2(ref object ppDisp, ref bool cancel)
{
//WPF这个和Winform差不多 没写
// 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)
{
web.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);
}
}

XAML 界面

//省略其他

<Grid>
<WebBrowser x:Name="WB" HorizontalAlignment="Left" Height="" Margin="10,10,0,0" VerticalAlignment="Top" Width="" Source="http://www.baidu.com" LoadCompleted="WB_LoadCompleted" />
</Grid>

XAML.CS

  public partial class MainWindow : Window
{
private bool Start;
public MainWindow()
{
InitializeComponent(); }
private void WB_LoadCompleted(object sender, NavigationEventArgs e)
{
if (!Start)
{ //这一步是没法省了
//webbrowser在WPF中时密封类型
WebbrowserOnNewWindow onNewWindow = new WebbrowserOnNewWindow(WB);
onNewWindow.BeforeNewWidnow += OnNewWindow_BeforeNewWidnow;
Start = true;
}
}
private void OnNewWindow_BeforeNewWidnow(WebBrowserUrl2 webBrowserUrl, WebBrowserEvent webBrowserEvent)
{
WB.Navigate(new Uri(webBrowserUrl.Url));
webBrowserEvent.cancel = true;
} }

C# WPF Webbrowser 强制所有网页链接在同一页面打开的更多相关文章

  1. C# WinForm Webbrowser 强制所有网页链接在同一页面打开或者在TabControl中弹出新窗口(续)

    上面那个文写的如同粑粑一样 效果图 Winfrom 中添加这个类就好了 using System; using System.Collections.Generic; using System.Com ...

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

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

  3. WPF WebBrowser Memory Leak 问题及临时解决方法

    首先介绍一下内存泄漏(Memory Leak)的概念,内存泄露是指程序中已动态分配的堆内存由于某种原因未释放或者无法释放,造成系统内存的浪费,导致程序运行速度减慢甚至系统崩溃等严重后果. 最近在使用W ...

  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. WPF WebBrowser+TabControl MVVM模式 简单应用 提供源码下载

    源代码下载 这个程序是TabControl和Webbrowser的练手小程序 可达到练手目的有: MVVM设计模式的基本使用 Binding(包括相对源[RelativeSource]绑定)的基本使用 ...

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

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

  8. WPF WebBrowser屏蔽弹出alert ,confirm ,prompt ,showModalDialog() ,window.open()

    WPF WebBrowser屏蔽弹出alert ,confirm ,prompt ,showModalDialog() ,window.open()添加Microsoft.mshtml.dll,然后写 ...

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

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

随机推荐

  1. win2003能支持的最高python版本为3.3.5版本

    https://www.python.org/downloads/windows/

  2. Windows可信任路径代码执行漏洞

    乌云里有很多这样的案例,当然在开发过程中也会存在这样的问题 搜索:可信任路径漏洞 调用案例中的描述: Microsoft Windows API使用CreateProcess()函数创建新的进程及其主 ...

  3. 界面主窗体,子窗体的InitializeComponent(构造函数)、Load事件执行顺序

    主窗体,子窗体的InitializeComponent(构造函数).Load事件执行顺序1.执行主窗体定义事件 new函数时,同时执行主窗体构造函数,默认就一个InitializeComponent函 ...

  4. Django Rest Framework 3

    目录 一.版本 二.解析器 三.序列化 四.请求数据验证 一.版本 程序也来越大时,可能通过版本不同做不同的处理 没用rest_framework之前,我们可以通过以下这样的方式去获取. 1 clas ...

  5. leetcode343

    public class Solution { public int IntegerBreak(int n) { ) { ; } ) { ; } var max = int.MinValue; ; i ...

  6. 什么是jsonp?——使用jsonp解决跨域请求问题

    我们在使用ajax请求的时候经常会产生跨域问题,这是由于浏览器的同源策略导致的.所谓同源,即域名.协议.端口均相同,否则不管是静态页面还是动态网页或者web服务都无法通过ajax正常请求.有时候,我们 ...

  7. solrCloud选举leader的逻辑分析

    First call *setup(ElectionContext) to ensure the election process is in it'd.    Next calljoinElecti ...

  8. 解决在Python中使用Win32api报错的问题,No module named win32api

    一.系统环境 操作系统: Win7 64位 Python:3.7.0 二.在使用import win32api时,报错:No module named win32api 网上查到有下面解决办法: 方法 ...

  9. Ubuntu16.04 ARM平台移植libcurl curl-7.63.0

    libcurl是免费的轻量级的客户端网络库,支持DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS,POP3, P ...

  10. OpenCV 2.4.13 installed in Ubuntu 14 and CMakeLists Demo

    1. 配置编译器环境 [compiler] sudo apt-get install build-essential 2. 安装OpenCV的依赖包 [required] -dev pkg-confi ...