如何用mfc实现一个activex控件的自定义接口

时间:2022-08-02 09:00:34

麻烦懂的帮我看一下网上实在找不到例子idl文件如下


#include <olectl.h>
#include <idispids.h>
#include  "ocidl.idl"
[object ,uuid(C15141B8-FCAB-4a97-958A-C4E9D3C6BCBE),
 helpstring("DVDataExchange接口"),pointer_default(unique)
]
interface IDVDataExchange: IUnknown
{
[helpstring("设置变量值")] HRESULT SetDataValue([in] BSTR bstrSymble, [in] VARIANT value);
[helpstring("获得变量列表")] HRESULT GetDatas([out,retval] VARIANT* value);
[helpstring("设置变量名称")] HRESULT SetDataName([in] BSTR bstrSymbl, [in] BSTR bstrName);
[helpstring("获得变量值")] HRESULT GetDataValue([in] BSTR bstrSymble,[out,retval] VARIANT* value);
};

[ uuid(EFEB2147-415E-4839-8502-74A7E5ECBC86), version(1.0),
  helpfile("MytestMeter2005.hlp"),
  helpstring("MytestMeter2005 ActiveX 控件模块"),
  control ]
library MytestMeter2005Lib
{
importlib(STDOLE_TLB);

//  CMytestMeter2005Ctrl 的主调度接口

[ uuid(BB06499B-216E-42EC-8C9E-85DAC4D423BC),
  helpstring("MytestMeter2005 Control 的调度接口")]
dispinterface _DMytestMeter2005
{
properties:
methods:

[id(DISPID_ABOUTBOX)] void AboutBox();
};

//  CMytestMeter2005Ctrl 的事件调度接口

[ uuid(6FDDB781-833A-419E-86C2-9FCAAD4FC6DA),
  helpstring("MytestMeter2005 Control 的事件接口") ]
dispinterface _DMytestMeter2005Events
{
properties:
//  事件接口没有任何属性

methods:
[id(1)] void OnUpdate(void);
[id(2)] void OnQueryHistData(DOUBLE tStartTime, DOUBLE tEndTime);
[id(3)] void QueryData(BSTR m_bstrName);
[id(4)] void AddData(BSTR bstrName);
[id(5)] void DeleteData(BSTR bstrName);
};

//  CMytestMeter2005Ctrl 的类信息

[ uuid(146D66F5-497D-490D-8151-5CC754C31B67),
  helpstring("MytestMeter2005 Control"), control ]
coclass MytestMeter2005
{
[default] interface  IDVDataExchange;
dispinterface _DMytestMeter2005;
[default, source] dispinterface _DMytestMeter2005Events;
};

};

IDVDataExchange 是我添加的自定义接口 
实现部分头文件


class CMytestMeter2005Ctrl : public COleControl
{
DECLARE_DYNCREATE(CMytestMeter2005Ctrl)

// 构造函数
public:
CMytestMeter2005Ctrl();

// 重写
public:
virtual void OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid);
virtual void DoPropExchange(CPropExchange* pPX);
virtual void OnResetState();
public:  
BEGIN_INTERFACE_PART(MytestMeter2005Ctrl, IDVDataExchange)
INIT_INTERFACE_PART( CMytestMeter2005Ctrl, MytestMeter2005Ctrl)
STDMETHOD(SetDataValue)(BSTR bstrSymble, VARIANT value);
STDMETHOD(GetDatas)(VARIANT* value);
STDMETHOD(SetDataName)(BSTR bstrSymble, BSTR bstrName);
STDMETHOD(GetDataValue)(BSTR bstrSymble, VARIANT* value);
END_INTERFACE_PART_STATIC(MytestMeter2005Ctrl) 

// 实现
protected:
~CMytestMeter2005Ctrl();

DECLARE_OLECREATE_EX(CMytestMeter2005Ctrl)    // 类工厂和 guid
DECLARE_OLETYPELIB(CMytestMeter2005Ctrl)      // GetTypeInfo
DECLARE_PROPPAGEIDS(CMytestMeter2005Ctrl)     // 属性页 ID
DECLARE_OLECTLTYPE(CMytestMeter2005Ctrl) // 类型名称和杂项状态

// 消息映射
DECLARE_MESSAGE_MAP()


    
// 调度映射
DECLARE_INTERFACE_MAP()
DECLARE_DISPATCH_MAP()

afx_msg void AboutBox();

// 事件映射
DECLARE_EVENT_MAP()

// 调度和事件 ID
public:
enum {
eventidDeleteData = 5L,
eventidAddData = 4L,
eventidQueryData = 3L,
eventidOnQueryHistData = 2L,
eventidOnUpdate = 1L
};
protected:

void OnUpdate(void)
{
FireEvent(eventidOnUpdate, EVENT_PARAM(VTS_NONE));
}

void OnQueryHistData(DOUBLE tStartTime, DOUBLE tEndTime)
{
FireEvent(eventidOnQueryHistData, EVENT_PARAM(VTS_R8 VTS_R8), tStartTime, tEndTime);
}

void QueryData(LPCTSTR m_bstrName)
{
FireEvent(eventidQueryData, EVENT_PARAM(VTS_BSTR), m_bstrName);
}

void AddData(LPCTSTR bstrName)
{
FireEvent(eventidAddData, EVENT_PARAM(VTS_BSTR), bstrName);
}

void DeleteData(LPCTSTR bstrName)
{
FireEvent(eventidDeleteData, EVENT_PARAM(VTS_BSTR), bstrName);
}
};

对应cpp部分

// MytestMeter2005Ctrl.cpp : CMytestMeter2005Ctrl ActiveX 控件类的实现。

#include "stdafx.h"
#include "MytestMeter2005.h"
#include "MytestMeter2005Ctrl.h"
#include "MytestMeter2005PropPage.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


IMPLEMENT_DYNCREATE(CMytestMeter2005Ctrl, COleControl)




// 消息映射

BEGIN_MESSAGE_MAP(CMytestMeter2005Ctrl, COleControl)
ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()

BEGIN_INTERFACE_MAP(CMytestMeter2005Ctrl, COleControl)
INTERFACE_PART(CMytestMeter2005Ctrl,  IID_IDVDataExchange, MytestMeter2005Ctrl)
END_INTERFACE_MAP()

// 调度映射

BEGIN_DISPATCH_MAP(CMytestMeter2005Ctrl, COleControl)
DISP_FUNCTION_ID(CMytestMeter2005Ctrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()



// 事件映射

BEGIN_EVENT_MAP(CMytestMeter2005Ctrl, COleControl)
EVENT_CUSTOM_ID("OnUpdate", eventidOnUpdate, OnUpdate, VTS_NONE)
EVENT_CUSTOM_ID("OnQueryHistData", eventidOnQueryHistData, OnQueryHistData, VTS_R8 VTS_R8)
EVENT_CUSTOM_ID("QueryData", eventidQueryData, QueryData, VTS_BSTR)
EVENT_CUSTOM_ID("AddData", eventidAddData, AddData, VTS_BSTR)
EVENT_CUSTOM_ID("DeleteData", eventidDeleteData, DeleteData, VTS_BSTR)
END_EVENT_MAP()



// 属性页

// TODO: 按需要添加更多属性页。请记住增加计数!
BEGIN_PROPPAGEIDS(CMytestMeter2005Ctrl, 1)
PROPPAGEID(CMytestMeter2005PropPage::guid)
END_PROPPAGEIDS(CMytestMeter2005Ctrl)



// 初始化类工厂和 guid

IMPLEMENT_OLECREATE_EX(CMytestMeter2005Ctrl, "MYTESTMETER2005.MytestMeter2005Ctrl.1",
0x146d66f5, 0x497d, 0x490d, 0x81, 0x51, 0x5c, 0xc7, 0x54, 0xc3, 0x1b, 0x67)



// 键入库 ID 和版本

IMPLEMENT_OLETYPELIB(CMytestMeter2005Ctrl, _tlid, _wVerMajor, _wVerMinor)



// 接口 ID

const IID BASED_CODE IID_DMytestMeter2005 =
{ 0xBB06499B, 0x216E, 0x42EC, { 0x8C, 0x9E, 0x85, 0xDA, 0xC4, 0xD4, 0x23, 0xBC } };
const IID BASED_CODE IID_DMytestMeter2005Events =
{ 0x6FDDB781, 0x833A, 0x419E, { 0x86, 0xC2, 0x9F, 0xCA, 0xAD, 0x4F, 0xC6, 0xDA } };
static const IID IID_IDVDataExchange= 
{ 0x8300c64, 0xc5b1, 0x4e97, { 0x88, 0x78, 0x8c, 0x38, 0x40, 0xe9, 0x67, 0x89 } };



// 控件类型信息

static const DWORD BASED_CODE _dwMytestMeter2005OleMisc =
OLEMISC_ACTIVATEWHENVISIBLE |
OLEMISC_SETCLIENTSITEFIRST |
OLEMISC_INSIDEOUT |
OLEMISC_CANTLINKINSIDE |
OLEMISC_RECOMPOSEONRESIZE;

IMPLEMENT_OLECTLTYPE(CMytestMeter2005Ctrl, IDS_MYTESTMETER2005, _dwMytestMeter2005OleMisc)



// CMytestMeter2005Ctrl::CMytestMeter2005CtrlFactory::UpdateRegistry -
// 添加或移除 CMytestMeter2005Ctrl 的系统注册表项

BOOL CMytestMeter2005Ctrl::CMytestMeter2005CtrlFactory::UpdateRegistry(BOOL bRegister)
{
// TODO: 验证您的控件是否符合单元模型线程处理规则。
// 有关更多信息,请参考 MFC 技术说明 64。
// 如果您的控件不符合单元模型规则,则
// 必须修改如下代码,将第六个参数从
// afxRegApartmentThreading 改为 0。

if (bRegister)
return AfxOleRegisterControlClass(
AfxGetInstanceHandle(),
m_clsid,
m_lpszProgID,
IDS_MYTESTMETER2005,
IDB_MYTESTMETER2005,
afxRegApartmentThreading,
_dwMytestMeter2005OleMisc,
_tlid,
_wVerMajor,
_wVerMinor);
else
return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}



// CMytestMeter2005Ctrl::CMytestMeter2005Ctrl - 构造函数

CMytestMeter2005Ctrl::CMytestMeter2005Ctrl()
{
InitializeIIDs(&IID_DMytestMeter2005, &IID_DMytestMeter2005Events);
// TODO: 在此初始化控件的实例数据。
}



// CMytestMeter2005Ctrl::~CMytestMeter2005Ctrl - 析构函数

CMytestMeter2005Ctrl::~CMytestMeter2005Ctrl()
{
// TODO: 在此清理控件的实例数据。
}



// CMytestMeter2005Ctrl::OnDraw - 绘图函数

void CMytestMeter2005Ctrl::OnDraw(
CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
if (!pdc)
return;

// TODO: 用您自己的绘图代码替换下面的代码。
pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
pdc->Ellipse(rcBounds);
}



// CMytestMeter2005Ctrl::DoPropExchange - 持久性支持

void CMytestMeter2005Ctrl::DoPropExchange(CPropExchange* pPX)
{
ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
COleControl::DoPropExchange(pPX);

// TODO: 为每个持久的自定义属性调用 PX_ 函数。
}



// CMytestMeter2005Ctrl::OnResetState - 将控件重置为默认状态

void CMytestMeter2005Ctrl::OnResetState()
{
COleControl::OnResetState();  // 重置 DoPropExchange 中找到的默认值

// TODO: 在此重置任意其他控件状态。
}



// CMytestMeter2005Ctrl::AboutBox - 向用户显示“关于”框

void CMytestMeter2005Ctrl::AboutBox()
{
CDialog dlgAbout(IDD_ABOUTBOX_MYTESTMETER2005);
dlgAbout.DoModal();
}



// CMytestMeter2005Ctrl 消息处理程序

HRESULT __stdcall CMytestMeter2005Ctrl::XMytestMeter2005Ctrl:: QueryInterface(const IID& iid, void** ppv)
{
METHOD_PROLOGUE_EX(CMytestMeter2005Ctrl, MytestMeter2005Ctrl);
    return S_OK ;

}

STDMETHODIMP_(ULONG) CMytestMeter2005Ctrl::XMytestMeter2005Ctrl::AddRef()
{  METHOD_PROLOGUE_EX(CMytestMeter2005Ctrl, MytestMeter2005Ctrl);
return S_OK ;
}

STDMETHODIMP_(ULONG) CMytestMeter2005Ctrl::XMytestMeter2005Ctrl::Release()
{
METHOD_PROLOGUE_EX(CMytestMeter2005Ctrl, MytestMeter2005Ctrl);
return S_OK ;
}

STDMETHODIMP CMytestMeter2005Ctrl::XMytestMeter2005Ctrl::GetDataValue(BSTR bstrSymble, VARIANT *value)
{

METHOD_PROLOGUE_EX(CMytestMeter2005Ctrl, MytestMeter2005Ctrl);
return S_OK;
}


STDMETHODIMP CMytestMeter2005Ctrl::XMytestMeter2005Ctrl::GetDatas(VARIANT* value)
{
METHOD_PROLOGUE_EX(CMytestMeter2005Ctrl, MytestMeter2005Ctrl);

// TODO: 在此添加实现代码

return S_OK;
}


STDMETHODIMP CMytestMeter2005Ctrl::XMytestMeter2005Ctrl::SetDataName(BSTR bstrSymble, BSTR bstrName)
{
METHOD_PROLOGUE_EX(CMytestMeter2005Ctrl, MytestMeter2005Ctrl);

// TODO: 在此添加实现代码

return S_OK;
}


STDMETHODIMP CMytestMeter2005Ctrl::XMytestMeter2005Ctrl:: GetDataValue(BSTR bstrSymble, VARIANT* value)
{
METHOD_PROLOGUE_EX(CMytestMeter2005Ctrl, MytestMeter2005Ctrl);

// TODO: 在此添加实现代码

return S_OK;
}

1 个解决方案

#1


来个好心人帮忙看下吧

#1


来个好心人帮忙看下吧