Tutorials to .com

Tutorials to .com » Dotnet » Control-and-components » WebBrowser control capture DHTML events

WebBrowser control capture DHTML events

Print View , by: iSee ,Total views: 24 ,Word Count: 710 ,Date: Thu, 21 May 2009 Time: 12:04 AM

WebBrowser control capture dhtml events

Hai (SunHai) translation

development tools: Microsoft Visual Studio. NET 2003
Operating System: Windows XP

Original: http://www.devx.com/vb2themax/tip/18798

And other controls, we can use WebBrowser control to build our Windows form application. From the toolbox, select the Windows Forms control group, click "Microsoft Web Browser", Visual Studio. NET in the background AxImp.exe tools used to create ActiveX controls, controls for the name "AxWebBrowser". In vb.net can not directly use COM components, COM are Unmanaged Code, in VB.NET in the use of these components must be completed to Unmanaged Code from Managed Code conversion.
In general, you can use the WebBrowser control, like the original, such as the method call to specify attributes, such as capture the event.
Some things are not as simple as that. We have to capture the page, such as when a user clicks on the page elements (such as the background), the page elements lead to the onclick event. Fat fruit we do not capture the event, it is necessary to raise the level of DHTML, Document Object until the highest level. In this way, we will be able to capture any of the events. In VB6, we can simply specify WithEvents Keywords WebBrowser.Document to MSHTML.HTMLDocument.
In VB.NET, this simple method is no longer valid. Because the ActiveX control to create two interfaces, both interfaces use the same method name, resulting in run-time error. Therefore, you must explicitly specify the interface Document object to use, and create event handler.

The following is a sample code:

'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


Translator's Note:
This is my first translation.
Personal experience, the past few days in foreign countries around the procedure site design, benefited a lot from listening. Learning calligraphy think "should follow the best." Sharing of software lies in the international. Software design of the study is it not so? International Learning Resources Learning Resources, compared to domestic?
English is not the obstacles. I do not believe that my English than Hello. Middle school fundamentals, coupled with PowerWord means that translation, is sufficient.



.Net Control and components Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.