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();

上述代碼調用的封裝接口code

/// <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;
        }
相關文章
相關標籤/搜索