ArcGIS二次开发基础教程(08):在MapControl上画图(添加元素)

ArcGIS二次开发基础教程(08):在MapControl上画图(添加元素)

添加临时元素

0. 点元素

//临时画图的元素将不会保存在地图中
//全局变量
IPoint pt;
IElement ele;
public void drawPoint(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
    pt = new PointClass();
    //获取单击位置地图坐标
    pt.PutCoords(e.mapX,e.mapY);
    //创建点要素
    ele = new MarkElementClass();
    //设置点要素的空间几何体为点
    ele.Geometry = pt as IGeometry;
    //图形容器
    IGraphicsContainer graphicsContainer = axMapControl1.Map as IGraphicsContainer;
    //添加元素
    graphicsConainer.AddElement(ele,0);
    axMapControl1.Refresh();
}
//菜单栏“点要素” 选项 添加事件开启画图状态
private void 点要素ToolStripMenuItem_Click(object sender, EventArgs e)
{
    axMapControl1.OnMouseDown += drawPoint;
}

1. 线元素

//全局变量
//点集
IPointCollection ptCollection;
IPoint linePt;
IElement ele;
public void drawLine(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
    linePt = new PointClass();
    linePt.PutCoords(e.mapX,e.mapY);
    ptCollection.AddPoint(linePt);
    //多段线要素
    ele = new PolylineElementClass();
    IPolyline polyline = new PolylineClass();
    //将点集转换为多段线
    polyline = ptCollection as IPolyline;
    ele.Geometry = polyline as IGeometry;
    //图形容器
    IGraphicsContainer graphicsContainer = axMapControl1.Map as IGraphicsContainer;
    //添加元素
    graphicsConainer.AddElement(ele,0);
    axMapControl1.Refresh();
}
 private void 线要素ToolStripMenuItem_Click(object sender, EventArgs e)
 {
     //每次开启画图状态都重新创建点集
     ptCollection = new PolylineClass();
     axMapControl1.OnMouseDown += drawLine;
 }

2. 面元素

//全局变量
IPoint polygonPt;
IElement ele;
IPointCollection ptCollection;
public void drawPolygon(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
    polygonPt = new PointClass();
    polygonPt.PutCoords(e.mapX,e.mapY);
    ptCollection.AddPoint(polyginPt);
    ele = new PolygonElementClass();
    IPolygon polygon = new PolygonClass();
    polygon = ptCollection as IPolygon;
    ele.Geometry = polygon as IGeometry;
    //图形容器
    IGraphicsContainer graphicsContainer = axMapControl1.Map as IGraphicsContainer;
    //添加元素
    graphicsConainer.AddElement(ele,0);
    axMapControl1.Refresh();
}
 private void 面要素ToolStripMenuItem_Click(object sender, EventArgs e)
{
	polygonCollect = new PolygonClass();
	axMapControl1.OnMouseDown += drawPolygon;
}

添加元素到要素中

0. 点元素

//此处添加的元素将保存在地图中
//全局变量
IPoint pt;
public void vectorPoint(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
    //获取图层及要素类
    IFeatureLayer featureLayer = GetLayerByName("图层名称");
    IFeatureClass featureClass = featureLayer.FeatrueClass;
    //创建点要素并赋值坐标信息
    pt = new PointClass();
    pt.PutCoords(e.mapX,e.mapY);
    //从要素类创建新要素
    IFeature feature = featureClass.CreateFeature();
    //将点元素设置为新建要素的Shape
    feature.Shape = pt as IGeomerty;
    //保存
    feature.Store();
    axMapControl1.Refresh();
}
//菜单栏“矢量点” 选项 添加事件开启画图状态
private void 矢量点ToolStripMenuItem_Click(object sender, EventArgs e)
{
	axMapControl1.OnMouseDown += vectorPoint;
}

1. 线元素

//全局变量
IPoint linePt;
IPointCollection ptCollection;
public void vectorLine(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
    //获取图层及要素类
    IFeatureLayer featureLayer = GetLayerByName("图层名称");
    IFeatureClass featureClass = featureLayer.FeatrueClass;
    linePt = new PointClass();
    linePt.PutCoords(e.mapX,e.mapY);
    ptCollection.AddPoint(linePt);
	IPolyline polyline = new PolylineClass();
    polyline = ptCollection as IPolyline;
    IFeature feature = featureClass.CreateFeature();
    feature.Shape = polyline as IGeometry;
    feature.Store();
    axMapControl1.Refresh();
}
private void 矢量线ToolStripMenuItem_Click(object sender, EventArgs e)
{
	pointCollect = new PolylineClass();
	axMapControl2.OnMouseDown += vectorLine;
}

2. 面元素

//全局要素
IPoint pt;
IPointCollection ptCollection;
public void vectorPolygon(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
    //获取图层及要素类
    IFeatureLayer featureLayer = GetLayerByName("图层名称");
    IFeatureClass featureClass = featureLayer.FeatrueClass;
    pt = new PointClass();
    pt.PutCoords(e.mapX,e.MapY);
    ptCollection.AddPoint(pt);
    IPolygon polygon = new PolygonClass();
    polygon = ptCollection as IPolygon;
    IFeature feature = featureClass.CreateFeature();
    feature.Shape = polygon as IGeometry;
    feature.Store();
    axMapControl1.Refresh();
}
private void 矢量面ToolStripMenuItem_Click(object sender, EventArgs e)
{
    polygonCollect = new PolygonClass();
	axMapControl2.OnMouseDown += vectorPolygon;
}

3. 文本元素

//实现为区县地图的每个区县添加区县名
IFeatureLayer layer = GetLayerByName("图层名称")
IFeatureCursor cursor = layer.FeatureClass.Search(null,true);
IFeature feature = cursor.NextFeature();
IGraphicsContainer graphicsContainer = axMapControl1.Map as IGraphicsContainer;
while(feature!=null)
{
    //获取区县名
	string str = feature.get_Value(feature.Fields.FindField("区县名")).ToString();
    //创建一个文本元素
    TextElement textElement = new TextElementClass();
    //字体
	IFontDisp font = new StdFontClass() as IFontDisp;
    font.Bold = true;
    font.Size = 10;
    font.Name = "宋体";
    //文本颜色
    IRgbColor color = new RgbColorClass();
    color.Red = 0;
    color.Green = 0;
    color.Blue = 0;
    ITextSymbol textSymbol = new TextSymbolClass();
    textSymbol.Font = font;
    textSymbol.Color = color;
    textElement.Symbol = textSymbol;
    textElement.Text = str;
    IElement element = textElement as IElement;
    //文本显示位置在区县要素中心
    IPoint point = new PointClass();
    IGeometry5 geo = feature.Shape as IGeometry5;
    point.X = geo.CentroidEx.X;
    point.Y = geo.CentroidEx.Y;
    element.Geometry = point as IGeometry;
    graphicsContainer.AddElement(element,0);
    feature = cursor.NextFeature();
}       axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,null,null);

历届GIS应用技能大赛开发题答案点这里,尚在不定期更新中

  • 4
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值