Click here to Skip to main content
15,891,828 members
Articles / Desktop Programming / WTL

WTL Class for ActiveX Hosting

Rate me:
Please Sign up or sign in to vote.
3.51/5 (16 votes)
6 Nov 20062 min read 179.1K   1.7K   46  
WTL Helper classes for Event Sink and ActiveX hosting
// WtlExt.h : WTL Extensions/Helpers
// Copyright: Jes�s Salas 2002 ( jesuspsalas@hotmail.com )
// License  : none, feel free to use it.
/////////////////////////////////////////////////////////////////////////////

#pragma once

template <class T,class Interface>
class CWTLDispEventHelper : public IDispEventImpl<0,T>
{
	public:
		CComPtr<IUnknown> m_pUnk;
		HRESULT EasyAdvise(IUnknown* pUnk) 
		{  
			m_pUnk = pUnk;
			AtlGetObjectSourceInterface(pUnk,&m_libid, &m_iid, &m_wMajorVerNum, &m_wMinorVerNum);
			return DispEventAdvise(pUnk, &m_iid);
		}
		HRESULT EasyUnadvise() 
		{ 
			AtlGetObjectSourceInterface(m_pUnk,&m_libid, &m_iid, &m_wMajorVerNum, &m_wMinorVerNum);
			return DispEventUnadvise(pUnk, &m_iid);
	  }

};


template <class T, class Interface>
class CWTLAxControl :	public CComPtr<Interface>,
						public CWindowImpl<CWTLAxControl,CAxWindow>, 
						public CWTLDispEventHelper<T,Interface>
{
	public:

	BEGIN_MSG_MAP(CWTLAxControl)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
	END_MSG_MAP()

	LRESULT OnCreate( UINT uMsg, WPARAM wParam , LPARAM lParam, BOOL & bHandled )
	{
		LRESULT lRet;
		// We must call DefWindowProc before we can attach to the control.
		lRet = DefWindowProc( uMsg, wParam,lParam );
		// Get the Pointer to the control with Events (true)
		AttachControl(true);
		return lRet;
	}


	HRESULT AttachControl( BOOL bWithEvents = false ) 
	{
		HRESULT hr = S_OK;
		CComPtr<IUnknown> spUnk;
		// Get the IUnknown interface of control
		hr |= AtlAxGetControl( m_hWnd, &spUnk);

		if (SUCCEEDED(hr))
			// Query our interface
			hr |= spUnk->QueryInterface( __uuidof(Interface), (void**) (CComPtr<Interface>*)this);

		if ( bWithEvents && ! FAILED(hr) )
			 // Start events
			 hr|= EasyAdvise( spUnk );

		return hr;
	};
};


#include <ExDispid.h>

#define EVENTFN void __stdcall
class CWTLIExplorer : public CWTLAxControl<CWTLIExplorer,IWebBrowser2>
{
	public:
		// BEGIN_MSG_MAP is optional, you don't need to define or use it if you don't want
		BEGIN_MSG_MAP( CWTLIExplorer )
			MESSAGE_HANDLER(WM_CREATE, OnCreate)			
		END_MSG_MAP()


		LRESULT OnCreate(UINT uMsg, WPARAM wParam , LPARAM lParam, BOOL& bHandled)
		{
			// First you must CWTLAxControl<...,...>::OnCreate ( it set this message Handled )
			return CWTLAxControl<CWTLIExplorer,IWebBrowser2>::OnCreate( uMsg, wParam, lParam, bHandled );
		}

		// BEGIN_SINK_MAP is optional, you don't need to define or use it if you don't want
		BEGIN_SINK_MAP( CWTLIExplorer )
			SINK_ENTRY(0, DISPID_NAVIGATECOMPLETE2, OnNavigateComplete2 )
		END_SINK_MAP()
		EVENTFN OnNavigateComplete2( IDispatch* pDisp,  VARIANT* URL )
		{
			MessageBox( "OnNavigateComplete2" );
		}
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
CEO wave-vs.net
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions