在GIS开发中,地图上的位置信息是很重要的,很多时候都想随时知道某一点的具体地理位置信息。
下面的代码用来实现当鼠标在地图上移动时随时将经纬度显示在状态栏上。
/1在事件槽中加入事件/
ON_EVENT(CSampView, IDC_MAP, DISPID_MOUSEMOVE,OnMouseMoveInMap,VTS_I2 VTS_I2
VTS_XPOS_PIXELS VTS_YPOS_PIXELS)
2在头文件中加入函数定义///
BOOL OnMouseMoveInMap(short Button, short Shift, OLE_XPOS_PIXELS x, OLE_YPOS_PIXELS y);
///3加入实现函数
//鼠标在地图上移动时随时将经纬度显示在状态栏上
BOOL CSampView::OnMouseMoveInMap(short Button, short Shift, OLE_XPOS_PIXELS x,
OLE_YPOS_PIXELS y)
{
/*#if DO_TRACE
TRACE("OnMouseMoveInMap/r/n");
TRACE("Button = %d/r/n",Button);
TRACE("Shift = %d/r/n",Shift);
TRACE("X = %g/r/n",x);
TRACE("Y = %g/r/n",y);
#endif//DO_TRACE
*/
//if(m_ctrlMapX.GetCurrentTool() == miArrowTool)
//{
float fXn,fYn;
double dfXn,dfYn;
CPoint pt;
GetCursorPos(&pt);
fXn = (float)pt.x;
fYn = (float)pt.y;
m_ctrlMapX.ConvertCoord(&fXn,&fYn,&dfXn,&dfYn,miScreenToMap);
CString str;
CMainFrame* pMainFrame = (CMainFrame*)(AfxGetApp()->m_pMainWnd);
CStatusBar* pStatusBar = &(pMainFrame->m_wndStatusBar);
if(NULL != pStatusBar)
{
str.Format("longitude = %f latitude = %f",dfXn,dfYn);
// CClientDC dc(this);
// CSize sz=dc.GetTextExtent(str);
pStatusBar->SetPaneInfo(1,ID_ONMAP_CURRENT_LOCATION,SBPS_NORMAL,sz.cx);
pStatusBar->SetPaneText(1,str);
}
//}
return TRUE;
}