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