//1.定義常量
#define MYTOOL_DISTANCE 100 //1~999
//2.創建工具
m_ctrlMap.CreateCustomTool(MYTOOL_DISTANCE,miToolTypePoly,miCrossCursor);
//3.響應事件
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 ();
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);
}
catch (COleDispatchException *e){
e->ReportError ();
e->Delete ();
}
catch (COleException *e){
e->ReportError ();
e->Delete ();
}
}
}//if(Flags==miPolyToolInProgress)
}
//4.調用工具
void CMapXSampleVIEw::OnMapToolDistancetool()
{
m_ctrlMapX.SetCurrentTool(MYTOOL_DISTANCE); //
}