WebBrowser control captures DHTML events

zhaozj2021-02-16  58

WebBrowser control captures DHTML events

Sunhai

Development Tools: Microsoft Visual Studio .NET 2003 operating system: Windows XP

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

Like other controls, we can build our Windows Form app with WebBrowser controls. Select the Windows Form Control Group from the Toolbox, click Microsoft Web Browser, Visual Studio .NET creates an ACTIVEX control in the background using the Aximp.exe tool, the control name is "AxWebBrowser". In VB.NET, COM components cannot be used directly, COM is unmanaged code, using these components in VB.NET, must complete the conversion from UNMANAGED CODE to Managed Code. In general, you can use the original WebBrowser control, such as a Call method, specify attribute, capture event, etc. Some things are not that simple. We want to capture page events, such as when users click on page elements (such as background), trigger the onClick event of page elements. As a result, we didn't capture the event, just enhance the level of DHTML until the highest level of the Document object. This way, we can capture any events. In VB6, we can specify webbrowser.document to mshtml.htmlDocument with WitHevents keywords. In VB.NET, this simple method is no longer valid. Because the ActiveX control creates two interfaces, the same method names are used in both interfaces, resulting in runtime errors. So, you must specify the interface used by the Document object and create an event handle.

The following is a sample code:

'Important: this code assumes, you've added a reason to the' Microsoft HTML Object Library Type Library

Private Sub Form1_Load (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 onclickprocEnd 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 Note: This is my first translation. Personal experience, in recent days, foreign countries related to the design website is turning, and there is more beneficial. Also think of "taking the way" in calligraphy learning. The way to share software is to go to the international. What is the study of software design? What is international learning resources compared to domestic learning resources? ENGLISH is never obstacles. I don't believe that my English will be better than you. Junior high school basis, plus Jinshan phrase, refers to translation, enough.

May 16, 2004

My QQ: 26624998 My website: http://blog.9cbs.net/2066/

转载请注明原文地址:https://www.9cbs.com/read-22803.html

New Post(0)