组件对象模型与ATL实现
2010-09-04 20:48:02 来源:WEB开发网添加实现代码到Canvas.h中,你只需注意加粗的文字:
#include "Layers.h"
// CCanvas
class ATL_NO_VTABLE CCanvas :
public CComObjectRootEx<CComSingleThreadModel>,
...
{
public:
CComPtr<ILayers> m_spLayers;
CCanvas()
{
m_bWindowOnly = TRUE; // 必须:总是创建自己的窗口
}
...
HRESULT FinalConstruct()
{
return CLayers::CreateInstance(&m_spLayers);
// return S_OK;
}
void FinalRelease()
{
}
public:
STDMETHODIMP CCanvas::get_Layers(ILayers** ppRef)
{
return m_spLayers.CopyTo(ppRef);
}
// 此方法在Canvas.cpp中实现,以避免交叉引用头文件
// STDMETHOD(CreateObject)(BSTR bstrObjName, IDispatch** ppOutObj);
};
在Canvas.cpp中,CreateObject可以如下实现:
// Canvas.cpp : CCanvas 的实现
#include "stdafx.h"
#include "Canvas.h"
// CCanvas
#include "Layers.h"
#include "Layer.h"
#include "Shapes.h"
#include "Shape.h"
/*
STDMETHODIMP CCanvas::CreateObject(BSTR bstrObjName, IDispatch** ppOutObj)
{
if (!bstrObjName || !ppOutObj)
return E_POINTER;
if (wcsicmp(bstrObjName, "layers")==0)
{
CComPtr<ILayers> spOut;
CLayers::CreateInstance(&spOut);
if(spOut)
return spOut->QueryInterface(IID_IDispatch, (void**)ppOutObj);
}
else if (wcsicmp(bstrObjName, "layer")==0)
{
CComPtr<ILayer> spOut;
CLayer::CreateInstance(&spOut);
if(spOut)
return spOut->QueryInterface(IID_IDispatch, (void**)ppOutObj);
}
else if(wcsicmp(bstrObjName, "shapes")==0)
{
CComPtr<IShapes> spOut;
CShapes::CreateInstance(&spOut);
if(spOut)
return spOut->QueryInterface(IID_IDispatch, (void**)ppOutObj);
}
else if(wcsicmp(bstrObjName, "shape")==0)
{
CComPtr<IShape> spOut;
CShape::CreateInstance(&spOut);
if(spOut)
return spOut->QueryInterface(IID_IDispatch, (void**)ppOutObj);
}
return E_INVALIDARG;
}
*/
更多精彩
赞助商链接