<!-- Download Links -->
<!-- Article image -->
<!-- Add the rest of your HTML here -->
The conventional approach to sink events when hosting a
WebBrowser control involves using the MSHTML Element
It requires retrieving a pointer to the element of interest of the page,
retrieving a pointer to the connection point container, finding the required
connection point, advising the connection point to start receiving events,
to handle any fired event and closing the connections when no longer needed.
This article illustrates a simple alternative technique to intercept and handle, in a
MFC application, HTML element events generated in a hosted WebBrowser Control.
It also shows how to use xml objects to pack event information to the hosting
application, and how to extract this information to implement an event handler.
Before navigation occurs in a WebBrowser control, a BeforeNavigate2
message is fired. VC++ 6.0 MFC Class Wizard, automatically adds the appropriate
OnBeforeNavigate2 member function to handle this event.
Two of the six
parameters taken by
OnBeforeNavigate2, are useful to intercept DHTML events:
that contains the url address to be navigated to, and
BOOL FAR* Cancel
cancels navigation before it occurs.
In a WebBrowser's HTML page, the
location.href of the browser
sets the entire URL as a string.
When a given URL is set in the HTML script event handler, the corresponding string
is passed as the second parameter of the member function
OnBeforeNavigate2 of the
In order to intercept and handle HTML element events, the
should contain, instead of a regular URL address, a string with the necessary
information of the DHTML event fired in the Browser control.
most cases, the information required by the hosting application consists of the html element's id , and an associated
value. This information is packed in a xml string having the form
value is passed to
OnBeforeNavigate2, the parameter
Cancel is set to TRUE to
avoid navigation. The following two code snippets illustrate these ideas.
<SCRIPT ID = "clientEventHandlersVBS" LANGUAGE="VBScript">
strIndex = "007"
strXML = "<element idevent='" & strIndex & "'>" &_<BR> myelement.innerText & "</element>"
location.href = "Event:" & strXML
// OnBeforeNavigate2 Event
handler void CDlgDHTMLEventsDlg::OnBeforeNavigate2(LPDISPATCH pDisp,
VARIANT FAR* URL,
VARIANT FAR* Flags,
VARIANT FAR* TargetFrameName,
VARIANT FAR* PostData,
VARIANT FAR* Headers,
BOOL FAR* Cancel)
strXML.Replace("%20", " ");
if(strURL == _T("about:blank"))
*Cancel = FALSE;
*Cancel = TRUE;
The sample application
dlgDHTMLEvents is a dialog based MFC application. The dialog hosts a WebBrowser control that shows a HTML page
having these DHTML objects: two INPUT elements, two BUTTONS,
one <h1> text element and one IMAGE object. When some predetermined events are fired by
each one of these elements, its id and associated value are
passed to the MFC dialog app.
Since xml is used to convey DHTML event
information, the Microsoft XML parser should be imported into the MFC project.
To do it, add this line to StdAfx.h (If not installed,
it can be downloaded from
#import "Msxml3.dll" named_guids raw_interfaces_only
To add ATL support
CComBSTR types are used), include this header file in StdAfx.h:
The HTML script is stored as a resource of the project (ID: IDR_HTML).
The script's event handlers are included in the section <SCRIPT ID =
"clientEventHandlersVBS" LANGUAGE="VBScript">of this resource.
The page is loaded into the Browser using the
interface and associated methods, of the
CWebBrowser2 class. The member function
LoadHTMLPage(CString strBaseAddress) implements this operation. The strBaseAddress parameter sets the baseline URL
for the graphical resources used in the HTML page.
The member variable
m_strBaseAddress contains this parameter. In
the sample application, this value is set, in the dialog constructor, to _T("c:\\dlgDHTMLEvents\\res").
Please modify according to your path.
Before a pointer to
IPersistStreamInit can be obtained, Mshtml.dll has to be
loaded (MSHTML is the IE rendering engine and parser for HTML). For this reason, the initial navigation of the browser is
set to the valid
LoadHTMLPage() is called from the event handler
m_bolLoaded is false.
The member function
ExtractXMLInfo(CString strXML) converts the string parameter strXML, received
from the Web browser control, to a valid xml
object. To obtain a valid
pointer to this interface,
CoCreateInstance() is called as follows:
hr = CoCreateInstance( MSXML2::CLSID_DOMDocument,
After obtaining this pointer, the parameter strXML, which has the form
is loaded and navigated to obtain the attribute node
, and the value node
event id and its associated value, are passed as parameters to the event handler
EventHandler(INT nEvent, CString strValue).
controls m_stcEvent and m_stcValue display in the dialog, the DHTML event
description and the
In summary, the steps involved for intercepting and handling HTML events are:
- Declare any desired event handlers in the HTML
- In each event handler, set the
location.href property to the xml string that
contains the values to be passed to the MFC host
- Call the function that loads the HTML document stream from
the event handler
- Extract HTML element event information from the
received xml string.
- Use this information in the MFC event handler function.
With minor modifications, these steps can be used for applications that