WebBrowser control captures DHTML events

Source: Internet
Author: User
Tags valid visual studio
Dhtml|web|webbrowser Control

Author: tuenhai.com msn:king#tuenhai.com

Copyright NOTICE: You can reprint, reprint, please be sure to hyperlink form to indicate the original source of the article and author information and this statement
http://www.Tuenhai.com/

Development tools: Microsoft Visual Studio. NET 2003
Operating system: Windows XP

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

As with other controls, we can use the WebBrowser control to build a Windows Form application. Select the Windows Forms control group from the Toolbox, click Microsoft Web Browser, and Visual Studio. NET uses the AxImp.exe tool in the background to create an ActiveX control with the name "Axwebbrowser". In vb.net, COM components cannot be used directly, COM is unmanaged code, and these components are used in vb.net, which must complete the conversion from unmanaged code to managed code.
Generally, you can use the same WebBrowser controls, such as the call method, specifying attributes, capturing events, and so on.
Some things are not so simple. We want to capture page events, such as the OnClick event that causes page elements when a user clicks on a page element (such as a background). When the fruit does not capture the event, it is necessary to elevate the level of DHTML until the highest level of the Document object. In this way, we can capture any event. In VB6, we can simply 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 name is used in two interfaces, resulting in a run-time error. Therefore, you must explicitly specify the interface used by the Document object and create an event handling handle (hehe, Tuenhai translation is not bad).

Here is the sample code:

' Important:this code assumes ' 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 ' The signature of this event are different from usual, as it
' is expected to return a boolean-if false ' default effect associated
' With the event (for example, jumping to another page if the ' click '
' 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 ' properties
' ...
End Function

Translator Note:
This is Tuenhai's first version of the translation.
Personal experience, in recent days in foreign-related programming websites around, benefited a lot. Also think of calligraphy learning "from the top." The way to share software is to move towards international. The study of software design is also not so? What is the international learning resources compared to the domestic learning resources?
Chinese is by no means an obstacle. Tuenhai do not believe in their Chinese will be better than you. The foundation of Junior High School, add powerword meaning namely translate, enough.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.