void CSomeClass::ConnectEvents()
{
IConnectionPointContainer* pCPContainer;
// Step 1: Get a pointer to the connection point container
HRESULT hr = m_pIE->QueryInterface(IID_IConnectionPointContainer,
(void**)&pCPContainer);
if (SUCCEEDED(hr))
{
// m_pConnectionPoint is defined like this:
// IConnectionPoint* m_pConnectionPoint;
// Step 2: Find the connection point
hr = pCPContainer->FindConnectionPoint(
DIID_DWebBrowserEvents2, &m_pConnectionPoint);
if (SUCCEEDED(hr))
{
// Step 3: Advise
hr = m_pConnectionPoint->Advise(this, &m_dwCookie);
if (FAILED(hr))
{
::MessageBox(NULL, "Failed to Advise",
"C++ Event Sink", MB_OK);
}
}
pCPContainer->Release();
}
}
void CSomeClass::Exit()
{
// Step 5: Unadvise
if (m_pConnectionPoint)
{
HRESULT hr = m_pConnectionPoint->Unadvise(m_dwCookie);
if (FAILED(hr))
{
::MessageBox(NULL, "Failed to Unadvise",
"C++ Event Sink", MB_OK);
}
}
}
Figure 4 Internet Explorer 4.0 DISPIDs
DISPID_BEFORENAVIGATE2
DISPID_COMMANDSTATECHANGE
DISPID_DOCUMENTCOMPLETE
DISPID_DOWNLOADBEGIN
DISPID_DOWNLOADCOMPLETE
DISPID_NAVIGATECOMPLETE2
DISPID_NEWWINDOW2
DISPID_ONQUIT
DISPID_ONVISIBLE
DISPID_ONTOOLBAR
DISPID_ONMENUBAR
DISPID_ONSTATUSBAR
DISPID_ONFULLSCREEN
DISPID_ONTHEATERMODE
DISPID_PROGRESSCHANGE
DISPID_PROPERTYCHANGE
DISPID_STATUSTEXTCHANGE
DISPID_TITLECHANGE
Figure 5 Sample Implementation of Invoke
#include <strstrea.h>
STDMETHODIMP CSomeClass::Invoke(DISPID dispidMember,
REFIID riid,
LCID lcid, WORD wFlags,
DISPPARAMS* pDispParams,
VARIANT* pvarResult,
EXCEPINFO* pExcepInfo,
UINT* puArgErr)
{
USES_CONVERSION;
strstream strEventInfo;
if (!pDispParams)
return E_INVALIDARG;
switch (dispidMember)
{
// The parameters for this DISPID:
// [0]: URL navigated to - VT_BYREF|VT_VARIANT
// [1]: An object that evaluates to the top-level or frame
// WebBrowser object corresponding to the event.
//
case DISPID_NAVIGATECOMPLETE2:
// Check the argument's type
if (pDispParams->rgvarg[0].vt == (VT_BYREF|VT_VARIANT))
{
CComVariant varURL(*pDispParams->rgvarg[0].pvarVal);
varURL.ChangeType(VT_BSTR);
char str[100]; // Not the best way to do this.
strEventInfo << "NavigateComplete2: "
<< OLE2T(vtURL.bstrVal)
<< ends;
::MessageBox(NULL, strEventInfo.str(),
"Invoke", MB_OK);
}
break;
default:
break;
}
return S_OK;
}
Figure 7 AfxConnectionAdvise Parameters
pUnkSrc | A pointer to the COM object that fires the events. If you are automating a COM object, for example, this will be a pointer to the object that is created with a call to CoCreateInstance. |
pUnkSink | A pointer to the IUnknown of your event sink. |
iid | The IID of the connection point. For Internet Explorer 4.0, this will be DIID_DWebBrowserEvents2. |
bRefCount | TRUE indicates that creating the connection should cause the reference count of pUnkSink to be incremented. FALSE indicates that the reference count should not be incremented. |
pdwCookie | The identifier of the connection. This identifier is returned to you by AfxConnectionAdvise and should be passed as the dwCookie parameter to AfxConnectionUnadvise when disconnecting the connection. |
Figure 8 DISP_FUNCTION_ID Parameters
theClass | The name of your event sink class. |
szExternalName | The external name of the function. |
dispid | The DISPID of the event. |
pfnMember | Pointer to a member function that handles the event. |
vtRetval | The return value of the member function. This is one of the VARENUM enumerated types defined in wtypes.h. |
vtsParams | A space-separated list of one or more VTS_XXX constants specifying the function's parameter list. These VTS_ XXX constants are defined in afxdisp.h. |
Figure 9 ON_EVENT Parameters
theClass | The class to which this event sink map belongs. |
id | The resource identifier of the control. |
dispid | The dispatch ID of the event fired by the control. |
pfnHandler | Pointer to a member function that handles the event. This function should have a BOOL return type and parameter types that match the event's parameters (see vtsParams). The function should return TRUE to indicate the event was handled; otherwise FALSE. |
vtsParams | A sequence of VTS_XXX constants that specifies the types of the parameters for the event. These are the same constants that are used in dispatch map entries such as DISP_FUNCTION and DISP_FUNCTION_ID. |
Figure 11 Events in vtable Order
StatusTextChange
ProgressChange
CommandStateChange
DownloadBegin
DownloadComplete
TitleChange
PropertyChange
BeforeNavigate2
NewWindow2
NavigateComplete2
DocumentComplete
OnQuit
OnVisible
OnToolBar
OnMenuBar
OnStatusBar
OnFullScreen
OnTheaterMode
Figure 12 Helpful Knowledge Base Articles
Number | Title |
Q180366 | HOWTO: Determine When a Page Is Done Loading in WebBrowser Ctrl |
Q180365 | INFO: Advantages of Hosting WebBrowser Control Instead of MSHTML |
Q179421 | FIX: WebBrowser Control in CFormView Does Not Repaint Properly |
Q179230 | FILE: IEHelper-Attaching to Internet Explorer 4 using a Browser Helper Object |
Q177269 | PRB: Internet Explorer 4.0 Keeps Active Document Servers Running |
Q177241 | HOWTO: Adding to the Standard Context Menus of the WebBrowser |
Q176789 | PRB: Permission Denied Accessing Web Browser Control in HTML |
Q175910 | BUG: IDocHostShowUI::ShowHelp Not Called for Internet Explorer 4.0/MSHTML Host |
Q175513 | FILE: Invoke Find, View Source, Options dialogs for WebBrowser |
Q175502 | PRB: Tabbing Broken in WebBrowser Hosted in MFC Regular DLL |
Q165074 | PRB: Keystroke Problems in CView/CWnd WebBrowser Control |
Q163282 | HOWTO: Using Forward & Back Buttons for WebBrowser Control |
Q156693 | FILE: IEZOOM.EXE Changes the Font Size of WebBrowser Control |