how to use ocx control in console app.


-------------------------------------------------------------------
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;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值