Arcgis Engine(ae)接口详解(8):临时元素(element)

                //主地图的地图(map)对象
                IMap map = null;
                IActiveView activeView = null;

                //IGraphicsContainer用于操作临时元素,可以通过map获取
                IGraphicsContainer gc = map as IGraphicsContainer;

                //删除所有临时元素
                gc.DeleteAllElements();
                activeView.Refresh();

                //画点的临时元素~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                IPoint point = new PointClass();
                point.PutCoords(100, 200);

                //首先定义点元素的样式
                //ISimpleMarkerSymbol意思是ISimple(简单的)Marker(点)Symbol(样式),MarkerSymbol处理simple的还有其他很多种,具体看IMarkerSymbol的实现类
                ISimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbolClass();
                //点颜色
                simpleMarkerSymbol.Color = SymbolHelper.CreateColorByRgb(255, 0, 0);
                //点大小
                simpleMarkerSymbol.Size = 5;
                //IMarkerElement代表点元素, new MarkerElementClass()是实例化点元素
                IMarkerElement markerElement = new MarkerElementClass();
                //设置点样式
                markerElement.Symbol = simpleMarkerSymbol;

                //IElement是所有元素(element)的顶层接口
                IElement element = markerElement as IElement;
                //设置元素几何对象,因为是画点所以赋值一个点
                //通过观察之后的添加线和面元素可发现,几何对象赋值都在IElement接口,而样式(symbol)赋值都在各种类型元素的接口
                element.Geometry = point;

                //添加元素到地图,最后刷新,完成添加
                gc.AddElement(element, 0);
                activeView.Refresh();


                //画线的临时元素~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                //线的生成不是重点,这里就随便了
                IPolyline polyline = null;

                //定义线样式
                //ISimpleLineSymbol意思是ISimple(简单的)Line(线)Symbol(样式)
                ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbolClass();
                //颜色
                simpleLineSymbol.Color = SymbolHelper.CreateColorByRgb(255, 0, 0);
                //线宽
                simpleLineSymbol.Width = 2;
                //ILineElement代表线元素, new LineElementClass()是实例化线元素
                ILineElement lineElement = new LineElementClass();
                //赋值线样式
                lineElement.Symbol = simpleLineSymbol;
                //IElement是所有元素(element)的顶层接口
                element = lineElement as IElement;
                //设置元素几何对象,因为是画线所以赋值一个线        
                element.Geometry = polyline;

                //添加元素到地图,最后刷新,完成添加
                gc.AddElement(element, 0);
                activeView.Refresh();


                //画面暂时略

                //以上是画临时元素的详细代码解析,在实际使用中,一般可以使用封装好的方法一行代码解决

                //画点
                DrawElementHelper.DrawPoint(map, point, 255, 0, 0, 3);

                //画线
                DrawElementHelper.DrawLine(map, polyline, 255, 0, 0, 3);

                //以上方法没有刷新,需另外调用刷新
                //PS:因此如果同时画多个元素,每次画都刷新会很卡
                activeView.Refresh();

上述代码调用的封装接口

        /// <summary>
        /// 通过rgb创建颜色
        /// </summary>
        /// <param name="r"></param>
        /// <param name="g"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static IColor CreateColorByRgb(int r, int g, int b)
        {
            IRgbColor color = new RgbColor();
            color.Red = r;
            color.Green = g;
            color.Blue = b;
            return color as IColor;
        }

        /// <summary>
        /// 画点
        /// 不带刷新
        /// </summary>
        /// <param name="map"></param>
        /// <param name="point"></param>
        /// <param name="r">颜色r</param>
        /// <param name="g">颜色g</param>
        /// <param name="b">颜色b</param>
        /// <param name="size">点大小</param>
        /// <returns></returns>
        public static IMarkerElement DrawPoint(IMap map, IPoint point, int r, int g, int b, double size)
        {
            ISimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbolClass();
            //点颜色
            simpleMarkerSymbol.Color = SymbolHelper.CreateColorByRgb(r, g, b);
            //点大小
            simpleMarkerSymbol.Size = size;
            //IMarkerElement代表点元素, new MarkerElementClass()是实例化点元素
            IMarkerElement markerElement = new MarkerElementClass();
            //设置点样式
            markerElement.Symbol = simpleMarkerSymbol;

            //IElement是所有元素(element)的顶层接口
            IElement element = markerElement as IElement;
            //设置元素几何对象,因为是画点所以赋值一个点
            //通过观察之后的添加线和面元素可发现,几何对象赋值都在IElement接口,而样式(symbol)赋值都在各种类型元素的接口
            element.Geometry = point;

            IGraphicsContainer gc = map as IGraphicsContainer;
            gc.AddElement(element, 0);

            return markerElement;
        }

        /// <summary>
        /// 画线
        /// 不带刷新
        /// </summary>
        /// <param name="map"></param>
        /// <param name="polyline"></param>
        /// <param name="r">颜色r</param>
        /// <param name="g">颜色g</param>
        /// <param name="b">颜色b</param>
        /// <param name="width">线宽</param>
        /// <returns></returns>
        public static ILineElement DrawLine(IMap map, IPolyline polyline, int r, int g, int b, double width)
        {
            ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbolClass();
            //颜色
            simpleLineSymbol.Color = SymbolHelper.CreateColorByRgb(r, g, b);
            //线宽
            simpleLineSymbol.Width = width;
            //ILineElement代表线元素, new LineElementClass()是实例化线元素
            ILineElement lineElement = new LineElementClass();
            //赋值线样式
            lineElement.Symbol = simpleLineSymbol;
            //IElement是所有元素(element)的顶层接口
            IElement element = lineElement as IElement;
            //设置元素几何对象,因为是画线所以赋值一个线        
            element.Geometry = polyline;

            IGraphicsContainer gc = map as IGraphicsContainer;
            gc.AddElement(element, 0);

            return lineElement;
        }

 

转载于:https://my.oschina.net/u/1251858/blog/1581374

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值