轉自原文 ArcEngine標注和注記
標注和注記是ArcEngine中提供的兩種使用文字信息標注地圖要素的方式.其中標注是作為圖層的屬性存在的,可以動態創建,注記作為地理要素被存儲.需要注意的是Shp文件不支持注記. 繪制標注的方式有兩種.讓我們先看第一種:
1.使用TextElment繪制標注.
這種方法的原理就是把屬性表中的某個屬性創建TextElment對象,然后使用IGraphicsContainer 的AddElement方法添加標注.實例代碼:
//使用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); }
2.使用ArcEngine中的標注對象口.LabelEngineLayerProperties來標注要素
IGeoFeatureLayer中的AnnotationProperties是一個包含LabelEngineLayerProperties對象的標注集合.而 LabelEngineLayerProperties實現了:
IAnnotateProperties, //
IAnnotateLayerProperties, //可以控制標注的顯示比例尺,過濾條件等
ILabelEngineLayerProperties,
IAnnotateLayerTransformationProperties //控制標注的參考比例尺,單位,標注邊界和縮放比率等
等幾個主要的接口.LabelEngineLayerProperties可以操作標注要素的多個屬性和行為,如設置文本的標注位置,標注尺寸,設置腳本,文字符號等.該類實現了大量操作標注的屬性和方法,對於復雜的標注非常有用,而TextElment適合簡單的標注. ILabelEngineLayerProperties2是LabelEngineLayerPropertiesClass 的主接口.他的Expression和IsExpressionSimple用法如下:
IsExpressionSimple=true,Expression為簡單表達式,其形式為: "["+屬性字段名+"]"+其他,
IsExpressionSimple=true=false,Expression為復雜表達式,其內容也為一個字符串,但是一個完整的VBScript or JScript 函數或者表達式.
ExpressionParser屬性是一個Expression解析器,它支持更復雜的JS和Vbs代碼.
//添加標注,比TextElment功能更強大 public static void AddAnnotate(ILayer layer,string fieldName) { IGeoFeatureLayer pGeoLayer = layer as IGeoFeatureLayer; IAnnotateLayerPropertiesCollection IPALPColl = pGeoLayer.AnnotationProperties; IPALPColl.Clear(); IRgbColor pColor = GetColor(255, 0, 0, 255); IFontDisp pFont = new StdFont() { Name = "宋體", Bold = true } as IFontDisp; ITextSymbol pTextSymbol = new TextSymbolClass() { Color = pColor, Font = pFont, Size = 12 }; //用來控制標注和要素的相對位置關系 ILineLabelPosition pLineLpos = new LineLabelPositionClass() { Parallel = false, //修改標注的屬性 Perpendicular = true, InLine = true }; //用來控制標注沖突 ILineLabelPlacementPriorities pLinePlace = new LineLabelPlacementPrioritiesClass() { AboveStart = 5, //讓above 和start的優先級為5 BelowAfter = 4 }; //用來實現對ILineLabelPosition 和 ILineLabelPlacementPriorities以及更高級屬性的控制 IBasicOverposterLayerProperties pBOLP = new BasicOverposterLayerPropertiesClass() { FeatureType = esriBasicOverposterFeatureType.esriOverposterPolygon, LineLabelPlacementPriorities = pLinePlace, LineLabelPosition = pLineLpos }; //創建標注對象 ILabelEngineLayerProperties pLableEngine = new LabelEngineLayerPropertiesClass() { Symbol = pTextSymbol, BasicOverposterLayerProperties = pBOLP, IsExpressionSimple = true, Expression = "["+fieldName+"]" }; //設置標注的參考比例尺 IAnnotateLayerTransformationProperties pAnnoLyrPros = pLableEngine as IAnnotateLayerTransformationProperties; pAnnoLyrPros.ReferenceScale = 2500000; //設置標注可見的最大最小比例尺 IAnnotateLayerProperties pAnnoPros = pLableEngine as IAnnotateLayerProperties; pAnnoPros.AnnotationMaximumScale = 2500000; pAnnoPros.AnnotationMinimumScale = 25000000; //pAnnoPros.WhereClause屬性 設置過濾條件 IPALPColl.Add(pAnnoPros); pGeoLayer.DisplayAnnotation = true; }