ArcEngine 一些實現程式碼
---------------------------------------------------------------------------------------------------------
●·● 目錄:
A1 …………
實現:滑鼠滑過顯示要素 tip
A2 …………
實現:通過滑鼠選擇要素並高亮顯示(ISelectionEnvironment)
A3 …………
實現:只顯示篩選的要素(IFeatureLayerDefinition)
A4 …………
實現:高亮顯示篩選的要素(IFeatureSelection)
A5 …………
實現:類似 ArcMap 中 Identify 工具的效果(IIdentify、IArray、IIdentifyObj
A6 ………… 實現:在 MapControl 上繪製幾何圖形 實現:在 MapControl 上繪製幾何圖形(IGraphicsContainer,幾何:圓)
A7 ………… 實現:在 MapControl 自由旋轉地圖(IScreenDisplay [RotateMoveTo]) 實現:在 MapControl 中滑鼠與地圖反向移動(IScreenDisplay [PanMoveTo])
A8 …………
實現:彈出顏色選擇器(IColorPalette、IColorSelector、IColorBrowser)
A9 ………… 實現:Symbol 物件(ISimpleMarkerSymbol、Arrow、Character、Picture)
G1 ………… 實現:Symbol 物件
G2 …………
實現:顯示圖層的屬性視窗
G3 …………
實現:PageLayoutControl 的樣式設定(IBorder、IBackground、IShadow、IMapGrid)
G4 …………
實現:刪除shapefile檔案中的重複資料
G5 …………
實現:MapControl 與 PageLayoutControl 的互動
G6 …………
G7 …………
實現:ArcGIS Engine 實現鷹眼 & 分析(IGraphicsContainer、IFillShapeElement)
G8 …………
實現:獨立視窗的鷹眼顯示(IHookHelper)
G9 …………
實現:自定義工具視窗(ICustomizeDialog、ICustomizeDialogEvents)
U1 …………
實現:Map 與 PageLayout 切換後工具不變
U2 …………
實現:在窗體中顯示漸變顏色 & 根據名稱獲取控制元件(IAlgorithmicColorRamp、IEnumColor)
U3 …………
實現:獲取地圖是否處於編輯狀態(IDataset、IWorkspaceEdit)
U4 …………
實現:為某一要素新增欄位內容(IFeature)
U5 …………
實現:獲取地圖是否處於編輯狀態
U6 …………
實現:獲取地圖是否處於編輯狀態
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣
第A1個 ╠══════════════════════════════════════════════════╣
╚════════╝
●·● 實現:滑鼠滑過顯示要素 tip:
對於這個有兩個方法:
第一種:通過將 axmapcontrol 自帶的 ShowMapTips 屬性設定為 true 來實現。
第二種:通過 .NET 自帶的控制元件 ToolTip 來實現!
第一種程式碼:
private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e) { axMapControl1.ShowMapTips = true; IFeatureLayer pFeatureLayer = axMapControl1.Map.get_Layer(0) as IFeatureLayer; pFeatureLayer.DisplayField = "Name"; pFeatureLayer.ShowTips = true; }
第二種程式碼:
private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e) { IFeatureLayer pFeatureLayer = axMapControl1.Map.get_Layer(0) as IFeatureLayer; pFeatureLayer.DisplayField = "Name"; pFeatureLayer.ShowTips = true; string pTip; pTip = pFeatureLayer.get_TipText(e.mapX, e.mapY, axMapControl1.ActiveView.FullExtent.Width / 10000); if (pTip != null) { toolTip1.SetToolTip(axMapControl1, "名稱:" + pTip); } else //當 ToolTip 空間顯示的內容為 null 的時候,就不會顯示了!相當於隱藏了! { toolTip1.SetToolTip(axMapControl1, ""); } }
以上兩種方法都可以實現顯示標註,但是第二種效果更好一點~!
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣
第A2個 ╠══════════════════════════════════════════════════╣
╚════════╝
●·● 實現:通過滑鼠選擇要素並高亮顯示:
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
通過 IMap 介面的 SelectByShape 方法來實現!同時可以修改高亮顯示的顏色!
private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) { axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault; IMap pMap = axMapControl1.Map; IGeometry pGeometry = axMapControl1.TrackRectangle(); //獲取框選幾何 ISelectionEnvironment pSelectionEnv = new SelectionEnvironment(); //新建選擇環境 IRgbColor pColor = new RgbColor(); pColor.Red = 255; pSelectionEnv.DefaultColor = pColor; //設定高亮顯示的顏色! pMap.SelectByShape(pGeometry, pSelectionEnv, false); //選擇圖形! axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null); }
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣
第A3個 ╠══════════════════════════════════════════════════╣
╚════════╝
●·● 實現:只顯示篩選的要素:
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
1. 通過 IFeatureLayerDefinition 介面的 DefinitionExpression 屬性可以實現!
IFeatureLayer pFeatureLayer = axMapControl1.Map.get_Layer(0) as IFeatureLayer; IFeatureLayerDefinition pFeatLyrDef = pFeatureLayer as IFeatureLayerDefinition; //新建 IFeatureLayerDefinition 介面例項 pFeatLyrDef.DefinitionExpression = "Area > 20"; //定義篩選條件 axMapControl1.ActiveView.Refresh(); //重新整理
這樣便只顯示符合要求的部分了!即面積大於20的要素!
2. 通過 IFeatureLayerDefinition 介面的 CreatSelectionLayer 方法可以通過篩選建立新圖層!
IFeatureLayer pFeatureLayer = axMapControl1.Map.get_Layer(0) as IFeatureLayer; IFeatureLayerDefinition pFeatLyrDef = pFeatureLayer as IFeatureLayerDefinition; pFeatLyrDef.DefinitionExpression = "Area > 20"; axMapControl1.ActiveView.Refresh(); //重新定義的圖層 IQueryFilter pQueryFilter = new QueryFilter(); pQueryFilter.WhereClause = "POP > 10"; IFeatureSelection pFeatSel = pFeatureLayer as IFeatureSelection; pFeatSel.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false); axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null); //在新定義圖層的基礎上進行的查詢 IFeatureLayer pNewFeat = pFeatLyrDef.CreateSelectionLayer("New Layer", true, null, null); //新建的圖層包括上面兩者的交集部分! pFeatSel.Clear(); axMapControl1.Map.AddLayer(pNewFeat); MessageBox.Show(axMapControl1.Map.LayerCount.ToString());
首先是建立一個虛擬的新圖層,然後在此新圖層的基礎上進行篩選,然後從而生成新的圖層!
參考:http://blog.csdn.net/qinyilang/article/details/6575539
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣
第A4個 ╠══════════════════════════════════════════════════╣
╚════════╝
●·● 實現:高亮顯示篩選的要素:
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
通過 IFeatureSelection 介面的 SelectFeatures 方法可以實現!
IFeatureLayer pFeatureLayer = axMapControl1.Map.get_Layer(0) as IFeatureLayer;
IQueryFilter pQueryFilter = new QueryFilter(); //建立查詢 pQueryFilter.WhereClause = "POP > 10"; IFeatureSelection pFeatSel = pFeatureLayer as IFeatureSelection; //新建 IFeatureSelection 介面例項 pFeatSel.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false); //實現方法,選擇篩選的部分! axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣
第A5個 ╠══════════════════════════════════════════════════╣
╚════════╝
●·● 實現:類似 ArcMap 中 Identify 工具的效果:
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
主要實現點選查詢並閃爍顯示,並把查詢要素的資訊通過DataGridView顯示出來,主要用到的介面:IIdentity、IArray、IIdentifyObj
IIdentify pIdentify = axMapControl1.Map.get_Layer(0) as IIdentify; //通過圖層獲取 IIdentify 例項 IPoint pPoint = new ESRI.ArcGIS.Geometry.Point(); //新建點來選擇 IArray pIDArray; IIdentifyObj pIdObj; pPoint.PutCoords(e.mapX, e.mapY); //定義點 pIDArray = pIdentify.Identify(pPoint); //通過點獲取陣列,用點一般只能選擇一個元素 if (pIDArray != null) { pIdObj = pIDArray.get_Element(0) as IIdentifyObj; //取得要素 pIdObj.Flash(axMapControl1.ActiveView.ScreenDisplay); //閃爍效果 MessageBox.Show("Layer: " + pIdObj.Layer.Name + "\n" + "Feature: " + pIdObj.Name); //輸出資訊 } else { MessageBox.Show("Nothing!"); }
效果:
框選實現如下所示:
IIdentify pIdentify = axMapControl1.Map.get_Layer(0) as IIdentify; IGeometry pGeo = axMapControl1.TrackRectangle() as IGeometry; IArray pIDArray; IIdentifyObj pIdObj; pIDArray = pIdentify.Identify(pGeo); if (pIDArray != null) { string str = "\n"; string lyrName = ""; for (int i = 0; i < pIDArray.Count;i++ ) { pIdObj = pIDArray.get_Element(i) as IIdentifyObj; pIdObj.Flash(axMapControl1.ActiveView.ScreenDisplay); str += pIdObj.Name + "\n"; lyrName = pIdObj.Layer.Name; } MessageBox.Show("Layer: " + lyrName + "\n" + "Feature: " + str); } else { MessageBox.Show("Nothing!"); }
效果如下:
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣
第A6個 ╠══════════════════════════════════════════════════╣
╚════════╝
●·● 實現:在 MapControl 上繪製幾何圖形:
可以直接使用 axMapControl1.DrawShape 方法來實現~!
ISimpleLineSymbol pLineSym = new SimpleLineSymbol(); IRgbColor pColor = new RgbColor(); pColor.Red = 11; pColor.Green = 120; pColor.Blue = 233; pLineSym.Color = pColor; pLineSym.Style = esriSimpleLineStyle.esriSLSSolid; pLineSym.Width = 2; IPolyline pLine = axMapControl1.TrackLine() as IPolyline; object symbol = pLineSym as object; axMapControl1.DrawShape(pLine, ref symbol);
也可以通過 IScreenDisplay 介面的方法來實現!~
ISimpleLineSymbol pLineSym = new SimpleLineSymbol(); IRgbColor pColor = new RgbColor(); pColor.Red = 11; pColor.Green = 120; pColor.Blue = 233; pLineSym.Color = pColor; pLineSym.Style = esriSimpleLineStyle.esriSLSSolid; pLineSym.Width = 2; IPolyline pLine = axMapControl1.TrackLine() as IPolyline; IScreenDisplay pScreenDisplay = axMapControl1.ActiveView.ScreenDisplay; pScreenDisplay.StartDrawing(pScreenDisplay.hDC, 1); pScreenDisplay.SetSymbol(pLineSym as ISymbol); pScreenDisplay.DrawPolyline(pLine); pScreenDisplay.FinishDrawing();
通過比較,只是後面實現的部分不同,前面都是相同的!
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣
第A6A個 ╠══════════════════════════════════════════════════╣
╚════════╝
●·● 實現:在 MapControl 上繪製幾何圖形(IGraphicsContainer,幾何:圓):
普通的圖形,可以直接向下面一樣實現!
m_ActiveView = m_hookHelper.ActiveView; m_Map = m_hookHelper.FocusMap; IScreenDisplay pScreenDisplay = m_ActiveView.ScreenDisplay; IRubberBand pRubberPolygon = new RubberPolygonClass(); ISimpleFillSymbol pFillSymbol = new SimpleFillSymbolClass(); pFillSymbol.Color = getRGB(255, 255, 0); IPolygon pPolygon = pRubberPolygon.TrackNew(pScreenDisplay, (ISymbol)pFillSymbol) as IPolygon; pFillSymbol.Style = esriSimpleFillStyle.esriSFSDiagonalCross; pFillSymbol.Color = getRGB(0, 255, 255); IFillShapeElement pPolygonEle = new PolygonElementClass(); pPolygonEle.Symbol = pFillSymbol; IElement pEle = pPolygonEle as IElement; pEle.Geometry = pPolygon; IGraphicsContainer pGraphicsContainer = m_Map as IGraphicsContainer; pGraphicsContainer.AddElement(pEle, 0); m_ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
畫圓比較特殊,因為沒有圓這個現成的幾何體,因此要轉換,如下所示:
m_ActiveView = m_hookHelper.ActiveView; m_Map = m_hookHelper.FocusMap; IScreenDisplay pScreenDisplay = m_ActiveView.ScreenDisplay; IRubberBand pRubberCircle = new RubberCircleClass(); ISimpleFillSymbol pFillSymbol = new SimpleFillSymbolClass(); pFillSymbol.Color = getRGB(255, 255, 0); IGeometry pCircle = pRubberCircle.TrackNew(pScreenDisplay, (ISymbol)pFillSymbol) as IGeometry; IPolygon pPolygon = new PolygonClass(); //空的多邊形 ISegmentCollection pSegmentCollection = pPolygon as ISegmentCollection; //段集合 ISegment pSegment = pCircle as ISegment; //將圓賦值給段 object missing = Type.Missing; //顯示預設值 pSegmentCollection.AddSegment(pSegment, ref missing, ref missing); //給空多邊形加入圓 pFillSymbol.Style = esriSimpleFillStyle.esriSFSDiagonalCross; pFillSymbol.Color = getRGB(0, 255, 255); IFillShapeElement pPolygonEle = new PolygonElementClass(); pPolygonEle.Symbol = pFillSymbol; IElement pEle = pPolygonEle as IElement; pEle.Geometry = pPolygon; IGraphicsContainer pGraphicsContainer = m_Map as IGraphicsContainer; pGraphicsContainer.AddElement(pEle, 0); m_ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣
第A7個 ╠══════════════════════════════════════════════════╣
╚════════╝
●·● 實現:在 MapControl 自由旋轉地圖:
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
通過 IScreenDisplay 介面來實現!
//滑鼠按下! private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) { IPoint pPoint = new PointClass(); pPoint.PutCoords(e.mapX, e.mapY); IPoint pCentrePoint = new PointClass(); pCentrePoint.PutCoords(axMapControl1.Extent.XMin + axMapControl1.ActiveView.Extent.Width / 2, axMapControl1.Extent.YMax - axMapControl1.ActiveView.Extent.Height / 2); //獲取影象的中心位置 axMapControl1.ActiveView.ScreenDisplay.RotateStart(pPoint, pCentrePoint); //開始旋轉}
//滑鼠移動! private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e) { IPoint pPoint = new PointClass(); pPoint.PutCoords(e.mapX, e.mapY); axMapControl1.ActiveView.ScreenDisplay.RotateMoveTo(pPoint); //旋轉到滑鼠的位置 axMapControl1.ActiveView.ScreenDisplay.RotateTimer(); //可以忽略}
//滑鼠擡起! private void axMapControl1_OnMouseUp(object sender, IMapControlEvents2_OnMouseUpEvent e) { double dRotationAngle = axMapControl1.ActiveView.ScreenDisplay.RotateStop(); //獲取旋轉的角度 axMapControl1.Rotation = dRotationAngle; //賦值給 axMapControl1.Rotation,這下真的旋轉了! axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); //重新整理!}
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣
第A7A個 ╠══════════════════════════════════════════════════╣
╚════════╝
●·● 實現:在 MapControl 中滑鼠與地圖反向移動:
double startMapX = 0; double startMapY = 0; IScreenDisplay pScreenDisplay; private void Form1_Load(object sender, EventArgs e) //窗體載入資訊 { pScreenDisplay = axMapControl1.ActiveView.ScreenDisplay; } private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) //滑鼠按下的時候觸發 { IPoint pPoint = new PointClass(); pPoint.PutCoords(e.mapX, e.mapY); pScreenDisplay.PanStart(pPoint); startMapY = e.mapY; startMapX = e.mapX; } private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e) //滑鼠移動的時候觸發 { IPoint pPoint = new PointClass(); pPoint.PutCoords(startMapX * 2 - e.mapX, startMapY * 2 - e.mapY); //獲取當前點關於起始點的對稱點 pScreenDisplay.PanMoveTo(pPoint); } private void axMapControl1_OnMouseUp(object sender, IMapControlEvents2_OnMouseUpEvent e) //滑鼠鬆開的時候觸發 { pScreenDisplay.PanStop(); }
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣
第A8個 ╠══════════════════════════════════════════════════╣
╚════════╝
●·● 實現:彈出顏色選擇器:
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
1. ColorPalette:
private void button1_Click(object sender, EventArgs e) { IColor pColor = new RgbColor(); pColor.RGB = 255; tagRECT pTag = new tagRECT(); pTag.left = this.Left + button1.Left + button1.Width; pTag.bottom = this.Top + button1.Top + button1.Height; IColorPalette pColorPalette = new ColorPalette(); pColorPalette.TrackPopupMenu(ref pTag, pColor, false, 0); pColor = pColorPalette.Color; }
效果:
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣
第A8A個 ╠══════════════════════════════════════════════════╣
╚════════╝
●·● 實現:獲取控制元件的螢幕位置(兩種方法):
第一種:將控制元件座標轉換為螢幕座標!
pTag.left = button1.PointToScreen(System.Drawing.Point.Empty).X;
pTag.bottom = button1.PointToScreen(System.Drawing.Point.Empty).Y + button1.Height;
第二種:通過空間之間屬性的間接計算!注:button1 在 groupBox2 中!
pTag.left = SystemInformation.FrameBorderSize.Width + this.Left + groupBox2.Left + button1.Left; pTag.bottom = (this.Height - this.ClientRectangle.Height - SystemInformation.FrameBorderSize.Height) + this.Top + groupBox2.Top + button1.Top + button1.Height;
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
2. ColorSelector:
IColor pColor = new RgbColor(); pColor.RGB = 255; IColorSelector pSelector = new ColorSelectorClass(); pSelector.Color = pColor; if (pSelector.DoModal(0)) { pColor = pSelector.Color; }
效果:
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
3. ColorBrowser:
IColor pColor = new RgbColor(); pColor.RGB = 255; IColorBrowser pColorBrowser = new ColorBrowser(); pColorBrowser.Color = pColor; if (pColorBrowser.DoModal(0)) { pColor = pColorBrowser.Color; }
效果:
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣
第A9個 ╠══════════════════════════════════════════════════╣
╚════════╝
●·● 實現:顏色臺(Color Ramp):
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
1. AlgorithmicColorRamp:
定義函式:
private IEnumColors CreateColorRamp(IColor fromColor,IColor toColor,int count) { IAlgorithmicColorRamp pRampColor = new AlgorithmicColorRamp(); pRampColor.FromColor = fromColor; pRampColor.ToColor = toColor; pRampColor.Size = count; bool ok = false; pRampColor.CreateRamp(out ok); if (ok) { return pRampColor.Colors; } else { return null; } }
呼叫函式:顏色在 red 和 violet 之間變化!
private void timer1_Tick(object sender, EventArgs e) { IRgbColor fromColor = new RgbColor(); fromColor.Red = 255; IRgbColor toColor = new RgbColor(); toColor.Red = 128; toColor.Blue = 255; IEnumColors pEnumColors = CreateColorRamp(fromColor, toColor, 50); IColor pColor = null; for (int i = 0; i < count;i++ ) { pColor = pEnumColors.Next(); } if (count == 50) { count = 0; timer1.Enabled = false; timer2.Enabled = true; } count++; axPageLayoutControl1.PageLayout.Page.BackgroundColor = pColor; } private void timer2_Tick(object sender, EventArgs e) { IRgbColor fromColor = new RgbColor(); fromColor.Red = 128; fromColor.Blue = 255; IRgbColor toColor = new RgbColor(); toColor.Red = 255; IEnumColors pEnumColors = CreateColorRamp(fromColor, toColor, 20); IColor pColor = null; for (int i = 0; i < count; i++) { pColor = pEnumColors.Next(); } if (count == 20) { count = 0; timer2.Enabled = false; timer1.Enabled = true; } count++; axPageLayoutControl1.PageLayout.Page.BackgroundColor = pColor; }
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
2. RandomColorRamp:
定義函式:
private IColor CreateRandomColorRamp() { IRandomColorRamp pRandomColor = new RandomColorRamp(); pRandomColor.StartHue = 140; pRandomColor.EndHue = 220; pRandomColor.MinValue = 35; pRandomColor.MaxValue = 100; pRandomColor.MinSaturation = 32; pRandomColor.MaxSaturation = 100; pRandomColor.Size = 12; pRandomColor.Seed = 7; bool ok = true; pRandomColor.CreateRamp(out ok); IEnumColors pEnumColors = pRandomColor.Colors; IColor pColor = pEnumColors.Next(); return pColor; }
呼叫函式
private void button5_Click(object sender, EventArgs e) { IColor pColor = CreateRandomColorRamp(); axPageLayoutControl2.PageLayout.Page.BackgroundColor = pColor; }
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣
第G1個 ╠══════════════════════════════════════════════════╣
╚════════╝
●·● 實現:Symbol 物件:
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
1. SimpleMarkerSymbol:
新建工具!
using System; using System.Drawing; using System.Runtime.InteropServices; using ESRI.ArcGIS.ADF.BaseC