onbeforenavigate2 打开一个html文件,WPF中使用webbrowser并且屏蔽在新窗口中打开页面...

本文介绍了如何使用C#创建WebBrowserHelper类,实现在Windows应用程序中监听Web浏览器事件,包括新窗口导航控制,适用于处理跨域请求和用户体验。核心代码展示了如何对接WebBrowser控件的事件处理和Cookie管理。
摘要由CSDN通过智能技术生成

此代码为从网上找到的方法,然后整理成一个文件方便引用

首先创建一个类文件 WebBrowserHelper.csusing System;

using System.ComponentModel;

using System.Windows.Controls;

using System.Globalization;

using System.Windows.Threading;

using System.Runtime.InteropServices;

using System.Reflection;

namespace Taobaoke

{

public partial class WebBrowserHelper

{

private WebBrowser _webBrowser;

private object _cookie;

public event CancelEventHandler NewWindow;

public WebBrowserHelper(WebBrowser webBrowser)

{

if (webBrowser == null)

throw new ArgumentNullException("webBrowser");

_webBrowser = webBrowser;

_webBrowser.Dispatcher.BeginInvoke(new Action(Attach), DispatcherPriority.Loaded);

}

public void Disconnect()

{

if (_cookie != null)

{

_cookie.ReflectInvokeMethod("Disconnect", new Type[] { }, null);

_cookie = null;

}

}

private void Attach()

{

var axIWebBrowser2 = _webBrowser.ReflectGetProperty("AxIWebBrowser2");

var webBrowserEvent = new WebBrowserEvent(this);

var cookieType = typeof(WebBrowser).Assembly.GetType("MS.Internal.Controls.ConnectionPointCookie");

_cookie = Activator.CreateInstance(

cookieType,

ReflectionService.BindingFlags,

null,

new[] { axIWebBrowser2, webBrowserEvent, typeof(DWebBrowserEvents2) },

CultureInfo.CurrentUICulture);

}

private void OnNewWindow(ref bool cancel)

{

var eventArgs = new CancelEventArgs(cancel);

if (NewWindow != null)

{

NewWindow(_webBrowser, eventArgs);

cancel = eventArgs.Cancel;

}

}

}

public static class ReflectionService

{

public readonly static BindingFlags BindingFlags =

BindingFlags.Instance |

BindingFlags.Public |

BindingFlags.NonPublic |

BindingFlags.FlattenHierarchy |

BindingFlags.CreateInstance;

public static object ReflectGetProperty(this object target, string propertyName)

{

if (target == null)

throw new ArgumentNullException("target");

if (string.IsNullOrWhiteSpace(propertyName))

throw new ArgumentException("propertyName can not be null or whitespace", "propertyName");

var propertyInfo = target.GetType().GetProperty(propertyName, BindingFlags);

if (propertyInfo == null)

throw new ArgumentException(string.Format("Can not find property '{0}' on '{1}'", propertyName, target.GetType()));

return propertyInfo.GetValue(target, null);

}

public static object ReflectInvokeMethod(this object target, string methodName, Type[] argTypes, object[] parameters)

{

if (target == null)

throw new ArgumentNullException("target");

if (string.IsNullOrWhiteSpace(methodName))

throw new ArgumentException("methodName can not be null or whitespace", "methodName");

var methodInfo = target.GetType().GetMethod(methodName, BindingFlags, null, argTypes, null);

if (methodInfo == null)

throw new ArgumentException(string.Format("Can not find method '{0}' on '{1}'", methodName, target.GetType()));

return methodInfo.Invoke(target, parameters);

}

}

[ComImport, TypeLibType(TypeLibTypeFlags.FHidden), InterfaceType(ComInterfaceType.InterfaceIsIDispatch), Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D")]

public interface DWebBrowserEvents2

{

[DispId(0x66)]

void StatusTextChange([In] string text);

[DispId(0x6c)]

void ProgressChange([In] int progress, [In] int progressMax);

[DispId(0x69)]

void CommandStateChange([In] long command, [In] bool enable);

[DispId(0x6a)]

void DownloadBegin();

[DispId(0x68)]

void DownloadComplete();

[DispId(0x71)]

void TitleChange([In] string text);

[DispId(0x70)]

void PropertyChange([In] string szProperty);

[DispId(250)]

void BeforeNavigate2([In, MarshalAs(UnmanagedType.IDispatch)] object pDisp, [In] ref object URL, [In] ref object flags, [In] ref object targetFrameName, [In] ref object postData, [In] ref object headers, [In, Out] ref bool cancel);

[DispId(0xfb)]

void NewWindow2([In, Out, MarshalAs(UnmanagedType.IDispatch)] ref object pDisp, [In, Out] ref bool cancel);

[DispId(0xfc)]

void NavigateComplete2([In, MarshalAs(UnmanagedType.IDispatch)] object pDisp, [In] ref object URL);

[DispId(0x103)]

void DocumentComplete([In, MarshalAs(UnmanagedType.IDispatch)] object pDisp, [In] ref object URL);

[DispId(0xfd)]

void OnQuit();

[DispId(0xfe)]

void OnVisible([In] bool visible);

[DispId(0xff)]

void OnToolBar([In] bool toolBar);

[DispId(0x100)]

void OnMenuBar([In] bool menuBar);

[DispId(0x101)]

void OnStatusBar([In] bool statusBar);

[DispId(0x102)]

void OnFullScreen([In] bool fullScreen);

[DispId(260)]

void OnTheaterMode([In] bool theaterMode);

[DispId(0x106)]

void WindowSetResizable([In] bool resizable);

[DispId(0x108)]

void WindowSetLeft([In] int left);

[DispId(0x109)]

void WindowSetTop([In] int top);

[DispId(0x10a)]

void WindowSetWidth([In] int width);

[DispId(0x10b)]

void WindowSetHeight([In] int height);

[DispId(0x107)]

void WindowClosing([In] bool isChildWindow, [In, Out] ref bool cancel);

[DispId(0x10c)]

void ClientToHostWindow([In, Out] ref long cx, [In, Out] ref long cy);

[DispId(0x10d)]

void SetSecureLockIcon([In] int secureLockIcon);

[DispId(270)]

void FileDownload([In, Out] ref bool cancel);

[DispId(0x10f)]

void NavigateError([In, MarshalAs(UnmanagedType.IDispatch)] object pDisp, [In] ref object URL, [In] ref object frame, [In] ref object statusCode, [In, Out] ref bool cancel);

[DispId(0xe1)]

void PrintTemplateInstantiation([In, MarshalAs(UnmanagedType.IDispatch)] object pDisp);

[DispId(0xe2)]

void PrintTemplateTeardown([In, MarshalAs(UnmanagedType.IDispatch)] object pDisp);

[DispId(0xe3)]

void UpdatePageStatus([In, MarshalAs(UnmanagedType.IDispatch)] object pDisp, [In] ref object nPage, [In] ref object fDone);

[DispId(0x110)]

void PrivacyImpactedStateChange([In] bool bImpacted);

}

public partial class WebBrowserHelper

{

private class WebBrowserEvent : StandardOleMarshalObject, DWebBrowserEvents2

{

private WebBrowserHelper _helperInstance = null;

public WebBrowserEvent(WebBrowserHelper helperInstance)

{

_helperInstance = helperInstance;

}

#region DWebBrowserEvents2 Members

public void StatusTextChange(string text)

{

}

public void ProgressChange(int progress, int progressMax)

{

}

public void CommandStateChange(long command, bool enable)

{

}

public void DownloadBegin()

{

}

public void DownloadComplete()

{

}

public void TitleChange(string text)

{

}

public void PropertyChange(string szProperty)

{

}

public void BeforeNavigate2(object pDisp, ref object URL, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel)

{

}

public void NewWindow2(ref object pDisp, ref bool cancel)

{

_helperInstance.OnNewWindow(ref cancel);

}

public void NavigateComplete2(object pDisp, ref object URL)

{

}

public void DocumentComplete(object pDisp, ref object URL)

{

}

public void OnQuit()

{

}

public void OnVisible(bool visible)

{

}

public void OnToolBar(bool toolBar)

{

}

public void OnMenuBar(bool menuBar)

{

}

public void OnStatusBar(bool statusBar)

{

}

public void OnFullScreen(bool fullScreen)

{

}

public void OnTheaterMode(bool theaterMode)

{

}

public void WindowSetResizable(bool resizable)

{

}

public void WindowSetLeft(int left)

{

}

public void WindowSetTop(int top)

{

}

public void WindowSetWidth(int width)

{

}

public void WindowSetHeight(int height)

{

}

public void WindowClosing(bool isChildWindow, ref bool cancel)

{

}

public void ClientToHostWindow(ref long cx, ref long cy)

{

}

public void SetSecureLockIcon(int secureLockIcon)

{

}

public void FileDownload(ref bool cancel)

{

}

public void NavigateError(object pDisp, ref object URL, ref object frame, ref object statusCode, ref bool cancel)

{

}

public void PrintTemplateInstantiation(object pDisp)

{

}

public void PrintTemplateTeardown(object pDisp)

{

}

public void UpdatePageStatus(object pDisp, ref object nPage, ref object fDone)

{

}

public void PrivacyImpactedStateChange(bool bImpacted)

{

}

#endregion

}

}

}

然后添加一个webbrowser控件

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:local="clr-namespace:Taobaoke"

mc:Ignorable="d"

Title="MainWindow" Height="350" Width="525">

da08b749f62c42d9b075e55ba48ba040.png

cs代码为下面using System;

using System.Windows;

using System.Windows.Navigation;

using System.ComponentModel;

namespace Taobaoke

{

/// 

/// MainWindow.xaml 的交互逻辑

/// 

public partial class MainWindow : Window

{

public MainWindow()

{

InitializeComponent();

var webBrowserHelper = new WebBrowserHelper(CamWeb);

webBrowserHelper.NewWindow += WebBrowserOnNewWindow;

string szTmp = "http://www.zhaokeli.com";

Uri uri = new Uri(szTmp);

CamWeb.Navigate(uri);

}

private void WebBrowserOnNavigated(object sender, NavigationEventArgs e)

{

dynamic browser = sender;

browser.Parent.Header = browser.Document.IHTMLDocument2_title;

}

private void WebBrowserOnNewWindow(object sender, CancelEventArgs e)

{

dynamic browser = sender;

dynamic activeElement = browser.Document.activeElement;

var link = activeElement.ToString();

// 这儿是在新窗口中打开,如果要在内部打开,改变当前browser的Source就行了

browser.Source = new Uri(link);

e.Cancel = true;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值