m_pAV=m_ctrlMap.GetActiveView(); m_ipMap=m_ctrlMap.GetMap(); //取得图层
ILayerPtr ipLayer ;
m_ipMap->get_Layer(0,&ipLayer); m_ipFeatLyr = ipLayer;
//设置编辑图层为唯一可选,此处设TOC中最上面的图层为编辑图层 hr=m_ipMap->ClearSelection(); if (FAILED(hr)) return FALSE; long nLyrCnt;
m_ipMap->get_LayerCount(&nLyrCnt); for(long i=0; i
m_ipMap->get_Layer(i,&ipLayer); IFeatureLayerPtr ipFeatLyr = ipLayer; if(ipFeatLyr != m_ipFeatLyr)
ipFeatLyr->put_Selectable(VARIANT_FALSE); }
m_ipFeatLyr->put_Selectable(VARIANT_TRUE); //创建显示反馈对象
m_pAV->get_ScreenDisplay(&ipDisp);
m_ipNEFdbk.CreateInstance(CLSID_NewEnvelopeFeedback); m_ipNEFdbk->putref_Display(ipDisp);
m_ipMPFdbk.CreateInstance(CLSID_MovePointFeedback); m_ipMPFdbk->putref_Display(ipDisp);
在View 类的 MouseDown 事件中添加如下代码
OnOnMouseDownControl(long button, long shift, long x, long y, double mapX, double mapY) {
if ((shift!=1) //未按shift键则返回 return; HRESULT hr;
IPointPtr ipCurPnt(CLSID_Point);
hr=ipCurPnt->PutCoords(mapX,mapY); if (FAILED(hr)) return ;
//若无选中要素,则为选择操作,否则是移动操作
if (NULL==m_ipCurFeat) {
m_ipNEFdbk->Start(ipCurPnt); m_bSel = TRUE; } else {
IGeometryPtr ipGeo;
hr=m_ipCurFeat->get_Shape(&ipGeo); IPointPtr ipPoint(ipGeo);
m_ipMPFdbk->Start(ipPoint,ipCurPnt); m_bMove = TRUE; } }
在View 类的 MouseMove 事件中添加如下代码 OnOnMouseMoveControl(long button, long shift, long x, long y, double mapX, double mapY) {
if ((shift!=1) //未按shift键则返回 return;
if(m_pAV==NULL || m_ipMap== NULL) return;
IPointPtr ipPoint(CLSID_Point); ipPoint->PutCoords(mapX,mapY);
//注意判断的顺序m_bSel要在(m_ipCurFeat==NULL)前,不然,程序崩溃,建议多写几个if
if( m_bSel && m_ipNEFdbk != NULL && m_ipCurFeat==NULL) m_ipNEFdbk->MoveTo(ipPoint);
else if(m_ipMPFdbk != NULL && m_bMove) m_ipMPFdbk->MoveTo(ipPoint); }
在View 类的 MouseUp 事件中添加如下代码: OnOnMouseUpControl(long button, long shift, long x, long y, double mapX, double mapY) {
if ((shift!=1) //未按shift键则返回 return;
if(m_pAV==NULL || m_ipMap== NULL) return;
if(m_bSel)/*m_ipCurFeat==NULL */ {
IEnvelopePtr ipEnv;
m_ipNEFdbk->Stop(&ipEnv);
m_ipMap->SelectByShape(ipEnv,NULL,VARIANT_TRUE); m_pAV->PartialRefresh(esriViewGeoSelection,NULL,NULL); IFeatureSelectionPtr ipFeatSel(m_ipFeatLyr); ISelectionSetPtr ipSelSet;
ipFeatSel->get_SelectionSet(&ipSelSet); //搜索光标可以获取用查询过滤确定的行
IQueryFilterPtr ipQF(CLSID_QueryFilter); ICursorPtr ipCursor;
ipSelSet->Search(ipQF,VARIANT_TRUE,&ipCursor); IRowPtr ipRow;
ipCursor->NextRow(&ipRow); m_ipCurFeat = ipRow; m_bSel = FALSE; }
else if(m_bMove) {
IPointPtr ipPoint;
m_ipMPFdbk->Stop(&ipPoint); IGeometryPtr ipGeo(ipPoint);
m_ipCurFeat->putref_Shape(ipGeo); m_ipCurFeat->Store(); m_ipCurFeat=NULL;
m_ipMap->ClearSelection(); m_pAV->Refresh(); m_bMove = FALSE; } }
在View 类的 DoubleClick 事件中添加如下代码:
OnOnDoubleClickControl(long button, long shift, long x, long y, double mapX, double mapY) {
if (NULL==m_ipCurFeat) {
AfxMessageBox(\ return; }
VARIANT var;
CString strFeatName;
CDlgHospital dlg; //这是一个对话框类,提供一个文本框实现要素名称的输入。 m_ipCurFeat->get_Value(1,&var);//取得要素的原值 strFeatName=var.bstrVal; dlg.m_sName=strFeatName; if (dlg.DoModal()!=IDOK) return ;
strFeatName=dlg.m_sName;
m_ipCurFeat->put_Value(1,(_variant_t)strFeatName); m_ipCurFeat->Store(); m_pAV->Refresh(); }
在View 类的 OnKeyDown 事件中添加如下代码: OnOnKeyDownControl(long keyCode, long shift) {
if(NULL==m_ipCurFeat) {
AfxMessageBox(\ return ; }
if(keyCode==VK_DELETE) {
if(AfxMessageBox(\删除选定点?\ return;
m_ipCurFeat->Delete(); m_ipCurFeat=NULL;
m_ipMap->ClearSelection(); m_pAV->Refresh(); } }