-------------------------------------------------------------------
Control Class:
-------------------------------------------------------------------
class CDMyOcxCtrl1 : public CWnd
{
protected:
DECLARE_DYNCREATE(CDMyOcxCtrl1)
public:
CLSID const& GetClsid()
{
static CLSID const clsid
= { ..... };
return clsid;
}
virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle,
const RECT& rect, CWnd* pParentWnd, UINT nID,
CCreateContext* pContext = NULL)
{
return CreateControl(GetClsid(), lpszWindowName, dwStyle, rect, pParentWnd, nID);
}
BOOL Create(LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd,
UINT nID, CFile* pPersist = NULL, BOOL bStorage = FALSE,
BSTR bstrLicKey = NULL)
{
return CreateControl(GetClsid(), lpszWindowName, dwStyle, rect, pParentWnd, nID,
pPersist, bStorage, bstrLicKey);
}
// Attributes
public:
// Operations
public:
void AboutBox()
{
InvokeHelper(DISPID_ABOUTBOX, DISPATCH_METHOD, VT_EMPTY, NULL, NULL);
}
unsigned long MyIntMtd(double dbP1, unsigned long ulP2)
{
unsigned long result;
static BYTE parms[] = VTS_R8 VTS_UI4 ;
InvokeHelper(0x1, DISPATCH_METHOD, VT_UI4, (void*)&result, parms, dbP1, ulP2);
return result;
}
void MyVoidFunc()
{
InvokeHelper(0x2, DISPATCH_METHOD, VT_EMPTY, NULL, NULL);
}
void MyTestOcxFunc()
{
InvokeHelper(0x3, DISPATCH_METHOD, VT_EMPTY, NULL, NULL);
}
};
-------------------------------------------------------------------
Ocx Wrapper:
-------------------------------------------------------------------
// OcxWrapper.h
#pragma once
class COcxWrapper : public CWnd
{
DECLARE_DYNAMIC(COcxWrapper)
public:
COcxWrapper();
virtual ~COcxWrapper();
private:
CDMyOcxCtrl1* m_pMyOcx;
public:
CDMyOcxCtrl1* GetControl()
{
return m_pMyOcx;
}
}
// OcxWrapper.cpp
IMPLEMENT_DYNAMIC(COcxWrapper, CWnd)
COcxWrapper::COcxWrapper()
{
AfxEnableControlContainer();
RECT rect = { 0, 0, 0, 0 };
CString strClassName = AfxRegisterWndClass(NULL);
CreateEx(0, strClassName, NULL, 0, rect, NULL, 0);
SetParent(NULL);
m_pMyOcx = new CDMyOcxCtrl1();
m_pMyOcx->Create(NULL, 0, rect, this, 1);
}
COcxWrapper::~COcxWrapper()
{
if (m_pMyOcx)
{
m_pMyOcx->DestroyWindow();
delete m_pMyOcx;
m_pMyOcx = NULL;
}
}
-------------------------------------------------------------------
Usage:
-------------------------------------------------------------------
CoInitialize(NULL);
COcxWrapper *pOcx = new COcxWrapper();
ULONG ul = pOcx->GetControl()->MyIntMtd(1.7, 87);
printf("call ocx function, return = %ld/n", ul);
delete pOcx;