请输入您要查询的百科知识:

 

词条 WebBrowser
释义

.net控件

简介

WebBrowser 是一个 .NET 控件类,在 .NET Framework 2.0 版中新增。WebBrowser 类使用户可以在窗体中导航网页。

命名空间:System.Windows.Forms

程序集:System.Windows.Forms(在 system.windows.forms.dll 中)

语法

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

JScript

ComVisibleAttribute(true) ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) public class WebBrowser extends WebBrowserBase

备注

使用 WebBrowser 控件可以在 Windows 窗体应用程序中承载网页以及支持浏览器的其他文档。例如,可以使用 WebBrowser 控件在应用程序中提供基于 HTML 的集成用户帮助或 Web 浏览功能。此外,还可以使用 WebBrowser 控件向 Windows 窗体客户端应用程序添加基于 Web 的现有控件。

重要事项

WebBrowser 控件会占用大量资源。使用完该控件后一定要调用 Dispose 方法,以便确保及时释放所有资源。必须在附加事件的同一线程上调用 Dispose 方法,该线程应始终是消息或用户界面 (UI) 线程。

WebBrowser 控件不能由部分受信任的代码使用。

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 控件实现地址栏。此示例要求窗体包含一个名为 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);

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2025/2/4 3:44:44