Community of VE/MapPoint Users and Developers
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 ...
| |||||||
| Register | Blogs | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| |||
| Catch mappoint events using C++ (without MFC...) 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 |
| |||
|
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)
__________________ rgds, Wilfried Mestdagh www.mestdagh.biz MapPoint coding demo Order MapPoint 2009 with Routing and User Tools Spreadsheet |
| |||
|
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();
}
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..) |
| ||||
|
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 |
| |||
|
Hi, Quote:
__________________ rgds, Wilfried Mestdagh www.mestdagh.biz MapPoint coding demo Order MapPoint 2009 with Routing and User Tools Spreadsheet |
| |||
|
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;
}
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_
|
| ||||
|
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 |
| |||
|
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:
|
| |||
|
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. |
![]() |
| Tags |
| catch, events, mappoint, mfc |
| ||||
| 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 | |
| |
| ||||
| 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 |