private void AddLabel(IFeatureLayer pLayer,string fieldname,ITextSymbol Symbol)
{ container.DeleteAllElements(); IFeatureClass pClass = pLayer.FeatureClass;
IFields pFields = pClass.Fields;
int index = pFields.FindField(fieldname); ITextElement pTextElement;
IEnvelope pEnv = null;
IElement pElement = null;
//pTextElement.ScaleText = true;
//pTextElement.Symbol = Symbol; IFeatureCursor pCursor = pClass.Search(null, false);
IFeature pFeature;
IPoint point;
pFeature = pCursor.NextFeature();
while (pFeature != null)
{
pEnv = pFeature.Extent;
point = new PointClass();
point.PutCoords((pEnv.XMin + pEnv.XMax) * 0.5, (pEnv.YMax + pEnv.YMin) * 0.5); pTextElement = new TextElementClass();
pTextElement.Symbol = Symbol;
pTextElement.ScaleText = true;
pTextElement.Text = pFeature.get_Value(index).ToString(); pElement = pTextElement as IElement;
pElement.Geometry = point; container.AddElement(pElement, 0);
pFeature = pCursor.NextFeature();
}
axMapControl1.Refresh();
}

以上为第一种方法,另一种方法(利用提供的接口):
 private void Label(IFeatureLayer pLayer, string fieldname, ITextSymbol Symbol)
{
IGeoFeatureLayer pGeoFeaturelayer = pLayer as IGeoFeatureLayer;
IAnnotateLayerPropertiesCollection annotLayercol = pGeoFeaturelayer.AnnotationProperties;
annotLayercol.Clear(); IBasicOverposterLayerProperties basicoverPost = new BasicOverposterLayerPropertiesClass();
basicoverPost.BufferRatio = 0.2;
switch (pLayer.FeatureClass.ShapeType)
{
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon:
basicoverPost.FeatureType = esriBasicOverposterFeatureType.esriOverposterPolygon;
break;
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline:
basicoverPost.FeatureType = esriBasicOverposterFeatureType.esriOverposterPolyline;
break;
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint:
basicoverPost.FeatureType = esriBasicOverposterFeatureType.esriOverposterPoint;
break; }
ILabelEngineLayerProperties pLabelEngine = new LabelEngineLayerPropertiesClass();
pLabelEngine.Expression = "[" + fieldname + "]";
pLabelEngine.Symbol = Symbol;
pLabelEngine.BasicOverposterLayerProperties = basicoverPost; annotLayercol.Add((IAnnotateLayerProperties)pLabelEngine);
pGeoFeaturelayer.DisplayAnnotation = true;
axMapControl1.Refresh();
}

最后调用:

  private void labelFeatureToolStripMenuItem_Click(object sender, EventArgs e)
{
//MessageBox.Show(PropertiesForm.FieldName);
IFeatureLayer layer = axMapControl1.CustomProperty as IFeatureLayer; IGeoFeatureLayer pGeoFeaturelayer = layer as IGeoFeatureLayer;
IAnnotateLayerPropertiesCollection annotLayercol = pGeoFeaturelayer.AnnotationProperties; string fieldname = PropertiesForm.FieldName;
ITextSymbol symbol = PropertiesForm.TextSymbol; if (show == false)
{ //AddLabel(layer, "XZQMC", symbol);
Label(layer, fieldname, symbol);
show = true;
MessageBox.Show("插入成功!");
} else
{
annotLayercol.Clear();
//container.DeleteAllElements();
show = false; axMapControl1.Refresh();
}
}

添加Labels的两种方法的更多相关文章

  1. Linux添加系统调用的两种方法

    前言 系统调用的基本原理 系统调用其实就是函数调用,只不过调用的是内核态的函数,但是我们知道,用户态是不能随意调用内核态的函数的,所以采用软中断的方式从用户态陷入到内核态.在内核中通过软中断0X80, ...

  2. unity3d为对象添加脚本的两种方法

    首先添加一个物体,然后新建一个C#脚本.接下去有两种方法把C#脚本与物体绑定. 1.在类声明上方添加如下代码: [AddComponentMenu("a/b")] 这句话表示在该物 ...

  3. 关于MySQL中添加数据的两种方法

    下面介绍两种执行SQL命令的方法,并作出相应地总结,第一种介绍一种常规用法,下面进行做简要地分析,首先我们需要执行打开数据库操作首先创建一个MySqlConnection对象,在其构造函数中传入一个连 ...

  4. view添加毛玻璃效果两种方法

    第一种方法: UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; UIVisualEffectV ...

  5. Cocos Creator 为Button添加事件的两种方法

    Button添加事件 Button 目前只支持 Click 事件,即当用户点击并释放 Button 时才会触发相应的回调函数.通过脚本代码添加回调方法一这种方法添加的事件回调和使用编辑器添加的事件回调 ...

  6. Cocos Creator EditBox(编辑框/输入框)添加事件的两种方法

    EditBox添加事件方法一这种方法添加的事件回调和使用编辑器添加的事件回调是一样的,通过代码添加, 你需要首先构造一个 cc.Component.EventHandler 对象,然后设置好对应的 t ...

  7. Cocos Creator scrollview添加事件的两种方法

    scrollview添加事件 方法一这种方法添加的事件回调和使用编辑器添加的事件回调是一样的,通过代码添加, 你需要首先构造一个 cc.Component.EventHandler 对象,然后设置好对 ...

  8. (转载)MySQl数据库-批量添加数据的两种方法

    方法一:使用excel表格 方法二:使用insert语句(FileWriter批量写入) 使用excel表格 1.打开数据表,按照表的字段在excel中添加数据.注意:表中字段名必须和excel中的名 ...

  9. WPF中RichTextBox中添加文字的两种方法

    RichTextBox控件不同于TextBox控件,后者可以直接通过其Text属性绑定或者在后台动态添加文字. (一)使用数据绑定 <RichTextBox FontSize="12& ...

随机推荐

  1. ES6初学习

    建议下一个chrome的插件Scratch.js[https://chrome.google.com/webstore/detail/alploljligeomonipppgaahpkenfnfkn] ...

  2. Python.Module.site

    site " This module is automatically imported during initialization. The automatic import can be ...

  3. 基类用的this指针

    结论:基类构造函数中的this指针指向的是派生类的对象 测试代码: #include <iostream> using namespace std; class father; fathe ...

  4. Web页面报错: Eval()、XPath() 和 Bind() 这类数据绑定方法只能在上下文中使用

    可以使用string.formt来避免出错. 如: <%# Convert.ToInt32(DataBinder.Eval(Container.DataItem, "Status&qu ...

  5. @helper函数使用方法

    这个函数方法,我也是通过别人博客看到的,感觉不错和大家一起学习分享一下. 1.自定义函数方法,只在同一个view视图文件里调用 Controller public ActionResult Index ...

  6. Debian 7(Linux) 安装SSH使用SecureCRT连接配置

    1 Debian 安装 ssh2 首先确保你的Debian或者linux安装ssh并开启ssh服务 Debian和ubuntu的安装方法一样,只要源OK的话,可以直接安装 apt-get instal ...

  7. wpf 空白汉字占位符

    <TextBlock xml:space="preserve" Text="主  编" />   表示一个汉字占位符

  8. MySQL提示符含义

    http://www.splaybow.com/post/mysql-prompt-introduce.html mysql> 准备好接受新命令. 说明:正常等待输入的提示符. -> 等待 ...

  9. CSS position绝对定位absolute relative

    常常使用position用于层的绝对定位,比如我们让一个层位于一个层内具体什么位置,为即可使用position:absolute和position:relative实现. 一.position语法与结 ...

  10. Mvc分页扩展类 分页代码 带跳转

    public static class HtmlPager { public static MvcHtmlString MvcPager(this HtmlHelper html, string cu ...