一般地,你可以像使用原来的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 library
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
AxWebBrowser1.Navigate("http://localhost/default.asp")
End Sub
Private 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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
2006-07-03 怎样将数据库日志文件缩短?
2006-07-03 Sql Server数据库的备份和恢复措施
2006-07-03 做技术,切不可沉湎于技术 !