最近正在做一个关于叶绿素反演的系统,期间遇到过很多问题,地图要素这一块亦然,虽然代码不难,出现的问题也比较好解决,但是将可以直接运行的代码给大家参考想必也不会徒增笑点。
还是老规矩先把相关的应用码出来
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
先来一个最简单--添加图例,代码如下,代码就不细讲了,我觉得该有的注释都码上去了。
public void InsertLegend()
{
//获取axPageLayoutControl1的图形容器
IGraphicsContainer graphicsContainer =
axPageLayoutControl1.GraphicsContainer;
//获取axPageLayoutControl1空间里面显示的地图图层
IMapFrame mapFrame =
(IMapFrame)graphicsContainer.FindFrame(axPageLayoutControl1.ActiveView.FocusMap);
if (mapFrame == null) return;
//创建图例
UID uID = new UIDClass();//创建UID作为该图例的唯一标识符,方便创建之后进行删除、移动等操作
uID.Value = "esriCarto.Legend";
IMapSurroundFrame mapSurroundFrame = mapFrame.CreateSurroundFrame(uID, null);
if (mapSurroundFrame == null) return;
if (mapSurroundFrame.MapSurround == null) return;
mapSurroundFrame.MapSurround.Name = "Legend";
IEnvelope envelope = new EnvelopeClass();
envelope.PutCoords(16, 5, 18, 7);//设置图例摆放位置(原点在axPageLayoutControl左下角)
IElement element = (IElement)mapSurroundFrame;
element.Geometry = envelope;
//将图例转化为几何要素添加到axPageLayoutControl1,并刷新页面显示
axPageLayoutControl1.AddElement(element, Type.Missing, Type.Missing,
"Legend", 0);
axPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null,
null);
}
比例尺和指北针的添加和图例添加过程基本一样,但是也有些许的差异,比例尺和指北针无法直接生成,需要先创建相应的实例,再转化为IMapSurround对象,才能转化为集合要素添加至axPageLayoutControl,代码如下:
//前三行是另外一种生成图层容器的方法,效果和添加图例的第一行代码效果一样,虽然代码更多,但是胜在稳定
IActiveView pActiveView = axPageLayoutControl1.PageLayout as IActiveView;
IMap pMap = pActiveView.FocusMap as IMap;
IGraphicsContainer pGraphicsContainer = pActiveView as IGraphicsContainer;
IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pMap) as IMapFrame;
IMapSurround pMapSurround;
INorthArrow pNorthArrow;
pNorthArrow = new MarkerNorthArrowClass();//创建指北针的实例
pMapSurround = pNorthArrow;
pMapSurround.Name = "NorthArrow";
//定义UID
UID uid = new UIDClass();
uid.Value = "esriCarto.MarkerNorthArrow";
//定义MapSurroundFrame对象
IMapSurroundFrame pMapSurroundFrame = pMapFrame.CreateSurroundFrame(uid, null);
pMapSurroundFrame.MapSurround = pMapSurround;
IElement pDeletElement = axPageLayoutControl1.FindElementByName("NorthArrow");//获取PageLayout中的图例元素
if (pDeletElement != null)
{
pGraphicsContainer.DeleteElement(pDeletElement); //如果已经存在指北针,删除已经存在的指北针
}
//定义Envelope设置Element摆放的位置
IEnvelope pEnvelope = new EnvelopeClass();
pEnvelope.PutCoords(16, 24, 21, 32);
IElement pElement = pMapSurroundFrame as IElement;
pElement.Geometry = pEnvelope;
pGraphicsContainer.AddElement(pElement, 0);
//刷新axPageLayoutControl1的内容
axPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
设置比例尺的代码因为要设置相应的字体、单位、样式等,所以代码较多,代码也较为简单,因此只介绍代码块的功能,代码如下所示:
IActiveView pActiveView = axPageLayoutControl1.PageLayout as IActiveView;
IMap pMap = pActiveView.FocusMap as IMap;
IGraphicsContainer pGraphicsContainer = pActiveView as IGraphicsContainer;
IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pMap) as IMapFrame;
IMapSurround pMapSurround;
//设置比例尺样式
IScaleBar pScaleBar = new ScaleLineClass();
pScaleBar.Units = esriUnits.esriKilometers;
pScaleBar.Divisions = 4;
pScaleBar.Subdivisions = 3;
pScaleBar.DivisionsBeforeZero = 0;
pScaleBar.UnitLabel = "km";
pScaleBar.LabelPosition = esriVertPosEnum.esriBelow;
pScaleBar.LabelGap = 3.6;
pScaleBar.LabelFrequency = esriScaleBarFrequency.esriScaleBarDivisionsAndFirstMidpoint;
pScaleBar.LabelPosition = esriVertPosEnum.esriBelow;
ITextSymbol pTextsymbol = new TextSymbolClass();
pTextsymbol.Size = 1;
stdole.StdFont pFont = new stdole.StdFont();
pFont.Size = 3;
pFont.Name = "Arial";
pTextsymbol.Font = pFont as stdole.IFontDisp;
pTextsymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHALeft;
pScaleBar.UnitLabelSymbol = pTextsymbol;
pScaleBar.LabelSymbol = pTextsymbol;
INumericFormat pNumericFormat = new NumericFormatClass();
pNumericFormat.AlignmentWidth = 0;
pNumericFormat.RoundingOption = esriRoundingOptionEnum.esriRoundNumberOfSignificantDigits;
pNumericFormat.RoundingValue = 0;
pNumericFormat.UseSeparator = true;
pNumericFormat.ShowPlusSign = false;
//定义UID
pMapSurround = pScaleBar;
pMapSurround.Name = "ScaleBar";
UID uid = new UIDClass();
uid.Value = "esriCarto.ScaleLine";
//定义MapSurroundFrame对象
IMapSurroundFrame pMapSurroundFrame = pMapFrame.CreateSurroundFrame(uid, null);
pMapSurroundFrame.MapSurround = pMapSurround;
//定义Envelope设置Element摆放的位置
IEnvelope pEnvelope = new EnvelopeClass();
pEnvelope.PutCoords(8, 5, 14, 7);
IElement pElement = pMapSurroundFrame as IElement;
pElement.Geometry = pEnvelope;
IElement pDeletElement = axPageLayoutControl1.FindElementByName("ScaleBar");//获取PageLayout中的比例尺元素
if (pDeletElement != null)
{
pGraphicsContainer.DeleteElement(pDeletElement); //如果已经存在比例尺,删除已经存在的比例尺
}
pGraphicsContainer.AddElement(pElement, 0);
//刷新axPageLayoutControl1的内容
axPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
本次分享到此结束,希望能对有需要的同志有所帮助。