Welcome to MapForums!

Register, sign in, or use Facebook Connect above to join in and participate in the forum.

When you are signed in, this message, the ads in this row, and the red-underlined link ads all go away.

Subscribe to receive our newsletter.
Subscribe Unsubscribe
Results 1 to 9 of 9

Catch mappoint events using C++ (without MFC...)

This is a discussion on Catch mappoint events using C++ (without MFC...) within the MapPoint Desktop Discussion forums, part of the Map Forums category; Hi all, i'm trying to catch events such as SelectionChange from MapPoint 2004. I've worked myself trough loads of examples ...

  1. #1
    Jeurink is offline Junior Member Yellow Belt
    Join Date
    Feb 2005
    Posts
    19

    Catch mappoint events using C++ (without MFC...)

    Hi all,

    i'm trying to catch events such as SelectionChange from MapPoint 2004.
    I've worked myself trough loads of examples (from wich about 50% actually works) but I'm not getting any further, and most excamples are for VB.

    I'm using VS .net 2003 and i'm coding in C++ without MFC/ managed extensions, because I use loads of code that is multi platform.

    Can someone please give me a clue or a hint?
    At the moment my program can draw lines, place & move pushpins etc, so I have a working interface.

    Thanks in advance, (yes, I'm Dutch )

  2. #2
    Wilfried is offline Senior Member Black Belt
    Join Date
    Nov 2004
    Location
    Belgium
    Posts
    2,433
    Hi Harmen,

    I'm not sure I understeand the question, you can just assign an event handler to SelectionChange:

    Code:
    private void MP_SelectionChange(object sender, AxMapPoint._IMappointCtrlEvents_SelectionChangeEvent e)

  3. #3
    Jeurink is offline Junior Member Yellow Belt
    Join Date
    Feb 2005
    Posts
    19
    Hi,

    as far I understand, your solution works only if you use MFC & managed classes.

    This is how my mappoint instance is managed: according the excellent tutorial at http://www.mp2kmag.com/a106--c++.wit....mappoint.html


    Code:
    #include <MapPointMgr.h>
    #include <stdafx.h>
    #include <ColorType.h>
    using namespace MapPoint;
    #define _WIN32_DCOM
    
    /* 
    // class that manages the MapPoint interface/com/activex object. Only one thread can acces it
    */
    
    MapPointMgr&#58;&#58;MapPointMgr&#40;&#41;
    &#123;
    	// initialise the mappoint window
    	hRes = &#58;&#58;CoInitialize&#40;NULL&#41;;
    	
    	// create a mappoint instance
    	myMapPointPtr.CreateInstance&#40; "MapPoint.Application" &#41;;
    
    	myMapPoint = &#40;_Application*&#41;myMapPointPtr;
    	
    	myMapPoint->Visible = true;
    	myMapPtr = myMapPoint->ActiveMap;
    	myMap = &#40;_Map*&#41;myMapPtr;
    &#125;
    
    void MapPointMgr&#58;&#58;MP_SelectionChange&#40;object sender, AxMapPoint._IMappointCtrlEvents_SelectionChangeEvent e&#41;&#123;
    	std&#58;&#58;cout << "Selection changed! " << std&#58;&#58;endl;
    &#125;
    
    void MapPointMgr&#58;&#58;PlacePushPin&#40;double latitude, double longitude, const std&#58;&#58;string &desc&#41;&#123;
    	// place a pushpin on the map with given coordinates
    	// the coordinates must be in dd.dddd format
    	LocationPtr locPtr = myMap->GetLocation&#40; latitude, longitude, 0 &#41;;
    	Location *loc = &#40;Location*&#41;locPtr;
    
    	_bstr_t desc2&#40;""&#41;; // a std&#58;&#58;string is not accepted as description...
    	bla2+=desc.c_str&#40;&#41;;
    
    	PushpinPtr pinPtr = myMap->AddPushpin&#40; loc, desc2 &#41;;
    	Pushpin* pin = &#40;Pushpin*&#41; pinPtr;
    
    	LocationPtr goToPtr = myMap->GetLocation&#40; latitude, longitude, 1 &#41;;
    	Location *goTo = &#40;Location*&#41;goToPtr;
    	pin->BalloonState = geoDisplayBalloon;
    
    	goTo->GoTo&#40;&#41;;
    
    	locPtr.Release&#40;&#41;;
    	pinPtr.Release&#40;&#41;;
    &#125;
    When I try to compile i get errors such as
    MapPointMgr.h(15): error C2061: syntax error : identifier 'object'

    I wonder how to attach my program to mappoint in such way that my code is called.
    It can be done with COM add -ins (and that is what they do in all VB examples, but in those examples they are using wizards to generate the code..)

  4. #4
    Winwaed's Avatar
    Winwaed is offline Mapping-Tools.com Black Belt
    Join Date
    Feb 2004
    Location
    Irving,Texas
    Posts
    1,859
    Blog Entries
    60
    I'm glad you liked my article

    I have trapped events in non-MFC C++ but it took a bit of working out. I think I borrowed some MS-Word examples. Here's some code snippets (ignore the word references!) from a proof-of-concept program I wrote over a year ago:

    Call this to Start MapPoint:
    Code:
    STDMETHODIMP GWordEngine&#58;&#58;StartWord&#40;&#41;
    &#123; 
    	m_pWord.CreateInstance&#40;"MapPoint.Application"&#41;;
        DispEventAdvise&#40;m_pWord&#41;;
    
    		_Application*	myMapPoint = &#40;_Application*&#41;m_pWord;
    		ATLASSERT&#40; myMapPoint != NULL &#41;;
    		myMapPoint->Visible = true;
    		myMapPoint->PutWindowState&#40;geoWindowStateNormal&#41;;
    
    		_MapPtr myMapPtr = myMapPoint->ActiveMap;
    
    		DispEventAdvise&#40;myMapPtr&#41;;
    		
    		_Map* myMap = &#40;_Map*&#41;myMapPtr;
    		ATLASSERT&#40; myMap!=NULL &#41;;
    
    		// Add three pushpins using this symbol
                    // &#40;this is my own routine - it just  adds a pushpin&#41;
    		addPushPin&#40;myMap, "Bardsey Primary School", "Woodacre Lane", "Bardsey", "Leeds", "LS17 9"&#41;;
    		addPushPin&#40;myMap, "Shadwell Primary School", "Main Street", "Shadwell", "Leeds", "LS17 8"&#41;;
    		addPushPin&#40;myMap, "Boston Spa Comprehensive", "Clifford Moor Road", "Boston Spa", "Leeds", "LS23"&#41;;
    
    		// Zoom
    		myMap->Altitude = 20;
    
    		// Reset the "Map Needs Saving" flag
    		myMap->Saved = true;
    
    	return S_OK;
    &#125;

    Here's one of the event handlers:

    Code:
    STDMETHODIMP GWordEngine&#58;&#58;OnSelection&#40; IDispatch* newSelection, IDispatch* oldSelection&#41;
    &#123;
    	// Get old selection - if it is a pushpin, hide the notes/caption
    	if &#40;oldSelection&#41;
    	&#123;
    		Pushpin *oldPin;
    		HRESULT hr = oldSelection->QueryInterface&#40; __uuidof&#40;Pushpin&#41;  , &#40;void**&#41;&oldPin&#41;;
    		if &#40;SUCCEEDED&#40;hr&#41;&#41;
    		&#123;
    			oldPin->BalloonState = geoDisplayNone;
    		&#125;
    	&#125;
    
    	// Get new selection - if it is a pushpin, display notes/caption and modify it	
    	if &#40;newSelection&#41;
    	&#123;
    		Pushpin *thisPin;
    		HRESULT hr = newSelection->QueryInterface&#40; __uuidof&#40;Pushpin&#41;  , &#40;void**&#41;&thisPin&#41;;
    		if &#40;SUCCEEDED&#40;hr&#41;&#41;
    		&#123;
    			thisPin->Note = "Modified";
    			thisPin->BalloonState = geoDisplayBalloon;
    		&#125;
    	&#125;
    
    	return S_OK;
    &#125;

    The .h file for the above object is defined:

    Code:
    // GXWordEngine.h &#58; Declaration of the GWordEngine
    
    #ifndef __WORDENGINE_H_
    #define __WORDENGINE_H_
    
    #include "resource.h"       // main symbols
    #include "WordEvents.h"
    
    /////////////////////////////////////////////////////////////////////////////
    // GWordEngine
    
    #pragma warning&#40;disable&#58; 4146&#41;
    
    #undef EOF
    #import "c&#58;\Program Files\Microsoft MapPoint Europe\mpeu81.tlb" no_implementation
    
    
    extern _ATL_FUNC_INFO OnQuitInfo;
    extern _ATL_FUNC_INFO OnSelectionInfo;
    
    //#include "WordEventsview.h"
    
    
    class ATL_NO_VTABLE GWordEngine &#58; 
    	                public CComObjectRootEx<CComSingleThreadModel>,
    	                public CComCoClass<GWordEngine, &CLSID_WordEngine>,
    	                public IDispatchImpl<IWordEngine, &IID_IWordEngine, &LIBID_WORDEVENTSLib>,
                        public IDispEventSimpleImpl<1,GWordEngine, &__uuidof&#40;MapPoint&#58;&#58;_MapEvents&#41;>
    //                    public IDispEventSimpleImpl<1,GWordEngine, &__uuidof&#40;MapPoint&#58;&#58;_ApplicationEvents&#41;>
    &#123;
    public&#58;
      MapPoint&#58;&#58;_ApplicationPtr     m_pWord;
    	
    
                            GWordEngine&#40;&#41;&#123;&#125;
    
    BEGIN_SINK_MAP&#40;GWordEngine&#41;
       SINK_ENTRY_INFO&#40;1, __uuidof&#40;MapPoint&#58;&#58;_MapEvents&#41;, 0x06041607, OnSelection, &OnSelectionInfo&#41;
    
    END_SINK_MAP&#40;&#41;
    DECLARE_REGISTRY_RESOURCEID&#40;IDR_WORDENGINE&#41;
    
    DECLARE_PROTECT_FINAL_CONSTRUCT&#40;&#41;
    
    BEGIN_COM_MAP&#40;GWordEngine&#41;
    	COM_INTERFACE_ENTRY&#40;IWordEngine&#41;
    	COM_INTERFACE_ENTRY&#40;IDispatch&#41;
    END_COM_MAP&#40;&#41;
    BEGIN_CONNECTION_POINT_MAP&#40;GWordEngine&#41;
    END_CONNECTION_POINT_MAP&#40;&#41;
    
    
    // IWordEngine
    public&#58;
    	STDMETHOD&#40;CloseWord&#41;&#40;&#41;;
    	STDMETHOD&#40;StartWord&#41;&#40;&#41;;
    	STDMETHOD&#40;OnDocChange&#41;&#40;&#41;;
    	STDMETHOD&#40;OnQuit&#41;&#40;&#41;;
    
    	STDMETHOD&#40;OnSelection&#41; &#40; IDispatch* newSelection, IDispatch* oldSelection&#41;;
    
    //	STDMETHOD&#40;OnBeforeClick&#41;&#40; long Button, long Shift, long x, long y, VARIANT_BOOL* Cancel &#41;;
    
    //	STDMETHODIMP SetView&#40; /*&#91;in&#93;*/ CWordEventsView* pView&#41;;
    &#125;;
    
    #endif //__WORDENGINE_H_

    Hope this gives you enough to get you started. It is a while since I've used the above code, and it was hacked together as a proof-of-concept.

    Richard
    Winwaed Software Technology LLC
    http://www.winwaed.com
    See http://www.mapping-tools.com for MapPoint Tools

  5. #5
    Wilfried is offline Senior Member Black Belt
    Join Date
    Nov 2004
    Location
    Belgium
    Posts
    2,433
    Hi,

    Quote Originally Posted by Jeurink
    as far I understand, your solution works only if you use MFC & managed classes.
    Oups sorry, I have to learn reading mail better You wrote C++ and I have read C#

  6. #6
    Jeurink is offline Junior Member Yellow Belt
    Join Date
    Feb 2005
    Posts
    19
    Hi,

    first: thanks Winwaed, you are really helping
    I was going your way already, but i decided to give up, because I hardly understand what I'm doing, only resolving compile errors for day after day. Very frustrating!

    But i was wondering if you could provide me some more source code?
    I've got now 3 not working examples (yours, WordEvents from MS, and ATLEventHandler also from MS) and all together it should work, but they use different names, interfaces everywhere.

    again, thanks in advance!

    this is what i have so far:

    Code:
    // MapPointMgr.h
    #ifndef MapPointMgrH
    #define MapPointMgrH
    
    #include <mpinterfaces.h>
    
    #include <string>
    #include <GPSData.h>
    
    #include <atlbase.h>
    #include <atlcom.h>
    
    #include <guiddef.h>
    #include <resource.h>
    #include <IWordEngine.h>
    
    extern _ATL_FUNC_INFO OnQuitInfo;
    extern _ATL_FUNC_INFO OnSelectionInfo; 
    
    #define MIDL_DEFINE_GUID&#40;type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8&#41; \
            DEFINE_GUID&#40;name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8&#41;
    
    typedef IID CLSID;
    
    MIDL_DEFINE_GUID&#40;IID, IID_ISwitch,0xBC7648FD,0xB55E,0x11D2,0xBA,0x67,0x00,0xC0,0x4F,0x8E,0xC8,0x47&#41;;
    MIDL_DEFINE_GUID&#40;IDD, IID_IWordEngine,0xAC7648FD,0xB55E,0x11D2,0xBA,0x67,0x00,0xC0,0x4F,0x8E,0xC8,0x47&#41;;
    MIDL_DEFINE_GUID&#40;IID, LIBID_WORDEVENTSLib,0xBC7648EF,0xB55E,0x11D2,0xBA,0x67,0x00,0xC0,0x4F,0x8E,0xC8,0x47&#41;;
    MIDL_DEFINE_GUID&#40;CLSID, CLSID_WordEngine,0xBC7648FC,0xB55E,0x11D2,0xBA,0x67,0x00,0xC0,0x4F,0x8E,0xC8,0x47&#41;;
    MIDL_DEFINE_GUID&#40;IWordEngine, IWordEngingeGui, 0xBC7647FC,0xB55E,0x11D2,0xBA,0x67,0x00,0xC0,0x4F,0x8E,0xC8,0x47&#41;;
    
    class ATL_NO_VTABLE MapPointMgr &#58;
    					public CComObjectRootEx<CComSingleThreadModel>,
    					public CComCoClass<MapPointMgr, &CLSID_WordEngine>,
    					public IDispatchImpl<IWordEngine, &IID_IWordEngine, &LIBID_WORDEVENTSLib>,
                        public IDispEventSimpleImpl<1,MapPointMgr, &__uuidof&#40;MapPoint&#58;&#58;_MapEvents&#41;>
    &#123;
    public&#58;
    	MapPointMgr&#40;&#41;;
    
    BEGIN_SINK_MAP&#40;MapPointMgr&#41;
       SINK_ENTRY_INFO&#40;1, __uuidof&#40;MapPoint&#58;&#58;_MapEvents&#41;, 0x06041607, OnSelection, &OnSelectionInfo&#41;
    END_SINK_MAP&#40;&#41; 
    
    DECLARE_REGISTRY_RESOURCEID&#40;IDR_WORDENGINE&#41;
    
    DECLARE_PROTECT_FINAL_CONSTRUCT&#40;&#41;
    
    BEGIN_COM_MAP&#40;MapPointMgr&#41;
       COM_INTERFACE_ENTRY&#40;IWordEngine&#41; // <-- compiler starts to complain here&#58;&#58; error C2787&#58; 'IWordEngine' &#58; no GUID has been associated with this object
    
       COM_INTERFACE_ENTRY&#40;IDispatch&#41;
    END_COM_MAP&#40;&#41;
    
    BEGIN_CONNECTION_POINT_MAP&#40;MapPointMgr&#41;
    END_CONNECTION_POINT_MAP&#40;&#41; 
    
    	~MapPointMgr&#40;void&#41;;
    	STDMETHODIMP StartMapPoint&#40;&#41;;
    	STDMETHODIMP OnSelection&#40; IDispatch* newSelection, IDispatch* oldSelection&#41;;
    
    	void MapPointMgr&#58;&#58;PlacePushPin&#40;double latitude, double longitude, const std&#58;&#58;string &desc&#41;;
    	void MapPointMgr&#58;&#58;UpdateIcon&#40;const std&#58;&#58;string &desc, double newLatitude, double newLongitude&#41;;
    	void MapPointMgr&#58;&#58;goToCoordinates&#40;double latitude, double longitude&#41;;
    	void MapPointMgr&#58;&#58;DrawLine&#40;double latitude1, double longitude1, double latitude2, double longitude2, int colorType, const&#58;&#58;std&#58;&#58;string &desc&#41;;
    	void MapPointMgr&#58;&#58;Close&#40;&#41;;
    	
    private&#58;
    	HRESULT hRes;
    
    	MapPoint&#58;&#58;_Map *myMap;
    
    	MapPoint&#58;&#58;_ApplicationPtr myMapPointPtr;
    	MapPoint&#58;&#58;_Application *myMapPoint;
    	MapPoint&#58;&#58;_MapPtr myMapPtr;
    &#125;;
    
    #endif //MapPointMgrH
    Code:
    //MapPointMgr.cpp
    #include <MapPointMgr.h>
    #include <boost/lexical_cast.hpp>
    
    #include <CFifo.h>
    #include <ColorType.h>
    
    #include <iostream>
    
    using namespace MapPoint;
    #define _WIN32_DCOM
    
    
    /* 
    // class that manages the MapPoint interface/com/activex object. Only one thread can acces it
    // so instructions from other threads should be put in oMapPointMgrInstructionBuffer.
    */
    
    MapPointMgr&#58;&#58;MapPointMgr&#40;&#41;
    &#123;
    	// initialise the Com environment
    	hRes = &#58;&#58;CoInitialize&#40;NULL&#41;;
    
    	StartMapPoint&#40;&#41;;
    	
    	
    // Forge a connection to enable us to receive events
    //    DispEventAdvise&#40;myMapPointPtr&#41;;
    
    &#125;
    
    STDMETHODIMP MapPointMgr&#58;&#58;StartMapPoint&#40;&#41;
    &#123;
    	myMapPointPtr.CreateInstance&#40;"MapPoint.Application"&#41;;
    
    	DispEventAdvise&#40;myMapPointPtr&#41;;
    
    	_Application*   myMapPoint = &#40;_Application*&#41;myMapPointPtr;
    
    	myMapPoint->Visible = true;
    
    	myMapPtr = myMapPoint->ActiveMap;
    
    	DispEventAdvise&#40;myMapPtr&#41;;
    
    	_Map* myMap = &#40;_Map*&#41;myMapPtr;
    
    	myMap->Saved = true;
    
        return S_OK;
    &#125; 
    
    STDMETHODIMP MapPointMgr&#58;&#58;OnSelection&#40; IDispatch* newSelection, IDispatch* oldSelection&#41;
    &#123;
    	std&#58;&#58;cout << "Catched EVENT!!" << std&#58;&#58;endl;
            return S_OK;
    &#125;
    and

    Code:
    // IWordEngine interface&#40;?&#41;
    #ifndef __WORDENGINE_H_
    #define __WORDENGINE_H_
    // IWordEngine
    class IWordEngine&#123;
    
    public&#58;
       STDMETHOD&#40;CloseWord&#41;&#40;&#41;;
       STDMETHOD&#40;StartWord&#41;&#40;&#41;;
       STDMETHOD&#40;OnDocChange&#41;&#40;&#41;;
       STDMETHOD&#40;OnQuit&#41;&#40;&#41;;
    
       STDMETHOD&#40;OnSelection&#41; &#40; IDispatch* newSelection, IDispatch* oldSelection&#41;;
    
    //   STDMETHOD&#40;OnBeforeClick&#41;&#40; long Button, long Shift, long x, long y, VARIANT_BOOL* Cancel &#41;;
    
    //   STDMETHODIMP SetView&#40; /*&#91;in&#93;*/ CWordEventsView* pView&#41;;
    &#125;;
    
    #endif //__WORDENGINE_H_

  7. #7
    Winwaed's Avatar
    Winwaed is offline Mapping-Tools.com Black Belt
    Join Date
    Feb 2004
    Location
    Irving,Texas
    Posts
    1,859
    Blog Entries
    60
    I think the WordEngine code depends on MS-Word, doesn't it?
    (and not MapPoint).

    It is a while since I was working with stuff, but I think I had to extract the GUI codes by hand from one of the files (.tlh?)


    Richard
    Winwaed Software Technology LLC
    http://www.winwaed.com
    See http://www.mapping-tools.com for MapPoint Tools

  8. #8
    Jeurink is offline Junior Member Yellow Belt
    Join Date
    Feb 2005
    Posts
    19
    Yes, I understand,
    but it has no need to rename it to IMapPointMgr because then I have to rename my IWordEngine class to IMapPoingMgr, and then get the same problem.
    eg. (error C2787: 'IMapPointMgr' : No GUID has been associated with this object)

    It is a while since I was working with stuff, but I think I had to extract the GUI codes by hand from one of the files (.tlh?)
    Okay I'm going to find out. Thanks again.

  9. #9
    Jeurink is offline Junior Member Yellow Belt
    Join Date
    Feb 2005
    Posts
    19
    Just for the record:

    It should be possible to catch events via C++, although I think you must have a good knowledge of COM compontents / controls, and how they work.

    I started all over again and ported all code to C#, and I got events working in about 20 minutes with an C# word automation sample from (http://support.microsoft.com/default...b;EN-US;302817) and Visual Studio .NET 2003. This editor really simplifies catching of events.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to send events to the MapPoint Control (in C#) ?
    By Anonymous in forum MapPoint Desktop Discussion
    Replies: 3
    Last Post: 12-25-2004, 07:17 AM
  2. Triggering Events in MapPoint...
    By joer1234 in forum MapPoint Desktop Discussion
    Replies: 0
    Last Post: 09-29-2004, 11:52 PM
  3. Events and VB code
    By tanguy_laverdure in forum MapPoint Desktop Discussion
    Replies: 0
    Last Post: 05-05-2004, 04:42 AM
  4. Optimize Events
    By Anonymous in forum MapPoint Desktop Discussion
    Replies: 0
    Last Post: 12-03-2003, 12:45 PM
  5. Events Communication
    By Anonymous in forum MapPoint Desktop Discussion
    Replies: 0
    Last Post: 09-16-2002, 09:19 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25