图层标注

private void BiaoZhu(int LayIndex, string Field)//图层标注
        {
            IFeatureCursor pFeatCursor;
            IFeatureClass pfeatClass;
            IFeatureLayer pfeatlayer;
            IFeature pfeature;
            IEnvelope pEnv;
            IRgbColor pColor;
            stdole.Font pFont;
            IPoint pt;
            ITextSymbol pTextSymbol;
            IElement pEle;
            ITextElement pTextEle;
            IActiveView pActiveView;

            pfeatlayer = (IFeatureLayer)axMapControl1.get_Layer(LayIndex );
            pfeatClass = pfeatlayer.FeatureClass;
            pFeatCursor = pfeatClass.Search(null, false);//获取要素类的游标对象
            pfeature = pFeatCursor.NextFeature();//获取第一个标注对象
            int fieldIdx;
            //int i;
            //得到name字段的Index
            fieldIdx = pfeature.Fields.FindField(Field );
            //设置颜色
            pColor = new RgbColorClass();
            if (Field == "NAME" && LayIndex == 1)
            {
                pColor.Red = 255;
            }
            else
            {
                pColor.Green  = 255;
            }
            //创建字体
            pFont = new stdole.StdFontClass();
            pFont.Name = "楷体";
            //创建标注样式
            pTextSymbol = new TextSymbolClass();
            pTextSymbol.Color = pColor;
            pTextSymbol.Font = pFont as stdole.IFontDisp;
            pTextSymbol.Size = 25;
            pGraphicsContainer = axMapControl1.Map as IGraphicsContainer;
            pActiveView = axMapControl1.Map as IActiveView;
            //循环遍历标注每一个要素
            while (pfeature != null)
            {
                pEnv = pfeature.Extent;
                pt = new PointClass();
                //使用地理对象的包络线中心作为标注位置
                pt.PutCoords(pEnv.XMin + pEnv.Width * 0.5, pEnv.YMin + pEnv.Height * 0.5);
                //创建标注文本
                pTextEle = new TextElementClass();
                pTextEle.Symbol = pTextSymbol;
                pTextEle.Text = pfeature.get_Value(fieldIdx).ToString();
                //设置标注位置
                pEle = pTextEle as IElement;
                pEle.Geometry = pt;
                //绘制标注
                pGraphicsContainer.AddElement(pEle, 0);
                pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, pEnv);
                pt = null;
                pTextEle = null;
                pfeature = pFeatCursor.NextFeature();
            }
        }

 

posted @ 2016-08-04 23:35  Manson123  阅读(616)  评论(0编辑  收藏  举报