MainWindow.xaml.cs


//新窗口事件
               {
Uri uri = new Uri(textBox_uri.Text);
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
ExtendedWebBrowser webBrowser1 =
new ExtendedWebBrowser();
host.Child = webBrowser1;
this.webGrid.Children.Add(host);
webBrowser1.BeforeNewWindow +=
new EventHandler<WebBrowserExtendedNavigatingEventArgs>(webBrowser1_BeforeNewWindow);
webBrowser1.Url = uri;
webBrowser1.ScriptErrorsSuppressed = true;//禁止弹出脚本错误
}
//新窗口事件
void webBrowser1_BeforeNewWindow(object sender, WebBrowserExtendedNavigatingEventArgs e)
{
e.Cancel = true;
((ExtendedWebBrowser)sender).Navigate(e.Url);
            textBox_uri.Text = e.Url;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
using System.IO;
using Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;
using System.Windows.Xps.Packaging;

ExtendedWebBrowser.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace WPF_WebBrowser
{
public class ExtendedWebBrowser : System.Windows.Forms.WebBrowser
{
System.Windows.Forms.AxHost.ConnectionPointCookie cookie;
WebBrowserExtendedEvents events; //This method will be called to give you a chance to create your own event sink
protected override void CreateSink()
{
//MAKE SURE TO CALL THE BASE or the normal events won't fire
base.CreateSink();
events = new WebBrowserExtendedEvents(this);
cookie = new System.Windows.Forms.AxHost.ConnectionPointCookie(this.ActiveXInstance, events, typeof(DWebBrowserEvents2));
} protected override void DetachSink()
{
if (null != cookie)
{
cookie.Disconnect();
cookie = null;
}
base.DetachSink();
} //This new event will fire when the page is navigating
public event EventHandler<WebBrowserExtendedNavigatingEventArgs> BeforeNavigate;
public event EventHandler<WebBrowserExtendedNavigatingEventArgs> BeforeNewWindow; protected void OnBeforeNewWindow(string url, out bool cancel)
{
EventHandler<WebBrowserExtendedNavigatingEventArgs> h = BeforeNewWindow;
WebBrowserExtendedNavigatingEventArgs args = new WebBrowserExtendedNavigatingEventArgs(url, null);
if (null != h)
{
h(this, args);
}
cancel = args.Cancel;
} protected void OnBeforeNavigate(string url, string frame, out bool cancel)
{
EventHandler<WebBrowserExtendedNavigatingEventArgs> h = BeforeNavigate;
WebBrowserExtendedNavigatingEventArgs args = new WebBrowserExtendedNavigatingEventArgs(url, frame);
if (null != h)
{
h(this, args);
}
//Pass the cancellation chosen back out to the events
cancel = args.Cancel;
}
//This class will capture events from the WebBrowser
class WebBrowserExtendedEvents : System.Runtime.InteropServices.StandardOleMarshalObject, DWebBrowserEvents2
{
ExtendedWebBrowser _Browser;
public WebBrowserExtendedEvents(ExtendedWebBrowser browser) { _Browser = browser; } //Implement whichever events you wish
public void BeforeNavigate2(object pDisp, ref object URL, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel)
{
_Browser.OnBeforeNavigate((string)URL, (string)targetFrameName, out cancel);
} public void NewWindow3(object pDisp, ref bool cancel, ref object flags, ref object URLContext, ref object URL)
{
_Browser.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 DWebBrowserEvents2
{ [System.Runtime.InteropServices.DispId()]
void BeforeNavigate2(
[System.Runtime.InteropServices.In,
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)] object pDisp,
[System.Runtime.InteropServices.In] ref object URL,
[System.Runtime.InteropServices.In] ref object flags,
[System.Runtime.InteropServices.In] ref object targetFrameName, [System.Runtime.InteropServices.In] ref object postData,
[System.Runtime.InteropServices.In] ref object headers,
[System.Runtime.InteropServices.In,
System.Runtime.InteropServices.Out] ref bool cancel);
[System.Runtime.InteropServices.DispId()]
void NewWindow3(
[System.Runtime.InteropServices.In,
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)] object pDisp,
[System.Runtime.InteropServices.In, System.Runtime.InteropServices.Out] ref bool cancel,
[System.Runtime.InteropServices.In] ref object flags,
[System.Runtime.InteropServices.In] ref object URLContext,
[System.Runtime.InteropServices.In] ref object URL); }
} public class WebBrowserExtendedNavigatingEventArgs : System.ComponentModel.CancelEventArgs
{
private string _Url;
public string Url
{
get { return _Url; }
} private string _Frame;
public string Frame
{
get { return _Frame; }
} public WebBrowserExtendedNavigatingEventArgs(string url, string frame)
: base()
{
_Url = url;
_Frame = frame;
}
}
}

WPF中的WebBrowser的更多相关文章

  1. 浏览器扩展系列————在WPF中定制WebBrowser快捷菜单

    原文:浏览器扩展系列----在WPF中定制WebBrowser快捷菜单 关于如何定制菜单可以参考codeproject上的这篇文章:http://www.codeproject.com/KB/book ...

  2. 在WPF中嵌入WebBrowser可视化页面

    无论是哪种C/S技术,涉及数据可视化就非常的累赘了,当然大神也一定有,只不过面向大多数人,还是通过网页来实现,有的时候不想把这两个功能分开,一般会是客户的原因,所以我们打算在WPF中嵌入WebBrow ...

  3. WPF中禁止WebBrowser控件打开新窗口

    一.针对纯WPF的WebBrowser控件: <summary> Suppress Script Errors In WPF WebBrowser </summary> pub ...

  4. WPF中使用WebBrowser

    最近在做北京现代项目的时候,遇到一个需求将韩国那边写好的网页嵌套到WPF程序中显示. 开始的时候使用的是第三方的浏览器控件:awesomium,在本地测试,显示没有问题.但是拿到客户现场,只显示半屏. ...

  5. C# WPF 中WebBrowser拖动来移动窗口,改变窗口位置

    前言 wpf中的WebBrowser相比之前的winform阉割了不少东西,也增加了不少东西,但是msdn对wpf也没有较好的文档 WebBrowser可以说是一个.NET控件,相对于WPF中的控件, ...

  6. Unity3D 将 Unity 嵌入WPF中的一些研究笔记

     一. 在 WPF 中使用 WebBrowser,直接打开 WebPlayer.html 以这种方式有一个问题是. 无法在 WebBrowser 的上面 放置其它的控件, 在运行时,都不会显示 . 以 ...

  7. WPF中展示HTML

    业务需求:将具有表格信息的HTML片段在WPF中展示出来,并像网页端一样,可以进行input的填写,checkbox选择,最后以HTML的形式完成保存. 天真的以为直接引入WPF中的WebBrowse ...

  8. WPF中不规则窗体与WebBrowser控件的兼容问题解决办法

    原文:WPF中不规则窗体与WebBrowser控件的兼容问题解决办法 引言 这几天受委托开发一个网络电视项目,要求初步先使用内嵌网页形式实现视频播放和选单,以后再考虑将网页中的所有功能整合进桌面程序. ...

  9. WPF中嵌入WinForm中的webbrowser控件

    原文:WPF中嵌入WinForm中的webbrowser控件 使用VS2008创建WPF应用程序,需使用webbrowser.从工具箱中添加WPF组件中的webbrowser发现其中有很多属性事件不能 ...

随机推荐

  1. Android(java)学习笔记113:Android编写代码调用Vibrator震动功能(Bug:按下按钮button始终没有震动)

    1.之前我编写的代码是如下: package com.himi.vibrate; import android.app.Activity; import android.app.Service; im ...

  2. javascript编程的最佳实践推荐

    推荐的javascript编程的最佳实践,摘要记录在这里: 可维护的代码保证代码的性能部署代码 1 可维护的代码1.1什么是维护的代码:可理解性——其他人可以接手代码并理解它的意图和一般途径,而无需原 ...

  3. 关于IE8不支持document.getElementById().innerHTML的问题

    document.getElementById("id").innerHTML = (showinfo);//IE8不支持. 可以用Jquery来解决这个问题: $('#id'). ...

  4. 汇总#pragma用法

    这几天忙着去复习了,但是心理总是不踏实,不到实验室里就觉得一天的生活变了个样,现在还是晚上来这里“搞起”吧,白天还是在复习准备考试.因为要开始学习freescale,准备明年的比赛了,觉得是时候开始搞 ...

  5. 小议jQuery中的事件

    学了jQuery这么长时间,到这里真的有一种柳暗花明又一村的感觉,在这里先表达一下自己学这一章节的happy心情吧(在严厉的金工实习老师眼皮底下偷偷学习,当然还有各种nerves~). 1加载DOM ...

  6. ActiveMQ 的安装

    1. 在 http://activemq.apache.org/ 下载 ActiveMQ.Windows 系统选择下载 apache-activemq-x.x.x-bin.zip,Unix/Linux ...

  7. PB11.5创建及调用WebService

    我今天做了这个“PB创建及调用WebService”的小作业,下面我来分享一下我的整个实践过程以及我遇到的问题,希望对大家有所帮助,这个小作业是分以下三部来完成的: 一.构建WebService ①创 ...

  8. JavaScript 参考手册

    http://www.w3school.com.cn/jsref/index.asp https://developer.mozilla.org/zh-CN/docs/Web/JavaScript

  9. android不依赖具体activity弹出Dialog对话框,即全局性对话框

    最近在DialogUtil类中声明了一个静态的弹出Dialog方法,弹出的Dialog也是静态的,并且只在第一次进行创建,由于Dialog弹出依附于Activity,所以就出现了问题. 即:第一次调用 ...

  10. Linux命令(7):rm命令

    1.作用: 删除一个目录中的一个或多个文件或目录: 2.格式: rm  [选项] 文件或目录 3.常见参数: 4.使用实例: [root@www hello]# rm –r -i ./why 5.使用 ...