Article ID: 246247 - Last Review: May 11, 2006 - Revision: 4.0 How to sink HTML document events for WebBrowser host
This article was previously published under Q246247 On This PageSUMMARY
Developers who host the WebBrowser control in either Microsoft Visual C++ or Visual Basic frequently want to sink events for the currently loaded document. This is very handy for providing global behavior that browser authors want to implement across all pages within the user experience, such as grabbing external information on a clicked link or a piece of selected text. The general strategy for both Visual C++ and Visual Basic is:
MORE INFORMATIONVisual C++ (ATL)The first order of business in Visual C++ is to create your event sinks. Two event sinks must be created: One for DWebBrowserEvents2 for the WebBrowser control, and one for HtmlDocumentEvents (defined in MSHTML.H) for the actual Active Template Library (ATL) document.The DWebBrowserEvents2 sink can be implemented very quickly using ATL's IDispEventImpl, as described in the following Microsoft Knowledge Base article: 194179
(http://support.microsoft.com/kb/194179/
)
AtlEvnt.exe sample shows how to creates ATL sinks by using the ATL IDispEventImpl and IDispEventSimpleImpl classes
Because there will be a new document every time the user navigates to a new page, you must sink the document events whenever the WebBrowser control throws the DocumentComplete event. Within your DocumentComplete handler, use the following code to sink document events:
180366
(http://support.microsoft.com/kb/180366/
)
How to determine when a page is done loading in WebBrowser control
Your HTML document event sink must include the files <MSHTML.h> and <MSHTMDID.h> (which defines all of the DISPIDs for HTMLDocumentEvents).Event handlers for cancelable document events must return a Boolean value in the pvarResult parameter of the IDispatch::Invoke() method. A value of VARIANT_TRUE indicates that Internet Explorer should perform its own event processing; a value of VARIANT_FALSE cancels the event. For this reason, you should override Invoke() directly instead of using a shortcut implementation like ATL's IDispEventImpl, which will not allow you to alter the pvarResult. To override Invoke() successfully in an ATL event sink, see the following article in the Microsoft Knowledge Base: 181277
(http://support.microsoft.com/kb/181277/
)
The AtlSink.exe sample demonstrates how to implement a dispinterface sink by using the Active Template Library (ATL) in Visual C++
The following sink code shows how Invoke() can be overridden to handle the Click event (DISPID_CLICK):
Visual BasicThe process in Visual Basic is the same: Sink document events in the DocumentComplete event for the WebBrowser object. Fortunately, the process of event sinking is shortened by using Visual Basic's WithEvents keyword in cooperation with a variable declaration.Assuming your WebBrowser is named WebBrowser1 and you added Microsoft HTML Object Library to the project, the following code behaves exactly the same as the C++ code: REFERENCESFor more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites: http://msdn.microsoft.com/ie/
(http://msdn.microsoft.com/ie/)
http://support.microsoft.com/iep (http://support.microsoft.com/iep) APPLIES TO
| Other Resources Other Support Sites
CommunityGet Help NowArticle Translations
|






















Back to the top