1. 程式人生 > >【ArcEngine標註和註記】- 使用TextElment繪製標註

【ArcEngine標註和註記】- 使用TextElment繪製標註

//使用TextElment繪製標註, fieldName為要繪製的屬性

        public static void AddLable(AxMapControl axMapControl, ILayer layer, string fieldName)
        {
            IRgbColor pColor = new RgbColorClass()
            {
                Red = 255,
                Blue = 0,
                Green = 0
            };
            IFontDisp pFont = new StdFont()
            {
                Name = "宋體",
                Size = 5
            } as IFontDisp;
            
            ITextSymbol pTextSymbol = new TextSymbolClass()
            {
                Color = pColor,
                Font = pFont,
                Size = 11
            };


            IGraphicsContainer pGraContainer = axMapControl.Map as IGraphicsContainer;


            //遍歷要標註的要素
            IFeatureLayer pFeaLayer = layer as IFeatureLayer;
            IFeatureClass pFeaClass = pFeaLayer.FeatureClass;
            IFeatureCursor pFeatCur = pFeaClass.Search(null, false);
            IFeature pFeature = pFeatCur.NextFeature();
            int index = pFeature.Fields.FindField(fieldName);//要標註的欄位的索引
            IEnvelope pEnv = null;
            ITextElement pTextElment = null;
            IElement pEle = null;
            while (pFeature != null)
            {
                //使用地理物件的中心作為標註的位置
                pEnv = pFeature.Extent;
                IPoint pPoint = new PointClass();
                pPoint.PutCoords(pEnv.XMin + pEnv.Width * 0.5, pEnv.YMin + pEnv.Height * 0.5);


                pTextElment = new TextElementClass()
                {
                    Symbol = pTextSymbol,
                    ScaleText = true,
                    Text = pFeature.get_Value(index).To\String()
                };
                pEle = pTextElment as IElement;
                pEle.Geometry = pPoint;
                //新增標註
                pGraContainer.AddElement(pEle, 0);
                pFeature = pFeatCur.NextFeature();
            }
            (axMapControl.Map as IActiveView).PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, axMapControl.Extent);
        }