WebBrowser

WebBrowser

WebBrowser 是一個 .NET 控制項類,在 .NET Framework 2.0 版中新增。WebBrowser 類使用戶可以在窗體中導航網頁,WebBrowser 控制項具有多個與導航相關的屬性、方法和事件,WebBrowser 控制項不能由部分受信任的代碼使用。。

WebBrowser 的命名空間是System.Windows.Forms,程式集包括System.Windows.Forms(在 system.windows.forms.dll 中),WebBrowser 控制項會占用大量資源。使用完該控制項後一定要調用 Dispose 方法,以便確保及時釋放所有資源。必須在附加事件的同一執行緒上調用 Dispose 方法,該執行緒應始終是訊息或用戶界面 (UI) 執行緒。

基本介紹

  • 中文名:WebBrowser
  • 外文名:WebBrowser
  • 命名空間::System.Windows.Forms
  • 程式集::System.Windows
.net控制項,簡介,重要事項,示例,IE瀏覽器控制項,

.net控制項

簡介

語法
Visual Basic(聲明)
<ComVisibleAttribute(True)> _<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _Public Class WebBrowser Inherits WebBrowserBase
Visual Basic(用法)
Dim instance As WebBrowser
C#
[ComVisibleAttribute(true)] [ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] public class WebBrowser : WebBrowserBase
C++
[ComVisibleAttribute(true)] [ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] public ref class WebBrowser : public WebBrowserBase
J#
/** @attribute ComVisibleAttribute(true) */ /** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ public class WebBrowser extends WebBrowserBase
JavaScript
ComVisibleAttribute(true) ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) public class WebBrowser extends WebBrowserBase
備註
使用 WebBrowser 控制項可以在 Windows 窗體應用程式中承載網頁以及支持瀏覽器的其他文檔。例如,可以使用 WebBrowser 控制項在應用程式中提供基於 HTML 的集成用戶幫助或 Web 瀏覽功能。此外,還可以使用 WebBrowser 控制項向 Windows 窗體客戶端應用程式添加基於 Web 的現有控制項。

重要事項

使用下面的成員可以將控制項導航到特定 URL、在導航歷史記錄列表中向後和向前移動,還可以載入當前用戶的主頁和搜尋頁:
Url
Navigate
GoBack
GoForward
GoHome
GoSearch
如果導航不成功,則顯示一頁指示出現的問題。使用這些成員中的任何一個進行導航都會導致在導航的不同階段發生 Navigating、Navigated 和 DocumentCompleted 事件。
使用這些成員和其他成員(如 Stop 和 Refresh 方法)可以在應用程式中實現與 Internet Explorer 中的用戶界面控制項類似的用戶界面控制項。即使不希望在窗體上顯示 WebBrowser 控制項,某些成員也十分有用。例如,可以使用 Print 方法列印網頁的最新版本,而不向用戶顯示該頁。
使用 WebBrowser 控制項還可以顯示在應用程式中創建的內容或從資料庫或資源檔案檢索的內容。使用 DocumentText 或 DocumentStream 屬性,以字元串或數據流的形式獲取或設定當前文檔的內容。
還可以通過 Document 屬性操作網頁的內容,該屬性包含一個 HtmlDocument 對象,向當前頁提供對 HTML 文檔對象模型 (DOM) 的託管訪問。該屬性與 ObjectForScripting 屬性組合使用時,對在應用程式代碼與網頁中的動態 HTML (DHTML) 代碼之間實現雙向通信十分有用,使用它可以在單個用戶界面中組合基於 Web 的控制項和 Windows 窗體控制項。在應用程式中可以使用 Document 屬性調用腳本代碼方法。腳本代碼可以通過 window.external 對象訪問應用程式,該對象是用於主機訪問的內置 DOM 對象,它映射到為 ObjectForScripting 屬性指定的對象。
注意
該類要求類級別上的安全性。如果派生類或調用堆疊中的任何調用方不具有完全信任許可權,則會引發 SecurityException。有關安全要求的詳細信息,請參見 連結要求 和 繼承要求。
注意
WebBrowser 類僅能用於設定為單執行緒單元 (STA) 模式的執行緒。若要使用此類,請確保使用 STAThreadAttribute 屬性標記 Main 方法。
Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE 平台說明: 要實現 .NET Compact Framework 應用程式中的 WebBrowser 的完整功能,需要用於 Pocket PC 和 Smartphone 的 Windows Mobile 5.0 版軟體。有關更多信息,請參見 如何:在 .NET Compact Framework 中使用 WebBrowser 控制項。

示例

下面的代碼示例演示如何使用 WebBrowser 控制項實現地址欄。此示例要求窗體包含一個名為 webBrowser1 的 WebBrowser 控制項、一個名為 TextBoxAddress 的 TextBox 控制項和一個名為 ButtonGo 的 Button 控制項。在文本框中鍵入 URL 並按 Enter 或單擊“轉到”按鈕時,WebBrowser 控制項會定位至指定的 URL。通過單擊超連結進行定位時,文本框會自動更新以顯示當前 URL。
Visual Basic
' Navigates to the URL in the address box when
' the ENTER key is pressed while the ToolStripTextBox has focus.
Private Sub toolStripTextBox1_KeyDown( _
ByVal sender As Object, ByVal e As KeyEventArgs) _
Handles toolStripTextBox1.KeyDown
If (e.KeyCode = Keys.Enter) Then
Navigate(toolStripTextBox1.Text)
End If
End Sub
' Navigates to the URL in the address box when
' the Go button is clicked.
Private Sub goButton_Click( _
ByVal sender As Object, ByVal e As EventArgs) _
Handles goButton.Click
Navigate(toolStripTextBox1.Text)
End Sub
' Navigates to the given URL if it is valid.
Private Sub Navigate(ByVal address As String)
If String.IsNullOrEmpty(address) Then Return
If address.Equals("about:blank") Then Return
If Not address.StartsWith("http://") And _
Not address.StartsWith("https://") Then
address = "http://" & address
End If
Try
webBrowser1.Navigate(New Uri(address))
Catch ex As System.UriFormatException
Return
End Try
End Sub
' Updates the URL in TextBoxAddress upon navigation.
Private Sub webBrowser1_Navigated(ByVal sender As Object, _
ByVal e As WebBrowserNavigatedEventArgs) _
Handles webBrowser1.Navigated
toolStripTextBox1.Text = webBrowser1.Url.ToString()
End Sub
C#
// Navigates to the URL in the address box when// the ENTER key is pressed while the ToolStripTextBox has focus.private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e){    if (e.KeyCode == Keys.Enter){    webBrowser1.Navigate(toolStripTextBox1.Text);}}// Navigates to the URL in the address box when// the Go button is clicked.private void goButton_Click(object sender, EventArgs e){    webBrowser1.Navigate(toolStripTextBox1.Text);}// Navigates to the given URL if it is valid.private void Navigate(String address){    if (String.IsNullOrEmpty(address)) return;    if (address.Equals("about:blank")) return;    if (!address.StartsWith("http://") &&    !address.StartsWith("https://"))    {        address = "http://" + address;    }    try    {        webBrowser1.Navigate(new Uri(address));    }    catch (System.UriFormatException)    {        return;    }}// Updates the URL in TextBoxAddress upon navigation.private void webBrowser1_Navigated(object sender,WebBrowserNavigatedEventArgs e){    toolStripTextBox1.Text = webBrowser1.Url.ToString();}
C++
// Navigates to the URL in the address text box when// the ENTER key is pressed while the text box has focus.void TextBoxAddress_KeyDown( Object^ /*sender*/, System::Windows::Forms::KeyEventArgs^ e ){if ( e->KeyCode == System::Windows::Forms::Keys::Enter && !this->TextBoxAddress->Text->Equals( "" ) ){    this->WebBrowser1->Navigate( this->TextBoxAddress->Text );}}// Navigates to the URL in the address text box when// the Go button is clicked.void ButtonGo_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ ){    if ( !this->TextBoxAddress->Text->Equals( "" ) )    {        this->WebBrowser1->Navigate( this->TextBoxAddress->Text );    }}// Updates the URL in TextBoxAddress upon navigation.void WebBrowser1_Navigated( Object^ /*sender*/, System::Windows::Forms::WebBrowserNavigatedEventArgs^ /*e*/ ){    this->TextBoxAddress->Text = this->WebBrowser1->Url->ToString();}
繼承層次結構
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.WebBrowserBase
System.Windows.Forms.WebBrowser
此類型的任何公共靜態(Visual Basic 中的 Shared)成員都是執行緒安全的,但不保證所有實例成員都是執行緒安全的。
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 並不是對每個平台的所有版本都提供支持。有關受支持版本的列表,請參見系統要求。
版本信息
.NET Framework
受以下版本支持:2.0
.NET Compact Framework
受以下版本支持:2.0
請參見
參考
WebBrowser 成員
System.Windows.Forms 命名空間
其他資源
WebBrowser 控制項(Windows 窗體
通過部分受信任的代碼使用庫

IE瀏覽器控制項

WebBrowser是IE內置的瀏覽器控制項,無需用戶下載。本文檔所討論的是有關IE6.0版本的WebBrowser控制項技術內容。其他版本的IE應該也支持。與其相關的技術要求有:列印文檔的生成、頁面設定、列印操作的實現等幾個環節。
一、WebBrowser控制項
<object ID='WebBrowser' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>
二、WebBrowder控制項的方法
//列印
WebBrowser1.ExecWB(6,1);
//列印設定
WebBrowser1.ExecWB(8,1);
WebBrowser1.ExecWB(7,1);

相關詞條

熱門詞條

聯絡我們