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;
}