和其他控件一样,我们可以用WebBrowser控件来构筑我们的Windows form应用程序。从工具箱中选择Windows 窗体控件组,单击“Microsoft Web 浏览器”,Visual Studio .NET 在后台使用AxImp.exe工具创建ActiveX 控件,控件名字为“AxWebBrowser”。在VB.NET中,不能直接使用COM组件,COM都是Unmanaged Code,在VB.NET中使用这些组件,必须完成从Unmanaged Code到Managed Code的转换。
一般地,你可以像使用原来的WebBrowser控件一样,如call 方法,指定属性,捕捉事件等。
有些事情并不是那么简单的。我们要捕捉页面事件,如当用户点击页面元素(如背景)时,引发页面元素的onclick事件。发果我们没有捕捉到事件,就要提升DHTML的等级,直到Document对象的最高层次。这样,我们就能捕捉到任何事件了。在VB6中,我们可以简单地用WithEvents关键词指定WebBrowser.Document到MSHTML.HTMLDocument。
在VB.NET中,这个简单方法不再有效。因为ActiveX控件创建了两个接口,两个接口中使用了同样的方法名,导致出现运行时错误。所以,你必须明确指定Document对象使用的接口,并创建事件处理句柄。
以下是示例代码:' IMPORTANT: this code assumes that you've added a reference to the
' Microsoft HTML Object Library type libraryPrivate Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
AxWebBrowser1.Navigate("http://localhost/default.asp")
End SubPrivate Sub AxWebBrowser1_NavigateComplete2(ByVal sender As Object, _
ByVal e As AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event) Handles _
AxWebBrowser1.NavigateComplete2
' must wait for this event to grab a valid refernece to the Document
' property
Dim doc As mshtml.HTMLDocument = DirectCast(AxWebBrowser1.Document, _
mshtml.HTMLDocument) ' Cast to the interface that defines the event you're interested in
Dim docevents As mshtml.HTMLDocumentEvents2_Event = DirectCast(doc, _
mshtml.HTMLDocumentEvents2_Event)
' Define a handler to the onclick event
AddHandler docevents.onclick, AddressOf onclickproc
End Sub' Notice that the signature of this event is different from usual, as it
' is expected to return a Boolean - if false the default effect associated
' with the event (for example, jumping to another page if the click is on
' an hyperlink) is canceled.Private Function onclickproc(ByVal obj As mshtml.IHTMLEventObj) As Boolean
' an object on the page has been clicked - you can learn more about
' type and position of this object by querying the obj's properties
' ...
End Function
一般地,你可以像使用原来的WebBrowser控件一样,如call 方法,指定属性,捕捉事件等。
有些事情并不是那么简单的。我们要捕捉页面事件,如当用户点击页面元素(如背景)时,引发页面元素的onclick事件。发果我们没有捕捉到事件,就要提升DHTML的等级,直到Document对象的最高层次。这样,我们就能捕捉到任何事件了。在VB6中,我们可以简单地用WithEvents关键词指定WebBrowser.Document到MSHTML.HTMLDocument。
在VB.NET中,这个简单方法不再有效。因为ActiveX控件创建了两个接口,两个接口中使用了同样的方法名,导致出现运行时错误。所以,你必须明确指定Document对象使用的接口,并创建事件处理句柄。
以下是示例代码:' IMPORTANT: this code assumes that you've added a reference to the
' Microsoft HTML Object Library type libraryPrivate Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
AxWebBrowser1.Navigate("http://localhost/default.asp")
End SubPrivate Sub AxWebBrowser1_NavigateComplete2(ByVal sender As Object, _
ByVal e As AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event) Handles _
AxWebBrowser1.NavigateComplete2
' must wait for this event to grab a valid refernece to the Document
' property
Dim doc As mshtml.HTMLDocument = DirectCast(AxWebBrowser1.Document, _
mshtml.HTMLDocument) ' Cast to the interface that defines the event you're interested in
Dim docevents As mshtml.HTMLDocumentEvents2_Event = DirectCast(doc, _
mshtml.HTMLDocumentEvents2_Event)
' Define a handler to the onclick event
AddHandler docevents.onclick, AddressOf onclickproc
End Sub' Notice that the signature of this event is different from usual, as it
' is expected to return a Boolean - if false the default effect associated
' with the event (for example, jumping to another page if the click is on
' an hyperlink) is canceled.Private Function onclickproc(ByVal obj As mshtml.IHTMLEventObj) As Boolean
' an object on the page has been clicked - you can learn more about
' type and position of this object by querying the obj's properties
' ...
End Function
解决方案 »
- windows窗体显示问题
- if语句里面有多个并列条件时,这两种写法是不是第二种效率更高,哪种风格好?
- tamir.sharpssh 的问题。
- 这个进度条怎么弄啊?
- DataGridView问题
- 请大家帮看看C#中的小问题,代码和错误已贴上!
- 在DataGrid中如何设置为不能追加记录呢?
- 高手,datagridview排序问题
- dev gridcontrol的一些问题。
- (高分求解:在线等待)DataGrid的SelectedIndexChanged无法执行
- 紧急求救,Flash
- DataSet.AcceptChanges()后 SqlDataAdapter.Update(DataSet)时数据无法更新数据库 希望路过高人指点 谢谢了
自动完成就是IE的一个功能,添过的表单信息再添的时候会自动出个列表
我的是想用个按钮控制唉~我是初学者
for (int i = 0; i < webBrowser1.Document.Window.Frames.Count; i++)
{
MessageBox.Show(webBrowser1.Document.Window.Frames[i].Name);
}
//刷新
webBrowser1.Document.Window.Frames[0].Navigate(webBrowser1.Document.Window.Frames[0].Url);
--------
在按钮click事件里,_top.xxx.xxx,这么一层层下去,直到你想刷新的那个Frame,指定其Localtion,意思就这么个意思,具体代码我也记不太清了