只要搞懂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. php获取当前月月初至月末的时间戳,上个月月初至月末的时间戳

    当前月 <?php $thismonth = date('m'); $thisyear = date('Y'); $startDay = $thisyear . '-' . $thismonth ...

  2. List转Datatable 新方法

    方法1,最简单的转换 DataTable dt = new DataTable(); dt.Columns.Add("id"); dt.Columns.Add("name ...

  3. Oracle11gr2_ADG管理之跳归档恢复dg实战

    模拟故障 关闭备库 SQL> shutdown immediate; Database closed. Database dismounted. ORACLE instance shut dow ...

  4. ___pInvalidArgHandler already defined in LIBCMTD.lib(invarg.obj)

    vs2013编译项目时出错,网上很多的解决方案全都是垃圾,根本不能用 不过也有不是垃圾的,就是下面这个: 关于采用静态链接编译生成EXE库函数重复定义问题 看了好多关于类似LIBCMT.lib(inv ...

  5. ffmpeg个人翻译文档1-8<转>

    [个人翻译]ffmpeg文档1 (2008-08-26 09:39:15) 转载 标签: 杂谈 分类: 翻译文档 指导1:制作屏幕录像 源代码:tutorial01.c 概要   电影文件有很多基本的 ...

  6. webapi help文档 添加测试功能

    在做webapi项目的时候 webapi为我们提供了help文档,开发者可以参考这个文档,但是这个文档缺少测试功能,如果加上一个测试的功能就更加方便了 于是就研究了下写了一段代码,代码比较简单,只要将 ...

  7. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 3_Linear Algebra Review

    Lecture3   Linear Algebra Review 线性代数回顾 3.1 矩阵和向量3.2 加法和标量乘法3.3 矩阵向量乘法3.4 矩阵乘法3.5 矩阵乘法的性质3.6 逆.转置 3. ...

  8. 【原】Zookeeper 概述 + 官网 Overview 翻译

    分布式应用 分布式应用 distributed application可以在给定时间(同时)在网络中的多个系统上运行,通过协调它们以快速有效的方式完成特定任务. (a), (b): a distrib ...

  9. Variable hoisting Function hoisting

    Variable hoisting Another unusual thing about variables in JavaScript is that you can refer to a var ...

  10. Asp.net 动态添加Meta标签

    下面代码动态设置浏览器文档模式 HtmlHead head = (HtmlHead)Page.Header; HtmlMeta contentType = new HtmlMeta();//显示字符集 ...