C#重寫WebBrowser元件,禁止跳轉到IE新視窗、指令碼錯誤
阿新 • • 發佈:2019-01-28
一、在VS2008裡用C#新建一個Windows窗體應用程式,起名為MyBrowser。
二、在裡面新增Label、TextBox、Button,然後整成如下圖的樣式,同時把各個控制元件的Anchor和Text歸整一下,位置和名稱放放好。
三、重寫WebBrowser元件,禁止跳轉到IE新視窗。選單“專案->新增類”,在模板中的“類”圖示上確認一下,然後名稱改為“ExtendedWebBrowser.cs”。
(一)在右邊解決方案管理器中右擊“檢視程式碼”,然後在ExtendedWebBrowser.cs程式碼視窗將程式碼修改成如下:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MyBrowser
{ 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 EventHandlerBeforeNavigate;
public event EventHandlerBeforeNewWindow; protected void OnBeforeNewWindow(string url, out bool cancel)
{
EventHandlerh = 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)
{
EventHandlerh = 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(250)]
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(273)]
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;
}
}
} (二)回到Form1.cs[設計]視窗,在選單“生成”中,點“生成解決方案”。一會之後在工具箱的最上方就會出現一個新的元件“ExtendedWebBrowser”,這正是我們需要的,hehe。
三、回到Form1.cs[設計]視窗,把ExtendedWebBrowser拖進來。
(一)在屬性窗口裡調整好Anchor,使之能最大化。
(二)雙擊“ScriptErrorSuppressed”,將之屬性改為“True”以禁用所有的對話方塊,比如提示Activex下載、執行以及安全登入等對話方塊。當然可以參考MSDN上的程式碼示例(http://apps.hi.baidu.com/share/detail/379735),有的放矢:private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
((WebBrowser)sender).Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);
}private void Window_Error(object sender, HtmlElementErrorEventArgs e)
{
// Ignore the error and suppress the error dialog box.
e.Handled = true;
} (三)在ExtendedWebBrowser的事件裡雙擊“BeforeNewWindow”並新增2行程式碼: private void extendedWebBrowser1_BeforeNewWindow(object sender, MyBrowser.WebBrowserExtendedNavigatingEventArgs e)
{
e.Cancel=true;
((ExtendedWebBrowser)sender).Navigate(e.Url);
} (四)最後一步,回到Form1.cs[設計]視窗,雙擊Button按鈕,新增程式碼如下: private void button1_Click(object sender, EventArgs e)
{
extendedWebBrowser1.Navigate(textBox1.Text);
}
F5執行,點幾個老跳的連結,oh~yeah,成功了! ??,還有些不爽,輸完網址後還要點登入,像IE一樣敲一下回車就好了。說幹就幹 (五)TextBox事件中,雙擊KeyPress(不要用Enter事件,試過不行),新增以下程式碼
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
button1_Click(null,null);
} }
二、在裡面新增Label、TextBox、Button,然後整成如下圖的樣式,同時把各個控制元件的Anchor和Text歸整一下,位置和名稱放放好。
三、重寫WebBrowser元件,禁止跳轉到IE新視窗。選單“專案->新增類”,在模板中的“類”圖示上確認一下,然後名稱改為“ExtendedWebBrowser.cs”。
(一)在右邊解決方案管理器中右擊“檢視程式碼”,然後在ExtendedWebBrowser.cs程式碼視窗將程式碼修改成如下:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
{ 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));
}
{
if (null != cookie)
{
cookie.Disconnect();
cookie = null;
}
base.DetachSink();
} //This new event will fire when the page is navigating
public event EventHandlerBeforeNavigate;
public event EventHandlerBeforeNewWindow;
{
EventHandlerh = 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)
{
EventHandlerh = 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(250)]
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(273)]
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;
}
}
} (二)回到Form1.cs[設計]視窗,在選單“生成”中,點“生成解決方案”。一會之後在工具箱的最上方就會出現一個新的元件“ExtendedWebBrowser”,這正是我們需要的,hehe。
三、回到Form1.cs[設計]視窗,把ExtendedWebBrowser拖進來。
(一)在屬性窗口裡調整好Anchor,使之能最大化。
(二)雙擊“ScriptErrorSuppressed”,將之屬性改為“True”以禁用所有的對話方塊,比如提示Activex下載、執行以及安全登入等對話方塊。當然可以參考MSDN上的程式碼示例(http://apps.hi.baidu.com/share/detail/379735),有的放矢:private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
((WebBrowser)sender).Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);
}private void Window_Error(object sender, HtmlElementErrorEventArgs e)
{
// Ignore the error and suppress the error dialog box.
e.Handled = true;
} (三)在ExtendedWebBrowser的事件裡雙擊“BeforeNewWindow”並新增2行程式碼: private void extendedWebBrowser1_BeforeNewWindow(object sender, MyBrowser.WebBrowserExtendedNavigatingEventArgs e)
{
e.Cancel=true;
((ExtendedWebBrowser)sender).Navigate(e.Url);
} (四)最後一步,回到Form1.cs[設計]視窗,雙擊Button按鈕,新增程式碼如下: private void button1_Click(object sender, EventArgs e)
{
extendedWebBrowser1.Navigate(textBox1.Text);
}
F5執行,點幾個老跳的連結,oh~yeah,成功了! ??,還有些不爽,輸完網址後還要點登入,像IE一樣敲一下回車就好了。說幹就幹 (五)TextBox事件中,雙擊KeyPress(不要用Enter事件,試過不行),新增以下程式碼
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
button1_Click(null,null);
} }