The webbrowser control captures DHTML events.

Source: Internet
Author: User
Like other controls, we can use the webbrowser control to construct our windows form application. Program . Select a Windows Forms control group from the toolbox, and click "Microsoft Web Browser". Visual Studio. NET uses the aximp.exe tool to create an ActiveX control. The control name is "axwebbrowser ". In VB. NET, COM components cannot be used directly. com is all unmanaged code. To use these components in VB. NET, the conversion from unmanaged code to managed code must be completed.
Generally, you can use the original webbrowser control, such as the call method, specifying attributes, and capturing events.
Some things are not that simple. We want to capture page events. For example, when a user clicks a page element (such as a background), The onclick event of the page element is triggered. If the event is not captured, we need to raise the DHTML level until the highest level of the Document Object. In this way, we can capture any event. In VB6, we can use the withevents keyword to specify webbrowser. Document to mshtml. htmldocument.
In VB. NET, this simple method is no longer effective. The ActiveX Control creates two interfaces and uses the same method name in the two interfaces, resulting in a runtime error. Therefore, you must specify the interface used by the Document Object and create an event processing handle.

The following is an exampleCode:

'Important: This Code assumes that you 've added a reference to
'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. html?entevents2_event = directcast (Doc ,_
Mshtml. html#entevents2_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

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.