IFeatureCursor pFeatCursor;
IFeatureClass pfeatClass;
IFeatureLayer pfeatlayer;
IFeature pfeature;
IEnvelope pEnv;
IRgbColor pColor;
stdole.Font pFont;
IPoint pt;
IElement pEle;
ITextElement pTextEle;
IGraphicsContainer pGraphisContainer;
ITextSymbol pTextSymbol;
IActiveView pActiveView;
pfeatlayer = (IFeatureLayer)axMapControl1.get_Layer(0);
pfeatClass = pfeatlayer.FeatureClass;
pFeatCursor = pfeatClass.Search(null, false);
pfeature = pFeatCursor.NextFeature();
int fieldIdx;
int i;
//得到cname字段的Index
fieldIdx = pfeature.Fields.FindField("CNTRY_NAME");
//创建颜色
pColor = new RgbColorClass();
pColor.Blue = 255;
//创建字体
pFont = new stdole.StdFontClass();
pFont.Name = "楷体";
//创建标注样式
pTextSymbol = new TextSymbolClass();
pTextSymbol.Color = pColor;
pTextSymbol.Font = pFont as stdole.IFontDisp;
pTextSymbol.Size = 10;
pGraphisContainer = axMapControl1.Map as IGraphicsContainer;
pActiveView = axMapControl1.Map as IActiveView;
//使用循环标注每个要素
while (pfeature != null)
{
pEnv = pfeature.Extent;
pt = new PointClass();
//使用地理对象的包络线中心作为标注位置
pt.PutCoords(pEnv.XMin + pEnv.Width * 0.5, pEnv.YMin + pEnv.Height * 0.5);
//创建文本标注
pTextEle = new TextElementClass();
pTextEle.Symbol = pTextSymbol;
pTextEle.Text = pfeature.get_Value(fieldIdx).ToString();
//设置标注位置
pEle = pTextEle as IElement;
pEle.Geometry = pt;
//绘制标注
pGraphisContainer.AddElement(pEle, 0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, pEnv);
pt = null;
pTextEle = null;
pfeature = pFeatCursor.NextFeature();
}
string CurrentPath = Directory.GetCurrentDirectory();
Directory.SetCurrentDirectory(CurrentPath + "\\..\\..\\..\\..\\..\\data\\WORLD");
string FilePath = Directory.GetCurrentDirectory();
axMapControl1.AddShapeFile(FilePath, "country.shp");
axMapControl1.AddShapeFile(FilePath, "rivers.shp");
axMapControl1.AddShapeFile(FilePath, "cities.shp");
IMap pMap;
int i;
pMap = axMapControl1.Map;
for (i = 0; i <= pMap.LayerCount - 1; i++)
{
axMapControl2.Map.AddLayer(pMap.get_Layer(i));
}
}
private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
if (e.button == 1)
{ axMapControl1.Extent = axMapControl1.TrackRectangle(); }
else if (e.button == 2)
{ axMapControl1.Pan(); }
}
private void axMapControl1_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
{
IEnvelope pEnv;
pEnv =(IEnvelope)e.newEnvelope;
IGraphicsContainer pGraphicsContainer;
IActiveView pActiveView;
pGraphicsContainer = axMapControl2.Map as IGraphicsContainer;
pActiveView = pGraphicsContainer as IActiveView;
//在绘制新的矩形框前,清除Map对象中的所有图形元素
pGraphicsContainer.DeleteAllElements();
//定义一个矩形元素对象
IRectangleElement pRectangelEle;
pRectangelEle = new RectangleElementClass();
IElement pEle;
pEle = pRectangelEle as IElement;
pEle.Geometry = pEnv;
IRgbColor pColor;
pColor = new RgbColorClass();
pColor.Red = 255;
pColor.Transparency = 255;
//产生一个线符号对象
ILineSymbol pOutline;
pOutline = new SimpleLineSymbolClass();
//设置线符号的属性
pOutline.Color = pColor;
pOutline.Width = 2;
//设置颜色属性
pColor = new RgbColorClass();
pColor.Transparency = 0;
//设置填充符号的属性
IFillSymbol pFillSymbol;
pFillSymbol = new SimpleFillSymbolClass();
pFillSymbol.Color = pColor;
pFillSymbol.Outline = pOutline;
IFillShapeElement pFillshapeEle;
pFillshapeEle = pEle as IFillShapeElement;
pFillshapeEle.Symbol = pFillSymbol;
pEle = pFillshapeEle as IElement;
pGraphicsContainer.AddElement(pEle,0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
private void axMapControl2_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
IPoint point = new PointClass();
point.PutCoords(e.mapX, e.mapY);
axMapControl1.CenterAt(point);
}
}
AE二次开发使用TextElement绘制标注
最新推荐文章于 2021-11-08 09:41:53 发布
该代码段展示了如何使用ArcGIS控件进行地图操作,包括加载图层、标注要素以及响应鼠标事件。首先,从地图控件获取第一层作为特征层,然后遍历特征并创建标注,标注内容取自要素的'CNTRY_NAME'字段。同时,实现了鼠标左键拖动更新地图范围和右键平移功能。最后,将地图控件1的图层添加到地图控件2。
摘要由CSDN通过智能技术生成