王朝百科
分享
 
 
 

WebBrowser

王朝百科·作者佚名  2010-08-20  
宽屏版  字体: |||超大  

简介[1]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)

{

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)

{

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 窗体)

通过部分受信任的代码使用库

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
如何用java替换看不见的字符比如零宽空格&#8203;十六进制U+200B
 干货   2023-09-10
网页字号不能单数吗,网页字体大小为什么一般都是偶数
 干货   2023-09-06
java.lang.ArrayIndexOutOfBoundsException: 4096
 干货   2023-09-06
Noto Sans CJK SC字体下载地址
 干货   2023-08-30
window.navigator和navigator的区别是什么?
 干货   2023-08-23
js获取referer、useragent、浏览器语言
 干货   2023-08-23
oscache遇到404时会不会缓存?
 干货   2023-08-23
linux下用rm -rf *删除大量文件太慢怎么解决?
 干货   2023-08-08
刀郎新歌破世界纪录!
 娱乐   2023-08-01
js实现放大缩小页面
 干货   2023-07-31
生成式人工智能服务管理暂行办法
 百态   2023-07-31
英语学习:过去完成时The Past Perfect Tense举例说明
 干货   2023-07-31
Mysql常用sql命令语句整理
 干货   2023-07-30
科学家复活了46000年前的虫子
 探索   2023-07-29
英语学习:过去进行时The Past Continuous Tense举例说明
 干货   2023-07-28
meta name="applicable-device"告知页面适合哪种终端设备:PC端、移动端还是自适应
 干货   2023-07-28
只用css如何实现打字机特效?
 百态   2023-07-15
css怎么实现上下滚动
 干货   2023-06-28
canvas怎么画一个三角形?
 干货   2023-06-28
canvas怎么画一个椭圆形?
 干货   2023-06-28
canvas怎么画一个圆形?
 干货   2023-06-28
canvas怎么画一个正方形?
 干货   2023-06-28
中国河南省郑州市金水区蜘蛛爬虫ip大全
 干货   2023-06-22
javascript简易动态时间代码
 干货   2023-06-20
感谢员工的付出和激励的话怎么说?
 干货   2023-06-18
 
>>返回首页<<
 
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有