ArcEngine 水平分子式标注

在ArcMap中,实现水平分子式标注的方法是在Label Expression中选中Advanced,输入如下VBA代码:

Function FindLabel ( [FieldName1] , [FieldName2]  )
  FindLabel = [FieldName1] &chr(13)&"———————————"&chr(13)& [FieldName2]
End Function

或者:

Function FindLabel ( [FieldName1] , [FieldName2]  )
  FindLabel = "<und>"&"      "&[FieldName1] &"     "&"</und>"& vbcrlf &[FieldName2]
End Function


注释:方法一中 chr(13)表示换行,间隔线表示分母线,&为VBScript连接字符,分母线的长短用间隔数表示,

                    也就是       字段1,换行,分母线,换行,字段2

        方法二中 <und>  </und>表示下划线,vbclf表示换行,分母线的长短用空格数表示,

                    也就是       加下划线的字段1,换行,字段2

       最后,分子和分母与分母线的间隔,可通过(TextSymbol)Symbol—>(Symbol Selector)Propertie—>(Editor)Formatting Text中的Leading调节。

在ArcEngine中,实现代码如下所示:

                IGeoFeatureLayer geoFeatureLayer = featureLayer as IGeoFeatureLayer;
               
                ITextSymbol textSymbol = new ESRI.ArcGIS.Display.TextSymbol();
                if (featureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
                {
                    continue;
                }
                else if (featureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
                {
                    textSymbol.Color = ((geoFeatureLayer.Renderer as ISimpleRenderer).Symbol as ILineSymbol).Color;
                }
                else if (featureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPoint)
                {
                    textSymbol.Color = ((geoFeatureLayer.Renderer as ISimpleRenderer).Symbol as IMarkerSymbol).Color;
                }
                textSymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHACenter;

                ILabelEngineLayerProperties2 pLelp = new LabelEngineLayerPropertiesClass();
                pLelp.IsExpressionSimple = false;
                pLelp.Symbol = textSymbol;
                IAnnotationExpressionEngine AnnExpEng = new AnnotationVBScriptEngineClass();
                pLelp.ExpressionParser = AnnExpEng;

                //方法一
                //pLelp.Expression = "Function FindLabel (" + "[编号]" + ", " + "[编号备注]" + ") \n " +
                //    "  FindLabel = " + "[编号]" + "&chr(13)" +"&\"——————————\""+ "&chr(13)" + "&[编号备注]" + "\n  End Function";
                //方法二
                pLelp.Expression = "Function FindLabel (" + "[编号]" + ", " + "[编号备注]" + ") \n " +
                   "  FindLabel = " + "\"<und>\"" + "&\"   \"" + "&[编号]" + "&\"   \"" + "&\"</und>\"" + " &vbcrlf " + "&[编号备注]" + "\n  End Function";

                IBasicOverposterLayerProperties pBasicOverposterLayerProperties = pLelp.BasicOverposterLayerProperties;                  
                pBasicOverposterLayerProperties.NumLabelsOption = esriBasicNumLabelsOption.esriOneLabelPerShape;
                 //水平标注
                ILineLabelPosition linelabelPosition;
                linelabelPosition = new LineLabelPositionClass();
                 linelabelPosition.Horizontal = true;
                 linelabelPosition.Above = false;
                 linelabelPosition.Parallel = false;
                 linelabelPosition.Perpendicular = false;
                 linelabelPosition.InLine = true;
                 linelabelPosition.Below = false;
                 linelabelPosition.OnTop = true;
                 pBasicOverposterLayerProperties.LineLabelPosition = linelabelPosition;
                
                pLelp.BasicOverposterLayerProperties = pBasicOverposterLayerProperties;
                pBasicOverposterLayerProperties.LabelWeight = esriBasicOverposterWeight.esriHighWeight;

                IAnnotateLayerProperties alps = (IAnnotateLayerProperties)pLelp;                
                alps.DisplayAnnotation = true;
                alps.FeatureLayer = geoFeatureLayer;
                alps.LabelWhichFeatures = esriLabelWhichFeatures.esriAllFeatures;
                
                geoFeatureLayer.AnnotationProperties.Clear();
                geoFeatureLayer.AnnotationProperties.Add(alps);
                geoFeatureLayer.DisplayAnnotation = true;              
               
                System.Runtime.InteropServices.Marshal.ReleaseComObject(textSymbol);

你可能感兴趣的:(ArcEngine)