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();
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();