通过在新建的窗体中的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;
}