複製鏈接
請複製以下鏈接發送給好友

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

WebBrowser.net控件

WebBrowser簡介

語法
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 的現有控件。

WebBrowser重要事項

使用下面的成員可以將控件導航到特定 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示例

下面的代碼示例演示如何使用 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 窗體
通過部分受信任的代碼使用庫

WebBrowserIE瀏覽器控件

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);