Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Simple ActiveX control interface support via CAxWindow

0.00/5 (No votes)
3 Jan 2003 1  
HOWTO support AX control interface via CAxWindow

Introduction

Code below describes only idea using CComQIPtr template in pair with CAxWindow.
class CAxOwnControl : 
public CWindowImpl< CAxOwnControl , CAxWindow >,
public CComQIPtr< IControl, &IID_IControl >
{
public:
DECLARE_WND_SUPERCLASS("AxOwnControl", CAxWindow::GetWndClassName());
CAxOwnControl(){}
virtual ~CAxOwnControl(){}

HWND Create(HWND hWndParent, RECT& rcPos, 
        DWORD dwStyle = 0, DWORD dwExStyle = 0, 
        UINT nID = 0, LPVOID lpCreateParam = NULL)
{
	AtlAxWinInit(); // initialize AxHost stuff

	LPOLESTR pstrCLSID_CAxOwnControl = NULL;
	StringFromCLSID( CLSID_Control, &pstrCLSID_CAxOwnControl );
	ATLASSERT( pstrCLSID_CAxOwnControl != NULL );
	if( !pstrCLSID_CAxOwnControl )
		return NULL;

	USES_CONVERSION;
	HWND hWnd = CWindowImpl<CAxOwnControl , 
                     CAxWindow>::Create(hWndParent, rcPos, 
                     OLE2T(pstrCLSID_CAxOwnControl ), 
                     dwStyle, dwExStyle, nID, lpCreateParam);
	CoTaskMemFree(pstrCLSID_CAxOwnControl);

	if( hWnd ){
		IControlPtr pControl = NULL;
		QueryControl( IID_IControl, (void**)&pControl );
		*((CComQIPtr< IControl, &IID_IControl >*)this) = pControl;
	}

	return hWnd;
}

BEGIN_MSG_MAP_EX(CAxOwnControl)
END_MSG_MAP()
};

HOWTO use:
    CAxOwnControl m_ctrl;
    m_ctrl.Create(...);
    m_ctrl->SomeControlMethod();

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