This solution worked for me:
1.I added Interface IActiveViewEvents to my class so it seems like this
[
coclass,
default(IMyCOMClass),
threading(apartment),
vi_progid("myprg.MyCOMClass"),
progid("myprg.MyCOMClass.1"),
version(1.0),
uuid("38A4302C-3513-4B20-9A38-318FE738FDCD"),
helpstring("MyCOMClass Class"),
implements_category(__uuidof(CATID_MxExtension))
]
class ATL_NO_VTABLE CMyCOMClass :
public IMyCOMClass,
public IExtension,
public IActiveViewEvents
In Startup method of IExtension,this class is added to Map event
listeners for IActiveViewEvents connection point by this code
STDMETHOD(Startup)(VARIANT * initializationData)
{
if(initializationData==NULL)
return E_POINTER;
HRESULT hr;
IDispatch* ipDsp;
ipDsp=initializationData->pdispVal;
if(FAILED(hr=ipDsp->QueryInterface(IID_IApplication,(void**)&this->m_ipApp)))
return E_UNEXPECTED;
IDocumentPtr ipDoc;
//m_ipApp is member variable,keeps a pointer to ArcMap application,its type is IApplicationPtr
if(FAILED(hr=this->m_ipApp->get_Document(&ipDoc)))
return E_UNEXPECTED;
//m_ipMxDoc is member variable of type IMxDocumentPtr
this->m_ipMxDoc=(IMxDocumentPtr)ipDoc;
IMapPtr ipMap;
if(FAILED(hr=this->m_ipMxDoc->get_FocusMap(&ipMap)))
return E_UNEXPECTED;
//Hook to IActiveViewEvents connection point in Map COM Object
//m_pComSource is member variable of type LPUNKNOWN,keeps pointer to event source
LPUNKNOWN pComSink;
if(FAILED(hr=ipMap->QueryInterface(IID_IUnknown,(LPVOID*)&this->m_pComSource)))
return hr;
if(FAILED(hr=this->QueryInterface(IID_IUnknown,(LPVOID*)&pComSink)))
return hr;
//m_Cookie is member variable of type DWORD,will be used when we want to disconnect this class from event source
if(FAILED(hr=AtlAdvise(this->m_pComSource,pComSink,IID_IActiveViewEvents,&this->m_Cookie)))
{
this->m_Cookie=0;
return hr;
}
return S_OK;
}
3.Finally,the tie beetwen this class and Map object(as event source) will be removed when this class shutdowns
STDMETHOD(Shutdown)()
{
if(this->m_Cookie!=0)
{
AtlUnadvise(this->m_pComSource,IID_IActiveViewEvents,this->m_Cookie);
}
return S_OK;
}
now,my class listens to Map object to find when Map fires event methods like ItemAdded,ItemDeleted or ItemReordered.
Regards