枚举器的定义,不使用ATL提供的枚举器军定义

枚举器的定义,不使用ATL提供的枚举器定义

1.在IDL文件,Ixxxxxxxxx接口中加入:

 [id(1), propget] HRESULT Count([out, retval] long* pCount);
 [id(2), propget] HRESULT Item([in] long Index, [out, retval] IUnknown** pUnknown);
 [id(3), propget] HRESULT _NewEnum([out, retval] IUnknown** ppUnk);

2.在Ixxxxxxxx类中加入:

 STDMETHOD(get_Count)(long *nCount);
 STDMETHOD(get_Item)(long nIndex, IUnknown **pItem);
 STDMETHOD(get__NewEnum)(IUnknown **punk);

 3.重载IEnumUnknown接口,建立枚举器对象.

 class CEnumUnknownShellList : public IEnumUnknown
        , public CComObjectRootEx<CComObjectThreadModel>
 {
  HRESULT Init(CYouClassName *pYouClasName)
  {
   m_pPopupMenuBar = pPopupMenuBar;
   return S_OK;
  }
  STDMETHOD(Next)(ULONG celt, IUnknown** rgelt, ULONG* pceltFetched);
  STDMETHOD(Skip)(ULONG celt);
  STDMETHOD(Reset)(void);
  STDMETHOD(Clone)(IEnumUnknown** ppEnum);

  BEGIN_COM_MAP(CEnumUnknownMenuItem)
   COM_INTERFACE_ENTRY_IID(IID_IEnumUnknown, IEnumUnknown)
  END_COM_MAP()
 protected:
 private:
  int m_nItem;
  CYouClassName *m_pYouClasName;
 };

你可能感兴趣的:(Class,interface)