AE中繪制圖形元素的方法
Element元素對象是一個非常龐雜的對象集合,主要分為兩大部分:圖形元素(Graphic Element)和框架元素(Frame Element);
圖形元素包括GroupElement、MarkerElement、LineElement、TextElement、DataElement、PictureElement、和FillShapeElement等對象,它們都是作為圖形的形式而存在的。
IElement是所有圖形元素和框架元素都實現的接口,它可以確定元素的Geometry屬性,Element是一個抽象類,IElement和ILineElement、ITextElement並不是父子關系,后者沒有Geometry屬性。
要在視圖繪制圖形的方法:
1 采用MapControl的DrawShape()方法。
在MapControl控件中通過DrawShape方法繪制的圖形,在視圖中進行縮放操作之后就會消失,這是因為這些圖形本質上都是緩存的,僅僅是暫時存在的。如果要保存這些元素,就需要使用Mxd文件。
ISimpleFillSymbol pSymbol = new SimpleFillSymbolClass();
pSymbol.Color = pColor;
Object symbol = pSymbol;
IGeometry pGeo=axMapControl1.DrawShape(pGeo,ref symbol);
2 用IGraphicsContainer::AddElement把圖形元素添加到視圖並顯示
主要步驟:
1 產生一個新的元素對象;
2 確定元素顯示時使用的Symbol(符號)和Geometry(幾何對象);
3用IGraphicsContainer::AddElement把圖形元素添加到視圖並顯示
4 刷新視圖,讓添加的元素可以顯示出來。
添加IMarkerElement對象和ILineElement對象
以LineElement為例添加它到視圖中需要兩個接口:IElement和ILineElement,前者用於確定線元素的Geometry,后者確定Symbol。需要注意的什么類型的元素配對什么類型的Symbol,例如LineElement元素只能用修飾LineElement對象的符號,也只能用Line或者Polyline作為Geometry,MarkerElement也是一樣的,使用的是Marker類型的Symbol和點作為它的Geometry。具體例子:
IMap pMap = axMapControl1.Map;
IActiveView pActive = pMap as IActiveView;
ISimpleLineSymbol pLineSymbol = new SimpleLineSymbolClass(); //設置Symbol屬性
pLineSymbol.Color = GetRGB(0, 255, 0);
pLineSymbol.Width = 3;
pGeo = axMapControl1.TrackLine();
ILineElement pLineElement = new LineElementClass();
IElement pEle = pLineElement as IElement;
pLineElement.Symbol = pLineSymbol;
pEle.Geometry = pGeo;
IGraphicsContainer pContainer = pMap as IGraphicsContainer;
pContainer.AddElement(pEle, 0);
pActive.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
添加TextElement對象(用ITextElement接口定義了設置文字元素的屬性,例如Text(字符)等
IMap pMap = axMapControl1.Map;
IActiveView pActiveView = pMap as IActiveView;
ITextElement pText = new TextElementClass();
pText.Text = "從今天開始你就是我的了就像驢子一樣給你蓋個章";
IElement pEle = pText as IElement;
IPoint point = new PointClass();
point.PutCoords(e.mapX-30, e.mapY+10);
IFormattedTextSymbol pTextSymbol =new TextSymbolClass();
ICallout pCallout = new BalloonCalloutClass();
pCallout.AnchorPoint = point;
pTextSymbol.Background = pCallout as ITextBackground;
pText.Symbol = pTextSymbol;
pEle.Geometry = point;
IGraphicsContainer pgraphic = axMapControl1.Map as IGraphicsContainer;
pgraphic.AddElement(pEle,0);
axMapControl1.Refresh();
添加FillShapeElement對象
FillShapeElement是一個抽象類,它的子類有CircleElement、EllipseElement、PolygonElement和RectangleElement。這些對象的共同特點是它們的Geometry屬性都是一個封閉的二維圖形。
IFillShapeElement接口是所有FillShapeElement類都實現的接口,它定義了用於顯示圖形元素的Symbol屬性,這個Symbol屬性必須設置為IFillsymbol對象。
IMap pMap = axMapControl1.Map;
IActiveView pActiveView = pMap as IActiveView;
pGeo = axMapControl1.TrackPolygon();
ISimpleFillSymbol fillsymbol = new SimpleFillSymbolClass();
fillsymbol.Style = esriSimpleFillStyle.esriSFSDiagonalCross;
fillsymbol.Color = GetRGB(0, 255, 0);
IFillShapeElement pFillElement = new PolygonElementClass();
pFillElement.Symbol = fillsymbol;
IElement pEle = pFillElement as IElement;
pEle.Geometry = pGeo;
IGraphicsContainer pGraphics = pMap as IGraphicsContainer;
pGraphics.AddElement(pEle, 0);
畫出來的圖像:
PictureElement對象
往PageLayout控件添加任意范圍的圖片,即用到該對象,一般的話,我們可以向PageLayOut中插入一張位圖圖片,這種圖片其實就是一種圖形元素,即PictureElement對象。
PictureElement是一個抽象類,有兩個子類BmpPictureElement和EmfPictureElement,IPictureElement是兩個子類都實現的接口,使用它可以用於操作一個圖片元素。
public void ImportPictureFromFile ( string Name);獲取一張圖片;
public bool MaintainAspectRatio {get; set;}該屬性決定調整圖片尺寸時是否保持其長寬比例
具體實現往PageLayout控件添加圖片例子:
private void axPageLayoutControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IPageLayoutControlEvents_OnMouseDownEvent e)
{
IPageLayout pPageLayout = axPageLayoutControl1.PageLayout;
IGraphicsContainer pGraphic = pPageLayout as IGraphicsContainer;
IActiveView pActiveView = pPageLayout as IActiveView;
IGeometry pGeo = axPageLayoutControl1.TrackRectangle();
IPictureElement pPictureElement = new BmpPictureElementClass();
pPictureElement.ImportPictureFromFile(@"f:\TEST\test.bmp");
pPictureElement.MaintainAspectRatio = true;
IElement pEle = pPictureElement as IElement;
pEle.Geometry = pGeo;
pGraphic.AddElement(pEle, 0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null,null);
}
結果: