1

How I can wire this event to my method?

I have an ATL class and I need to know when a layer added to map object so that I can update my list of layers in this class.My class implements IExtension and I've put this code in Startup method of this interface:

//Some code to obtain a reference to Map object __hook(&IActiveViewEvents::ItemReordered,pMap/pointer to map/,&MyClass::MyEventHandler);

and in Shutdown method of IExtension I unhook it,but I receive compile error says the source and handler method not compatible while both signatures,arguments and names are exactly same.

Is it true a all this method?any simpler method for this wiring?or any reference on net?

0

1 Answer 1

0

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

  1. 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

1
  • What happens if you delete the dataframe (map) and add a new one, do you re-wire events to the newly added map? Commented Jan 17, 2011 at 1:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.