WebBrowser 类

来源:互联网 发布:埃及艳后哪个版本 知乎 编辑:程序博客网 时间:2024/06/07 20:21
 

HTML Tags and JavaScript tutorial



WebBrowser 类





WebBrowser 类 
注意:此类在 .NET Framework 2.0 版中是新增的。

使用户可以在窗体中导航网页。
命名空间:
System.Windows.Forms
程序集:
System.Windows.Forms(在 system.windows.forms.dll 中)
var ExpCollDivStr=ExpCollDivStr;ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl034eb4c1e,";var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl03img,";
语法
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
var ExpCollDivStr=ExpCollDivStr;ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl1026dc927,";var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl10img,";
备注
使用
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 控件

var ExpCollDivStr=ExpCollDivStr;ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl36904b17f,";var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl36img,";
示例
下面的代码示例演示如何使用
WebBrowser
控件实现地址栏。此示例要求窗体包含一个名为
webBrowser1

WebBrowser
控件、一个名为
TextBoxAddress

TextBox
控件和一个名为
ButtonGo

Button
控件。在文本框中键入 URL 并按 Enter 或单击“转到”按钮时,
WebBrowser
控件会定位至指定的 URL。通过单击超链接进行定位时,文本框会自动更新以显示当前 URL。
有关完整的代码示例,请参见
如何:将 Web 浏览器功能添加到 Windows 窗体应用程序

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();}
var ExpCollDivStr=ExpCollDivStr;ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl43df64ca6,";var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl43img,";
.NET Framework 安全性

SecurityPermission
  供直接调用方使用此控件。要求值:
LinkDemand

命名的权限集

FullTrust


SecurityPermission
  供派生类继承此控件。要求值:
InheritanceDemand

命名的权限集

FullTrust

var ExpCollDivStr=ExpCollDivStr;ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl4912005c7,";var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl49img,";
继承层次结构
System.Object

  
System.MarshalByRefObject

    
System.ComponentModel.Component

      
System.Windows.Forms.Control

        
System.Windows.Forms.WebBrowserBase

          
System.Windows.Forms.WebBrowser
var ExpCollDivStr=ExpCollDivStr;ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl559123a52,";var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl55img,";
线程安全
此类型的任何公共静态(Visual Basic 中的
Shared
)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
var ExpCollDivStr=ExpCollDivStr;ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl56b8accde,";var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl56img,";
平台
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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见
系统要求

var ExpCollDivStr=ExpCollDivStr;ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl580b1ac27,";var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl58img,";
版本信息
.NET Framework
受以下版本支持:2.0
.NET Compact Framework
受以下版本支持:2.0
var ExpCollDivStr=ExpCollDivStr;ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl592e9ce67,";var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl59img,";
请参见
参考
WebBrowser 成员
System.Windows.Forms 命名空间
其他资源
WebBrowser 控件(Windows 窗体)
通过部分受信任的代码使用库
 


原创粉丝点击