VC6.0制作MapX自定义工具(测距工具)

申明:

本人在网上找了很多例子,就这个比较完整,经过调试无误,遂贴出来供大家参考,让大家少走弯路(注意要先加载地图)。

参考博客:http://blog.csdn.net/fengrx/archive/2009/05/09/4163187.aspx

以下文章前部分是原文作者的,后部分是本人结合具体细节添加的,也算是对原文的一个完善。

 

制作自定义工具是MapX编程中一个很重要的部分,其步骤比较简单,但很多人很容易忽略其中的部分步骤,使得此功能难以实现。具体可以按以下步骤来做,本人在做这一功能时花费时间颇多,因为大部分介绍这一功能的文章都忽略了其中第三步骤,使得初学者很难成功实现。


     故在原来基础上加以完善,使得步骤更加详细。其他自定义工具可以按照类似步骤来做。


//1.定义常量
#define MYTOOL_DISTANCE 100 //1~999


//2.创建工具
m_ctrlMap.CreateCustomTool(MYTOOL_DISTANCE,miToolTypePoly,miCrossCursor);


//3.创建事件槽
在View.h中,在DECLARE_MESSAGE_MAP()行下边加入一行代码:DECLARE_EVENTSINK_MAP()

在View.cpp中,加入


BEGIN_EVENTSINK_MAP(CPatrolCheckView, CView)
 ON_EVENT(CPatrolCheckView, IDC_MAP, 11/*MAPX_DISPID_POLYTOOLUSED*/, OnPolyToolUsedMap, 
VTS_I2 VTS_I4  VTS_DISPATCH VTS_BOOL VTS_BOOL VTS_PBOOL)
 END_EVENTSINK_MAP()


 
//4.响应事件
在View.h中添加 void OnPolyToolUsedMap(short ToolNum, long Flags, LPDISPATCH Points,BOOL 


bShift,BOOL bCtrl,BOOL FAR* EnableDefault);
在View.cpp中,加入实现代码
void CMapXSampleView::OnPolyToolUsedMap(short ToolNum, long Flags, LPDISPATCH Points,
BOOL bShift,BOOL bCtrl,BOOL FAR* EnableDefault)
{
//计算距离
if(Flags==miPolyToolInProgress) {//这句可有可无
if(ToolNum==MYTOOL_DISTANCE)
{
CMapXPoints pts;
long n;
long i;
try{
pts.AttachDispatch (Points, FALSE); // Get points Object
double dDistanceTot=0.0;
n=pts.GetCount ();
m_ctrlMapX.SetMapUnit(miUnitKilometer);
for (i=1; i<n;i++)
{
CMapXPoint pt1=pts.Item(i);
CMapXPoint pt2=pts.Item(i+1);
double d=m_ctrlMapX.Distance (pt1.GetX(),pt1.GetY(),pt2.GetX (),
pt2.GetY ());
dDistanceTot+=d;  
}
CString str;
str.Format ("总距离为:%f",dDistanceTot);
//在状态栏输出距离
((CMainFrame*)AfxGetMainWnd())->m_wndStatusBar.SetPaneText(0,str); //要先将m_wndStatusBar设


为public
}
catch (COleDispatchException *e){
e->ReportError ();
e->Delete ();
}
catch (COleException *e){
e->ReportError ();
e->Delete ();
}
}
}//if(Flags==miPolyToolInProgress) 
}


//5.调用工具
void CMapXSampleView::OnMapToolDistancetool() 
{
m_ctrlMapX.SetCurrentTool(MYTOOL_DISTANCE); // 




注意:ON_EVENT(CPatrolCheckView,  IDC_MAP11/*MAPX_DISPID_POLYTOOLUSED*/, OnPolyToolUsedMap, 中标红的地方,具体工具类型的判断在OnPolyToolUsedMap中进行

以上部分参考:CSDN博客,转载请标明出处:http://blog.csdn.net/fengrx/archive/2009/05/09/4163187.aspx


下面是我完整的代码:

// TestTView.h : interface of the CTestTView class

//
/////////////////////////////////////////////////////////////////////////////


#if !defined(AFX_TESTTVIEW_H__605D4332_7071_4CE3_9EFE_73D15BB50061__INCLUDED_)
#define AFX_TESTTVIEW_H__605D4332_7071_4CE3_9EFE_73D15BB50061__INCLUDED_


#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


#include "MapX.h"
#include "MainFrm.h"
class CTestTView : public CView
{
protected: // create from serialization only
CTestTView();
DECLARE_DYNCREATE(CTestTView)


// Attributes
public:
CTestTDoc* GetDocument();


// Operations
public:


// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTestTView)
public:
virtual void OnDraw(CDC* pDC);  // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
//}}AFX_VIRTUAL


// Implementation
public:
BOOL OnToolTest();
void OnPolyToolUsedMap(short ToolNum, long Flags, LPDISPATCH Points,BOOL bShift,BOOL bCtrl,BOOL FAR* EnableDefault);
virtual ~CTestTView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif


protected:
private:
CMapX m_ctrlMapX;
// Generated message map functions
protected:
//{{AFX_MSG(CTestTView)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnTest();
afx_msg void OnSize(UINT nType, int cx, int cy);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
DECLARE_EVENTSINK_MAP()

};


#ifndef _DEBUG  // debug version in TestTView.cpp
inline CTestTDoc* CTestTView::GetDocument()
   { return (CTestTDoc*)m_pDocument; }
#endif


/////////////////////////////////////////////////////////////////////////////


//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.


#endif // !defined(AFX_TESTTVIEW_H__605D4332_7071_4CE3_9EFE_73D15BB50061__INCLUDED_)

// TestTView.cpp : implementation of the CTestTView class
//


#include "stdafx.h"
#include "TestT.h"


#include "TestTDoc.h"
#include "TestTView.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/////////////////////////////////////////////////////////////////////////////
// CTestTView


IMPLEMENT_DYNCREATE(CTestTView, CView)


BEGIN_MESSAGE_MAP(CTestTView, CView)
//{{AFX_MSG_MAP(CTestTView)
ON_WM_CREATE()
ON_COMMAND(ID_TEST, OnTest)
ON_WM_SIZE()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)

END_MESSAGE_MAP()


BEGIN_EVENTSINK_MAP(CTestTView,CView)
ON_EVENT(CTestTView,IDC_MAP,MAPX_DISPID_POLYTOOLUSED,OnPolyToolUsedMap,VTS_I2 VTS_I4  VTS_DISPATCH VTS_BOOL VTS_BOOL VTS_PBOOL)
END_EVENTSINK_MAP()
// CTestTView construction/destruction
CTestTView::CTestTView()
{
// TODO: add construction code here


}


CTestTView::~CTestTView()
{
}


BOOL CTestTView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
//  the CREATESTRUCT cs


return CView::PreCreateWindow(cs);
}


/////////////////////////////////////////////////////////////////////////////
// CTestTView drawing


void CTestTView::OnDraw(CDC* pDC)
{
CTestTDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}


/////////////////////////////////////////////////////////////////////////////
// CTestTView printing


BOOL CTestTView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}


void CTestTView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}


void CTestTView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}


/////////////////////////////////////////////////////////////////////////////
// CTestTView diagnostics


#ifdef _DEBUG
void CTestTView::AssertValid() const
{
CView::AssertValid();
}


void CTestTView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}


CTestTDoc* CTestTView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestTDoc)));
return (CTestTDoc*)m_pDocument;
}
#endif //_DEBUG


/////////////////////////////////////////////////////////////////////////////
// CTestTView message handlers


int CTestTView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
if(!m_ctrlMapX.Create(NULL,WS_VISIBLE,CRect(0,0,100,100),this,IDC_MAP))
return -1;
m_ctrlMapX.SetGeoSet("CHINA.GST");
m_ctrlMapX.SetTitleText("");
//创建自定义工具
m_ctrlMapX.CreateCustomTool(DRAG_TOOL,miToolTypePoly,miCrossCursor);
// TODO: Add your specialized creation code here   miSizeAllCursor
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
return 0;
}


void CTestTView::OnPolyToolUsedMap(short ToolNum, long Flags, LPDISPATCH Points,
BOOL bShift,BOOL bCtrl,BOOL FAR*  EnableDefault)
{
((CMainFrame*)AfxGetMainWnd())->m_wndStatusBar.SetPaneText(0,"开始计算");
//计算距离
//if(Flags==miPolyToolInProgress) {//这句可有可无
if(ToolNum==DRAG_TOOL)
{
CMapXPoints pts;
long n;
long i;
try{
pts.AttachDispatch (Points, FALSE); // Get points Object
double dDistanceTot=0.0;
n=pts.GetCount ();
m_ctrlMapX.SetMapUnit(miUnitKilometer);
for (i=1; i<n;i++)
{
CMapXPoint pt1=pts.Item(i);
CMapXPoint pt2=pts.Item(i+1);
double d=m_ctrlMapX.Distance (pt1.GetX(),pt1.GetY(),pt2.GetX (),
pt2.GetY ());
dDistanceTot+=d;  
}
CString str;
str.Format ("总距离为:%f",dDistanceTot);
//在状态栏输出距离
((CMainFrame*)AfxGetMainWnd())->m_wndStatusBar.SetPaneText(0,str); //要先将m_wndStatusBar设为public
}
catch (COleDispatchException *e){
e->ReportError ();
e->Delete ();
}
catch (COleException *e){
e->ReportError ();
e->Delete ();
}
// }
}//if(Flags==miPolyToolInProgress) 
}


void CTestTView::OnTest() 
{
// TODO: Add your command handler code here
m_ctrlMapX.SetCurrentTool(DRAG_TOOL);
((CMainFrame*)AfxGetMainWnd())->m_wndStatusBar.SetPaneText(0,"gooooooooood");
}


void CTestTView::OnSize(UINT nType, int cx, int cy) 
{
CView::OnSize(nType, cx, cy);

// TODO: Add your message handler code here
if (cx!=0&&cy!=0)
{
m_ctrlMapX.MoveWindow(0,0,cx,cy,TRUE);
}
}


BOOL CTestTView::OnToolTest()
{
MessageBox("goood test");
return TRUE;
}

你可能感兴趣的:(serialization,command,工具,printing,distance,construction)