1、窗体加载事件:
/// <summary> /// 窗体加载 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void formLabel_Load(object sender, EventArgs e) { //字体大小 for (int i = 5; i < 73; i++) { this.cmbFontsSize.Items.Add(i.ToString()); } //字体样式 string[] strFontsStyle = new string[] {"宋体","Times New Roman","隶书","黑体","仿宋","新宋体","幼圆" }; for (int i = 0; i < strFontsStyle.Length; i++) { this.cmbFontsStyle.Items.Add(strFontsStyle[i]); } //添加图层 AddLayers(); }
/// <summary> /// 添加图层 /// </summary> private void AddLayers() { //首先清除 cmbLayers.Items.Clear(); //加载图层,默认选择第一个图层 int layerCount = _mapControl.LayerCount; for (int i = 0; i < layerCount; i++) { cmbLayers.Items.Add(_mapControl.get_Layer(i).Name); } if (layerCount != 0) { cmbLayers.SelectedIndex = 0; } }
/// <summary> /// 显示字段 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cmbLayers_SelectedIndexChanged(object sender, EventArgs e) { //清空字段列表 cmbFields.Items.Clear(); //改变字段列表 //获取选择的图层 IFeatureLayer featureLayer = _mapControl.get_Layer(cmbLayers.SelectedIndex) as IFeatureLayer; IFeatureClass featureClass = featureLayer.FeatureClass; int fieldsCount = featureClass.Fields.FieldCount; for (int i = 0; i < fieldsCount; i++) { IField field = featureClass.Fields.get_Field(i); //除去Shape字段 if (field.Name == "Shape") { continue; } cmbFields.Items.Add(field.Name); } }
/// <summary> /// 添加标注 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOk_Click(object sender, EventArgs e) { AddLabel addlabel = new AddLabel(); ILayer mLayer = _mapControl.get_Layer(cmbLayers.SelectedIndex) as ILayer; addlabel.AddAnnotate(mLayer, this.cmbFields.Text, cmbFontsStyle.Text, double.Parse(cmbFontsSize.Text), int.Parse(txtR.Text), int.Parse(txtG.Text), int.Parse(txtB.Text)); _mapControl.ActiveView.Refresh(); this.Close(); }在该按钮事件中调用了AddLabel这个类的AddAnnotate方法,在AddAnnotate方法中包含7个参数,标注的图层,标注字段,字体大小,字体样式以及字体颜色的RGB值,类AddLabel的代码如下:
using System; using System.Collections.Generic; using System.Linq; //ArcEngine引用 using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Display; using stdole; namespace MapDemo { public class AddLabel { private GetRgbColor getrgbcolor = new GetRgbColor(); //添加标注,比TextElment功能更强大 public void AddAnnotate(ILayer layer, string fieldName,string fontFamily,double fontSize,int colorR,int colorG,int colorB) { IGeoFeatureLayer pGeoLayer = layer as IGeoFeatureLayer; IAnnotateLayerPropertiesCollection IPALPColl = pGeoLayer.AnnotationProperties; IPALPColl.Clear(); IRgbColor pColor = getrgbcolor.GetRGBColor(colorR, colorG, colorB); IFontDisp pFont = new StdFont() { Name = fontFamily, Bold = true } as IFontDisp; ITextSymbol pTextSymbol = new TextSymbolClass() { Color = pColor, Font = pFont, Size = fontSize }; //用来控制标注和要素的相对位置关系 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; } } }