WEB开发网
开发学院软件开发VC 组件对象模型与ATL实现 阅读

组件对象模型与ATL实现

 2010-09-04 20:48:02 来源:WEB开发网   
核心提示:添加实现代码到Canvas.h中,你只需注意加粗的文字:#include "Layers.h"// CCanvasclass ATL_NO_VTABLE CCanvas : public CComObjectRootEx<CComSingleThreadModel>, ...{publi

添加实现代码到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;
}
*/

上一页  4 5 6 7 8 9 10 11 12 13  下一页

Tags:组件 对象 模型

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接