Last active
October 12, 2022 16:01
-
-
Save netcore-jroger/ce8fc3fbb64a8e1d2180 to your computer and use it in GitHub Desktop.
通过扩展 WebBrowser 使其有处理错误的能力
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
using System.Security.Permissions; | |
using System.Windows.Forms; | |
namespace WindowsFormsApplication1 | |
{ | |
public class MyWebBrowser : WebBrowser | |
{ | |
private AxHost.ConnectionPointCookie cookie; | |
private MyWebBrowserEventHelper helper; | |
public delegate void WebBrowserNavigateErrorEventHandler(object sender, WebBrowserNavigateErrorEventArgs e); | |
/// <summary> | |
/// 在 <see cref="MyWebBrowser"/> 导航发生错误时触发此事件。 | |
/// </summary> | |
public event WebBrowserNavigateErrorEventHandler NavigateError; | |
[PermissionSetAttribute( SecurityAction.LinkDemand, Name = "FullTrust" )] | |
protected override void CreateSink() | |
{ | |
base.CreateSink(); | |
// Create an instance of the client that will handle the event | |
// and associate it with the underlying ActiveX control. | |
helper = new MyWebBrowserEventHelper( this ); | |
cookie = new AxHost.ConnectionPointCookie( this.ActiveXInstance, helper, typeof( DWebBrowserEvents2 ) ); | |
} | |
[PermissionSetAttribute( SecurityAction.LinkDemand, Name = "FullTrust" )] | |
protected override void DetachSink() | |
{ | |
// Disconnect the client that handles the event | |
// from the underlying ActiveX control. | |
if ( cookie != null ) | |
{ | |
cookie.Disconnect(); | |
cookie = null; | |
} | |
base.DetachSink(); | |
} | |
// Raises the NavigateError event. | |
protected virtual void OnNavigateError(WebBrowserNavigateErrorEventArgs e) | |
{ | |
if ( this.NavigateError != null ) | |
{ | |
this.NavigateError( this, e ); | |
} | |
} | |
// Handles the NavigateError event from the underlying ActiveX | |
// control by raising the NavigateError event defined in this class. | |
private class MyWebBrowserEventHelper : StandardOleMarshalObject, DWebBrowserEvents2 | |
{ | |
private readonly MyWebBrowser parent; | |
public MyWebBrowserEventHelper(MyWebBrowser parent) | |
{ | |
this.parent = parent; | |
} | |
public void NavigateError(object pDisp, ref object url, ref object frame, ref object statusCode, ref bool cancel) | |
{ | |
// Raise the NavigateError event. | |
this.parent.OnNavigateError(new WebBrowserNavigateErrorEventArgs((String)url, (String)frame, (Int32)statusCode, cancel ) ); | |
} | |
} | |
} | |
public class WebBrowserNavigateErrorEventArgs : EventArgs | |
{ | |
public WebBrowserNavigateErrorEventArgs(String url, String frame, Int32 statusCode, Boolean cancel) | |
{ | |
Url = url; | |
Frame = frame; | |
StatusCode = statusCode; | |
Cancel = cancel; | |
} | |
/// <summary> | |
/// 获取或设置请求 URL 。 | |
/// </summary> | |
public string Url { get; set; } | |
public string Frame { get; set; } | |
/// <summary> | |
/// 获取或设置 HTTP 状态码。 | |
/// </summary> | |
public int StatusCode { get; set; } | |
/// <summary> | |
/// 获取或设置是否取消请求。 | |
/// </summary> | |
public bool Cancel { get; set; } | |
} | |
[ComImport, Guid( "34A715A0-6587-11D0-924A-0020AFC7AC4D" ), InterfaceType( ComInterfaceType.InterfaceIsIDispatch ), TypeLibType( TypeLibTypeFlags.FHidden )] | |
public interface DWebBrowserEvents2 | |
{ | |
[DispId( 271 )] | |
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 | |
); | |
} | |
} | |
// 调用示例 | |
using System.Windows.Forms; | |
namespace WindowsFormsApplication1 | |
{ | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
this.myWebBrowser1.NavigateError += browser_NavigateError; | |
} | |
void browser_NavigateError(object sender, WebBrowserNavigateErrorEventArgs e) | |
{ | |
e.Cancel = true; | |
MessageBox.Show(e.StatusCode+""); | |
} | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
this.myWebBrowser1.Navigate(this.textBox1.Text); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment