1. 程式人生 > >ArcGISEngine二次開發(5):新增向量要素

ArcGISEngine二次開發(5):新增向量要素

通過在新建的窗體中的RadioButton來實現在MapControl上畫出向量圖形,用到介面有ICommand,ITool。
用到的方法有TrackPolyLine(),TrackPolygon(),MapToPoint(),三個方法建立向量要素,通過IElement介面建立的物件的Geometry屬性接收IGeometry介面定義的,通過IElement介面的element型別物件,AddElement方法新增到MapControl的屬性GraphicsContainer中並且通過axMapControl物件重新整理MapControl視窗。
最後通過ISimpleMarkerSymbol介面設定物件的RGB值,將該物件給予IElement物件,實現向量要素的symbol更改。

建立步驟及程式碼如下

1、新建createfeature類
新增引用

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.ADF;

定義全域性變數

IMapControlDefault mapp;

例項窗體

createfeatureform fr = new createfeatureform();

實現ICommand介面,ITool介面,在如下位置新增對應程式碼

public void OnClick()
        {
            fr.Show();
        }
        public void OnCreate(object Hook)
        {
            mapp = Hook as IMapControlDefault;
        }
        public void OnMouseDown(int button, int shift, int x
, int y) { //GraphicsContainer新增資料須新增IElement型別的Object if(fr.radioButton1.Checked) { IGeometry point = mapp.ToMapPoint(x, y); IElement pelement = new MarkerElementClass(); pelement.Geometry = point; ISimpleMarkerSymbol psims = new SimpleMarkerSymbolClass(); psims.Style = esriSimpleMarkerStyle.esriSMSCircle; psims.Size = 5; IRgbColor prgb = new RgbColorClass(); prgb.Blue = 255; prgb.Red = 0; prgb.Green = 0; psims.Color = prgb; (pelement as IMarkerElement).Symbol = psims; mapp.ActiveView.GraphicsContainer.AddElement(pelement,0); } if (fr.radioButton2.Checked) { IGeometry line = mapp.TrackLine(); IElement pelement = new LineElementClass(); pelement.Geometry = line; mapp.ActiveView.GraphicsContainer.AddElement(pelement, 0); } if (fr.radioButton3.Checked) { IGeometry polygon = mapp.TrackPolygon(); IElement pelement = new PolygonElementClass(); pelement.Geometry = polygon; mapp.ActiveView.GraphicsContainer.AddElement(pelement, 0); } if (fr.radioButton4.Checked) { IGeometry ptext = mapp.ToMapPoint(x, y); IElement pelement = new TextElementClass(); (pelement as ITextElement).Text = fr.textBox1.Text; pelement.Geometry = ptext; mapp.ActiveView.GraphicsContainer.AddElement(pelement, 0); } mapp.ActiveView.Refresh(); }

2、新建窗體,佈局如圖所示:
這裡寫圖片描述

3、主視窗TooltripButton事件程式碼如下

 private void toolStripButton1_Click(object sender, EventArgs e)
        {
            ICommand createfeature = new createfeature();
            createfeature.OnCreate(axMapControl1.Object);
            createfeature.OnClick();
            ITool ptool = createfeature as ITool;
            axMapControl1.CurrentTool = ptool;
        }