MapPoint Forums

MapForums

Community of VE/MapPoint Users and Developers




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

This is a discussion on Catch mappoint events using C++ (without MFC...) within the MapPoint 2006/2009 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 ...


Go Back   MapPoint Forums > Map Forums > MapPoint 2006/2009 Discussion

Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read



Click here to register

Reply

 

LinkBack (2) Thread Tools Display Modes
  2 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 02-23-2005
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 )
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #2 (permalink)  
Old 02-23-2005
Senior Member
Black Belt
 
Join Date: Nov 2004
Posts: 2,112
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3 (permalink)  
Old 02-23-2005
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::MapPointMgr()
{
	// initialise the mappoint window
	hRes = ::CoInitialize(NULL);
	
	// create a mappoint instance
	myMapPointPtr.CreateInstance( "MapPoint.Application" );

	myMapPoint = (_Application*)myMapPointPtr;
	
	myMapPoint->Visible = true;
	myMapPtr = myMapPoint->ActiveMap;
	myMap = (_Map*)myMapPtr;
}

void MapPointMgr::MP_SelectionChange(object sender, AxMapPoint._IMappointCtrlEvents_SelectionChangeEvent e){
	std::cout << "Selection changed! " << std::endl;
}

void MapPointMgr::PlacePushPin(double latitude, double longitude, const std::string &desc){
	// place a pushpin on the map with given coordinates
	// the coordinates must be in dd.dddd format
	LocationPtr locPtr = myMap->GetLocation( latitude, longitude, 0 );
	Location *loc = (Location*)locPtr;

	_bstr_t desc2(""); // a std::string is not accepted as description...
	bla2+=desc.c_str();

	PushpinPtr pinPtr = myMap->AddPushpin( loc, desc2 );
	Pushpin* pin = (Pushpin*) pinPtr;

	LocationPtr goToPtr = myMap->GetLocation( latitude, longitude, 1 );
	Location *goTo = (Location*)goToPtr;
	pin->BalloonState = geoDisplayBalloon;

	goTo->GoTo();

	locPtr.Release();
	pinPtr.Release();
}
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..)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4 (permalink)  
Old 02-23-2005
Winwaed's Avatar
Mapping-Tools.com
Red Belt
 
Join Date: Feb 2004
Posts: 893
Blog Entries: 10
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::StartWord()
{ 
	m_pWord.CreateInstance("MapPoint.Application");
    DispEventAdvise(m_pWord);

		_Application*	myMapPoint = (_Application*)m_pWord;
		ATLASSERT( myMapPoint != NULL );
		myMapPoint->Visible = true;
		myMapPoint->PutWindowState(geoWindowStateNormal);

		_MapPtr myMapPtr = myMapPoint->ActiveMap;

		DispEventAdvise(myMapPtr);
		
		_Map* myMap = (_Map*)myMapPtr;
		ATLASSERT( myMap!=NULL );

		// Add three pushpins using this symbol
                // (this is my own routine - it just  adds a pushpin)
		addPushPin(myMap, "Bardsey Primary School", "Woodacre Lane", "Bardsey", "Leeds", "LS17 9");
		addPushPin(myMap, "Shadwell Primary School", "Main Street", "Shadwell", "Leeds", "LS17 8");
		addPushPin(myMap, "Boston Spa Comprehensive", "Clifford Moor Road", "Boston Spa", "Leeds", "LS23");

		// Zoom
		myMap->Altitude = 20;

		// Reset the "Map Needs Saving" flag
		myMap->Saved = true;

	return S_OK;
}

Here's one of the event handlers:

Code:
STDMETHODIMP GWordEngine::OnSelection( IDispatch* newSelection, IDispatch* oldSelection)
{
	// Get old selection - if it is a pushpin, hide the notes/caption
	if (oldSelection)
	{
		Pushpin *oldPin;
		HRESULT hr = oldSelection->QueryInterface( __uuidof(Pushpin)  , (void**)&oldPin);
		if (SUCCEEDED(hr))
		{
			oldPin->BalloonState = geoDisplayNone;
		}
	}

	// Get new selection - if it is a pushpin, display notes/caption and modify it	
	if (newSelection)
	{
		Pushpin *thisPin;
		HRESULT hr = newSelection->QueryInterface( __uuidof(Pushpin)  , (void**)&thisPin);
		if (SUCCEEDED(hr))
		{
			thisPin->Note = "Modified";
			thisPin->BalloonState = geoDisplayBalloon;
		}
	}

	return S_OK;
}

The .h file for the above object is defined:

Code:
// GXWordEngine.h : Declaration of the GWordEngine

#ifndef __WORDENGINE_H_
#define __WORDENGINE_H_

#include "resource.h"       // main symbols
#include "WordEvents.h"

/////////////////////////////////////////////////////////////////////////////
// GWordEngine

#pragma warning(disable: 4146)

#undef EOF
#import "c:\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 : 
	                public CComObjectRootEx<CComSingleThreadModel>,
	                public CComCoClass<GWordEngine, &CLSID_WordEngine>,
	                public IDispatchImpl<IWordEngine, &IID_IWordEngine, &LIBID_WORDEVENTSLib>,
                    public IDispEventSimpleImpl<1,GWordEngine, &__uuidof(MapPoint::_MapEvents)>
//                    public IDispEventSimpleImpl<1,GWordEngine, &__uuidof(MapPoint::_ApplicationEvents)>
{
public:
  MapPoint::_ApplicationPtr     m_pWord;
	

                        GWordEngine(){}

BEGIN_SINK_MAP(GWordEngine)
   SINK_ENTRY_INFO(1, __uuidof(MapPoint::_MapEvents), 0x06041607, OnSelection, &OnSelectionInfo)

END_SINK_MAP()
DECLARE_REGISTRY_RESOURCEID(IDR_WORDENGINE)

DECLARE_PROTECT_FINAL_CONSTRUCT()

BEGIN_COM_MAP(GWordEngine)
	COM_INTERFACE_ENTRY(IWordEngine)
	COM_INTERFACE_ENTRY(IDispatch)
END_COM_MAP()
BEGIN_CONNECTION_POINT_MAP(GWordEngine)
END_CONNECTION_POINT_MAP()


// IWordEngine
public:
	STDMETHOD(CloseWord)();
	STDMETHOD(StartWord)();
	STDMETHOD(OnDocChange)();
	STDMETHOD(OnQuit)();

	STDMETHOD(OnSelection) ( IDispatch* newSelection, IDispatch* oldSelection);

//	STDMETHOD(OnBeforeClick)( long Button, long Shift, long x, long y, VARIANT_BOOL* Cancel );

//	STDMETHODIMP SetView( /*[in]*/ CWordEventsView* pView);
};

#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
Pre-Order MapPoint 2009 today: http://www.mapping-tools.com/mappoint2009
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5 (permalink)  
Old 02-23-2005
Senior Member
Black Belt
 
Join Date: Nov 2004
Posts: 2,112
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#
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6 (permalink)  
Old 02-24-2005
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(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \
        DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8)

typedef IID CLSID;

MIDL_DEFINE_GUID(IID, IID_ISwitch,0xBC7648FD,0xB55E,0x11D2,0xBA,0x67,0x00,0xC0,0x4F,0x8E,0xC8,0x47);
MIDL_DEFINE_GUID(IDD, IID_IWordEngine,0xAC7648FD,0xB55E,0x11D2,0xBA,0x67,0x00,0xC0,0x4F,0x8E,0xC8,0x47);
MIDL_DEFINE_GUID(IID, LIBID_WORDEVENTSLib,0xBC7648EF,0xB55E,0x11D2,0xBA,0x67,0x00,0xC0,0x4F,0x8E,0xC8,0x47);
MIDL_DEFINE_GUID(CLSID, CLSID_WordEngine,0xBC7648FC,0xB55E,0x11D2,0xBA,0x67,0x00,0xC0,0x4F,0x8E,0xC8,0x47);
MIDL_DEFINE_GUID(IWordEngine, IWordEngingeGui, 0xBC7647FC,0xB55E,0x11D2,0xBA,0x67,0x00,0xC0,0x4F,0x8E,0xC8,0x47);

class ATL_NO_VTABLE MapPointMgr :
					public CComObjectRootEx<CComSingleThreadModel>,
					public CComCoClass<MapPointMgr, &CLSID_WordEngine>,
					public IDispatchImpl<IWordEngine, &IID_IWordEngine, &LIBID_WORDEVENTSLib>,
                    public IDispEventSimpleImpl<1,MapPointMgr, &__uuidof(MapPoint::_MapEvents)>
{
public:
	MapPointMgr();

BEGIN_SINK_MAP(MapPointMgr)
   SINK_ENTRY_INFO(1, __uuidof(MapPoint::_MapEvents), 0x06041607, OnSelection, &OnSelectionInfo)
END_SINK_MAP() 

DECLARE_REGISTRY_RESOURCEID(IDR_WORDENGINE)

DECLARE_PROTECT_FINAL_CONSTRUCT()

BEGIN_COM_MAP(MapPointMgr)
   COM_INTERFACE_ENTRY(IWordEngine) // <-- compiler starts to complain here:: error C2787: 'IWordEngine' : no GUID has been associated with this object

   COM_INTERFACE_ENTRY(IDispatch)
END_COM_MAP()

BEGIN_CONNECTION_POINT_MAP(MapPointMgr)
END_CONNECTION_POINT_MAP() 

	~MapPointMgr(void);
	STDMETHODIMP StartMapPoint();
	STDMETHODIMP OnSelection( IDispatch* newSelection, IDispatch* oldSelection);

	void MapPointMgr::PlacePushPin(double latitude, double longitude, const std::string &desc);
	void MapPointMgr::UpdateIcon(const std::string &desc, double newLatitude, double newLongitude);
	void MapPointMgr::goToCoordinates(double latitude, double longitude);
	void MapPointMgr::DrawLine(double latitude1, double longitude1, double latitude2, double longitude2, int colorType, const::std::string &desc);
	void MapPointMgr::Close();
	
private:
	HRESULT hRes;

	MapPoint::_Map *myMap;

	MapPoint::_ApplicationPtr myMapPointPtr;
	MapPoint::_Application *myMapPoint;
	MapPoint::_MapPtr myMapPtr;
};

#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::MapPointMgr()
{
	// initialise the Com environment
	hRes = ::CoInitialize(NULL);

	StartMapPoint();
	
	
// Forge a connection to enable us to receive events
//    DispEventAdvise(myMapPointPtr);

}

STDMETHODIMP MapPointMgr::StartMapPoint()
{
	myMapPointPtr.CreateInstance("MapPoint.Application");

	DispEventAdvise(myMapPointPtr);

	_Application*   myMapPoint = (_Application*)myMapPointPtr;

	myMapPoint->Visible = true;

	myMapPtr = myMapPoint->ActiveMap;

	DispEventAdvise(myMapPtr);

	_Map* myMap = (_Map*)myMapPtr;

	myMap->Saved = true;

    return S_OK;
} 

STDMETHODIMP MapPointMgr::OnSelection( IDispatch* newSelection, IDispatch* oldSelection)
{
	std::cout << "Catched EVENT!!" << std::endl;
        return S_OK;
}
and

Code:
// IWordEngine interface(?)
#ifndef __WORDENGINE_H_
#define __WORDENGINE_H_
// IWordEngine
class IWordEngine{

public:
   STDMETHOD(CloseWord)();
   STDMETHOD(StartWord)();
   STDMETHOD(OnDocChange)();
   STDMETHOD(OnQuit)();

   STDMETHOD(OnSelection) ( IDispatch* newSelection, IDispatch* oldSelection);

//   STDMETHOD(OnBeforeClick)( long Button, long Shift, long x, long y, VARIANT_BOOL* Cancel );

//   STDMETHODIMP SetView( /*[in]*/ CWordEventsView* pView);
};

#endif //__WORDENGINE_H_
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7 (permalink)  
Old 02-24-2005
Winwaed's Avatar
Mapping-Tools.com
Red Belt
 
Join Date: Feb 2004
Posts: 893
Blog Entries: 10
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
Pre-Order MapPoint 2009 today: http://www.mapping-tools.com/mappoint2009
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8 (permalink)  
Old 02-25-2005
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)

Quote:
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9 (permalink)  
Old 03-15-2005
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
catch, events, mappoint, mfc


LinkBacks (?)
LinkBack to this Thread: http://www.mapforums.com/catch-mappoint-events-using-c-without-mfc-3866.html

Posted By For Type Date
St Louis Mortgage Refinance - louis mortgage refinance, and mortgage louis This thread Refback 09-06-2007 06:08 PM
Web Link This thread Refback 09-05-2007 08:45 PM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
How to send events to the MapPoint Control (in C#) ? Anonymous MapPoint 2006/2009 Discussion 3 12-25-2004 07:17 AM
Triggering Events in MapPoint... joer1234 MapPoint 2006/2009 Discussion 0 09-29-2004 11:52 PM
Events and VB code tanguy_laverdure MapPoint 2006/2009 Discussion 0 05-05-2004 04:42 AM
Optimize Events Anonymous MapPoint 2006/2009 Discussion 0 12-03-2003 12:45 PM
Events Communication Anonymous MapPoint 2006/2009 Discussion 0 09-16-2002 09:19 AM


All times are GMT -5. The time now is 02:15 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
MP2K Magazine
Visitor Map


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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54