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;
        }
  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值