ESRI.ArcGIS.Carto.IGraphicsContainer

本文介绍了IGraphicsContainer接口,该接口为管理图形元素集合的对象提供了成员访问权限,适用于PageLayout、Map和FDOGraphicsLayer等对象。文章详细列举了接口的成员方法,如AddElement、BringForward、DeleteAllElements等,并说明了实现该接口的类,如CompositeGraphicsLayer、FDOGraphicsLayer等。

提供对控制图形容器的成员的访问。

何时使用:

管理图形元素集合的对象实现此接口。例如,PageLayout、Map和FDOGraphicsLayer对象都实现了这个接口,以提供对它们管理的图形元素的访问。

PageLayout对象包含元素对象的集合,包括MapFrames、MapSurroundFrames和GraphicElements,如PictureElement、MarkerElement和LineElement。此接口的成员提供对元素的访问。

当使用此接口向在对应系统中操作的层类型(如FDOGraphicsLayer和CompositeGraphicsLayer)添加元素时,这些元素必须实现IGraphicElement。

成员:

成员描述
AddElement

向图层添加一个新的图形元素。

AddElements

向图层添加新的图形元素。

BringForward将指定元素移动一步,靠近元素堆栈的顶部。
BringToFront使指定元素在所有其他元素前面绘制。
DeleteAllElements删除所有元素。
DeleteElement删除给定的元素。
FindFrame查找包含指定对象的框架。
GetElementOrder用于撤消排序操作。
LocateElements返回给定坐标下的元素。
LocateElementsByEnvelope返回给定信封内的元素。
MoveElementFromGroup将元素从组移动到容器。
MoveElementToGroup将元素从容器移动到组。
Next返回容器中的下一个图形。
PutElementOrder用于撤消排序操作。
Reset重置内部光标,以便下一步返回第一个元素。
SendBackward一步一步地靠近元素堆栈的底部。
SendToBack使指定元素在所有其他元素后面绘制。
UpdateElement

图形元素的属性已经更改。

实现IGraphicsContainer的类

描述
CompositeGraphicsLayer一组象单层一样的图形层集合。
FDOGraphicsLayer用于注释层(特征数据对象图形层)的属性集合。
GlobeGraphicsLayer (esriGlobeCore)全球图形层
GraphicsLayer3D (esri3DAnalyst)三维图形层。
GraphicsSubLayer图形层通过复合图形层交接。
Map一个用于显示和操纵地图数据的容器。
PageLayout包含地图和地图包围。

创建方法 

由Map创建

IGraphicsContainer pGraphicsContainer = axMapControl1.Map as IGraphicsContainer;

由PageLayout创建

IGraphicsContainer pGraphicsContainer = pPageLayout as IGraphicsContainer;

 

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.RuntimeManager; using ESRI.ArcGIS.esriSystem; namespace 李佳怡2023030331 { static class Program { [STAThread] static void Main() { // ✅ 必须放在最前面! /*if (!ESRI.ArcGIS.RuntimeManager.RuntimeManager.Bind(ProductCode.Engine)) { MessageBox.Show("无法绑定 ArcGIS Engine 运行时"); return; } */ Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } public partial class Form1 : Form { private bool isDrawing = false; // 是否正在绘制轨迹 private List<IPoint> trackPoints = new List<IPoint>(); // 存储地图坐标点 private IElement trackElement = null; // 当前显示的轨迹图形元素 public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { try { string shapeName = "河北省行政区.shp"; string shapePath = Application.StartupPath; string fullPath = System.IO.Path.Combine(shapePath, shapeName); // 检查主文件及配套文件是否存在 if (!System.IO.File.Exists(fullPath)) { MessageBox.Show("主文件不存在:" + fullPath); return; } if (!System.IO.File.Exists(System.IO.Path.ChangeExtension(fullPath, ".shx"))) { MessageBox.Show("缺少 .shx 文件"); return; } if (!System.IO.File.Exists(System.IO.Path.ChangeExtension(fullPath, ".dbf"))) { MessageBox.Show("缺少 .dbf 文件"); return; } // 加载 Shapefile axMapControl1.AddShapeFile(shapePath, shapeName); axMapControl1.ActiveView.Refresh(); MessageBox.Show("Shapefile 加载完成!"); } catch (Exception ex) { MessageBox.Show("加载失败:" + ex.Message); } } private void btnAttrQuery_Click(object sender, EventArgs e) { Form2 frm = new Form2(this); frm.Show(); } private void btnDrawTrack_Click(object sender, EventArgs e) { isDrawing = !isDrawing; btnDrawTrack.Text = isDrawing ? "结束绘制" : "绘制轨迹"; if (!isDrawing) { // 结束绘制时不清空点,允许保留轨迹 // 若想清空再画新线,则保留下面这句 // trackPoints.Clear(); // UpdateTrackDrawing(); // 可选:清除最后一段未完成的线 } } private void btnClearTrack_Click() { IGraphicsContainer graphicsContainer = axMapControl1.ActiveView.GraphicsContainer; if (trackElement != null) { graphicsContainer.DeleteElement(trackElement); trackElement = null; } trackPoints.Clear(); IActiveView activeView = axMapControl1.ActiveView; activeView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGraphics, null, null); isDrawing = false; btnDrawTrack.Text = "绘制轨迹"; } // 鼠标按下:添加轨迹点 private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) { if (!isDrawing) return; if (e.button != 1) return; // 左键点击 try { // 将屏幕坐标转为地图坐标 IPoint mapPoint = axMapControl1.ToMapPoint(e.x, e.y); trackPoints.Add(mapPoint); UpdateTrackDrawing(); // 实时更新轨迹显示 } catch (Exception ex) { MessageBox.Show("添加点失败:" + ex.Message); } } // 更新轨迹显示 private void UpdateTrackDrawing() { IActiveView activeView = axMapControl1.ActiveView; IGraphicsContainer graphicsContainer = activeView.GraphicsContainer; // 移除旧轨迹 if (trackElement != null) { graphicsContainer.DeleteElement(trackElement); trackElement = null; } // 至少两个点才能成线 if (trackPoints.Count < 2) return; try { // 创建 Polyline IPolyline polyline = new PolylineClass(); IPointCollection pointCollection = polyline as IPointCollection; foreach (IPoint pt in trackPoints) { pointCollection.AddPoint(pt); } // 设置空间参考(非常重要) polyline.SpatialReference = axMapControl1.Map.SpatialReference; // 创建线图形元素 ILineElement lineElement = new LineElementClass(); IElement element = lineElement as IElement; element.Geometry = polyline; // 设置线符号 ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbolClass(); simpleLineSymbol.Color = GetColor(255, 0, 0) as IColor; // 红色 simpleLineSymbol.Width = 2; lineElement.Symbol = simpleLineSymbol as ILineSymbol; // 添加到图层 graphicsContainer.AddElement(element, 0); trackElement = element; // 局部刷新图形层 activeView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGraphics, null, null); } catch (Exception ex) { MessageBox.Show("更新轨迹失败:" + ex.Message); } } // 辅助方法:创建 RGB 颜色 private IRgbColor GetColor(int r, int g, int b) { IRgbColor color = new RgbColorClass(); color.Red = r; color.Green = g; color.Blue = b; return color; } } } 我现在 创建 Polyline代码IPolyline polyline = new PolylineClass();以及创建线图形元素和设置线符号的实例化报错,要怎么改
11-27
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; // 添加此行,用于处理路径 using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.SystemUI; namespace 李佳怡2023030331 { public partial class Form1 : Form { private bool isDrawing = false; // 是否正在绘制 private List<ESRI.ArcGIS.Geometry.IPoint> trackPoints = new List<ESRI.ArcGIS.Geometry.IPoint>(); // 存储轨迹点(地图坐标) private IElement trackElement; // 地图上的轨迹元素 public Form1() { InitializeComponent(); } private void btnAttrQuery_Click(object sender, EventArgs e) { Form2 frm = new Form2(this); frm.Show(); } private void Form1_Load(object sender, EventArgs e) { try { // 添加System.IO命名空间引用(顶部) //using System.IO; string shapeName = "河北省行政区.shp"; string shapePath = Application.StartupPath; string fullPath = Path.Combine(shapePath, shapeName); // 检查文件及配套文件是否存在 if (!File.Exists(fullPath)) { MessageBox.Show("主文件不存在:" + fullPath); return; } if (!File.Exists(Path.ChangeExtension(fullPath, ".shx")) || !File.Exists(Path.ChangeExtension(fullPath, ".dbf"))) { MessageBox.Show("缺少.shx或.dbf配套文件"); return; } // 直接调用AddShapeFile,不接收返回值(兼容void返回类型) axMapControl1.AddShapeFile(shapePath, shapeName); axMapControl1.ActiveView.Refresh(); // 刷新地图显示 MessageBox.Show("Shapefile加载完成"); } catch (Exception ex) { MessageBox.Show("加载失败:" + ex.Message); } } // 轨迹绘制相关变量 // 轨迹绘制相关变量 // 鼠标按下时开始绘制 private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e) { // 只有点击"绘制轨迹"按钮后才允许绘制 if (isDrawing && e.button == 1) // 1=左键 { // 将屏幕坐标转换为地图坐标并直接存储 ESRI.ArcGIS.Geometry.IPoint mapPoint = axMapControl1.ToMapPoint(e.x, e.y); trackPoints.Add(mapPoint); UpdateTrackDrawing(); // 更新轨迹显示 } } // 更新轨迹绘制 private void UpdateTrackDrawing() { // 移除之前的轨迹 if (trackElement != null) { axMapControl1.ActiveView.GraphicsContainer.DeleteElement(trackElement); } // 至少2个点才能绘制线 if (trackPoints.Count >= 2) { // 【修复核心】使用GeometryEnvironment创建Polyline,而非直接new PolylineClass() // ArcGIS 10.2中正确写法(无Class后缀) IGeometryEnvironment geometryEnv = new GeometryEnvironment(); // 错误写法:new GeometryEnvironmentClass(); // 10.2中无此类 IPolyline polyline = geometryFactory.CreatePolyline(trackPoints.ToArray()) as IPolyline; // 也可以手动通过IPointCollection添加点(兼容老版本) // IPolyline polyline = GeometryCreator.CreatePolyline(); // 若用自定义工具类 // IPointCollection pointCollection = polyline as IPointCollection; // foreach (var mapPoint in trackPoints) pointCollection.AddPoint(mapPoint); // 创建线元素(红色,宽度2) IElement element = new LineElementClass(); // 此处若也报错,用方案2的Activator创建 ILineSymbol lineSymbol = new SimpleLineSymbolClass(); lineSymbol.Color = GetColor(255, 0, 0); lineSymbol.Width = 2; (element as ILineElement).Symbol = lineSymbol; element.Geometry = polyline; // 添加到地图并刷新 axMapControl1.ActiveView.GraphicsContainer.AddElement(element, 0); trackElement = element; axMapControl1.ActiveView.Refresh(); } } // 辅助方法:创建颜色对象 private ESRI.ArcGIS.Display.IColor GetColor(int r, int g, int b) { ESRI.ArcGIS.Display.IRgbColor rgbColor = new ESRI.ArcGIS.Display.RgbColorClass(); rgbColor.Red = r; rgbColor.Green = g; rgbColor.Blue = b; return rgbColor; } private void btnDrawTrack_Click(object sender, EventArgs e) { isDrawing = !isDrawing; // 切换绘制状态 btnDrawTrack.Text = isDrawing ? "结束绘制" : "绘制轨迹"; // 切换按钮文字 if (!isDrawing) { trackPoints.Clear(); // 结束绘制时清空临时点 } } private void btnClearTrack_Click(object sender, EventArgs e) { if (trackElement != null) { axMapControl1.ActiveView.GraphicsContainer.DeleteElement(trackElement); trackElement = null; trackPoints.Clear(); axMapControl1.ActiveView.Refresh(); } // 重置绘制状态 isDrawing = false; btnDrawTrack.Text = "绘制轨迹"; } } } 帮我找下这个代码的错误
11-27
namespace 李佳怡2023030331 { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); this.axLicenseControl1 = new ESRI.ArcGIS.Controls.AxLicenseControl(); this.axMapControl1 = new ESRI.ArcGIS.Controls.AxMapControl(); this.axTOCControl1 = new ESRI.ArcGIS.Controls.AxTOCControl(); this.axToolbarControl1 = new ESRI.ArcGIS.Controls.AxToolbarControl(); this.btnAttrQuery = new System.Windows.Forms.Button(); this.btnDrawTrack = new System.Windows.Forms.Button(); this.btnClearTrack = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.axLicenseControl1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.axMapControl1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.axTOCControl1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.axToolbarControl1)).BeginInit(); this.SuspendLayout(); // // axLicenseControl1 // this.axLicenseControl1.Enabled = true; this.axLicenseControl1.Location = new System.Drawing.Point(44, 352); this.axLicenseControl1.Name = "axLicenseControl1"; this.axLicenseControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axLicenseControl1.OcxState"))); this.axLicenseControl1.Size = new System.Drawing.Size(32, 32); this.axLicenseControl1.TabIndex = 0; // // axMapControl1 // this.axMapControl1.Location = new System.Drawing.Point(163, 70); this.axMapControl1.Name = "axMapControl1"; this.axMapControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMapControl1.OcxState"))); this.axMapControl1.Size = new System.Drawing.Size(399, 291); this.axMapControl1.TabIndex = 1; // // axTOCControl1 // this.axTOCControl1.Location = new System.Drawing.Point(22, 70); this.axTOCControl1.Name = "axTOCControl1"; this.axTOCControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axTOCControl1.OcxState"))); this.axTOCControl1.Size = new System.Drawing.Size(112, 291); this.axTOCControl1.TabIndex = 2; // // axToolbarControl1 // this.axToolbarControl1.Location = new System.Drawing.Point(25, 13); this.axToolbarControl1.Name = "axToolbarControl1"; this.axToolbarControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axToolbarControl1.OcxState"))); this.axToolbarControl1.Size = new System.Drawing.Size(536, 28); this.axToolbarControl1.TabIndex = 3; // // btnAttrQuery // this.btnAttrQuery.Location = new System.Drawing.Point(763, 70); this.btnAttrQuery.Name = "btnAttrQuery"; this.btnAttrQuery.Size = new System.Drawing.Size(89, 39); this.btnAttrQuery.TabIndex = 4; this.btnAttrQuery.Text = "属性查询"; this.btnAttrQuery.UseVisualStyleBackColor = true; this.btnAttrQuery.Click += new System.EventHandler(this.btnAttrQuery_Click); // // btnDrawTrack // this.btnDrawTrack.Location = new System.Drawing.Point(763, 140); this.btnDrawTrack.Name = "btnDrawTrack"; this.btnDrawTrack.Size = new System.Drawing.Size(89, 38); this.btnDrawTrack.TabIndex = 5; this.btnDrawTrack.Text = "绘制轨迹"; this.btnDrawTrack.UseVisualStyleBackColor = true; //this.btnDrawTrack.Click += new System.EventHandler(this.btnDrawTrack_Click); // // btnClearTrack, // // 原代码(被注释): //this.btnDrawTrack.Click += new System.EventHandler(this.btnDrawTrack_Click); //this.btnClearTrack.Click += new System.EventHandler(this.btnClearTrack_Click); // 修正后: this.btnDrawTrack.Click += new System.EventHandler(this.btnDrawTrack_Click); //this.btnClearTrack.Click += new System.EventHandler(this.btnClearTrack_Click); this.btnClearTrack.Location = new System.Drawing.Point(763, 208); this.btnClearTrack.Name = "btnClearTrack"; this.btnClearTrack.Size = new System.Drawing.Size(89, 42); this.btnClearTrack.TabIndex = 6; this.btnClearTrack.Text = "重绘轨迹"; this.btnClearTrack.UseVisualStyleBackColor = true; //this.btnClearTrack.Click += new System.EventHandler(this.btnClearTrack_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(864, 503); this.Controls.Add(this.btnClearTrack); this.Controls.Add(this.btnDrawTrack); this.Controls.Add(this.btnAttrQuery); this.Controls.Add(this.axToolbarControl1); this.Controls.Add(this.axTOCControl1); this.Controls.Add(this.axMapControl1); this.Controls.Add(this.axLicenseControl1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.axLicenseControl1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.axMapControl1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.axTOCControl1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.axToolbarControl1)).EndInit(); this.ResumeLayout(false); // 新增的鼠标按下事件绑定 this.axMapControl1.OnMouseDown += new ESRI.ArcGIS.Controls.IMapControlEvents2_Ax_OnMouseDownEventHandler(this.axMapControl1_OnMouseDown); } #endregion private ESRI.ArcGIS.Controls.AxLicenseControl axLicenseControl1; private ESRI.ArcGIS.Controls.AxTOCControl axTOCControl1; private ESRI.ArcGIS.Controls.AxToolbarControl axToolbarControl1; private System.Windows.Forms.Button btnAttrQuery; public ESRI.ArcGIS.Controls.AxMapControl axMapControl1; private System.Windows.Forms.Button btnDrawTrack; private System.Windows.Forms.Button btnClearTrack; } } 警告 1 由于程序集“d:\Program Files (x86)\ArcGIS\DeveloperKit10.2\DotNet\ESRI.ArcGIS.AxControls.dll”创建了对嵌入互操作程序集“c:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Common\stdole.dll”的间接引用,因此创建了对该程序集的引用。请考虑更改其中一个程序集的“嵌入互操作类型”属性。这样不能运行
最新发布
11-27
未处理 System.Runtime.InteropServices.COMException HResult=-2146827235 Message=指定的路径无效 Source=esriControls.MapControl.1 ErrorCode=-2146827235 StackTrace: 在 System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData) 在 ESRI.ArcGIS.Controls.IMapControlDefault.AddShapeFile(String Path, String fileName) 在 AxESRI.ArcGIS.Controls.AxMapControl.AddShapeFile(String path, String fileName) 在 _2023012505.Form1.Form1_Load(Object sender, EventArgs e) 位置 D:\改\地信一班李健华2023012505\地信一班李健华2023012505\2023012505\form1.cs:行号 38 在 System.Windows.Forms.Form.OnLoad(EventArgs e) 在 System.Windows.Forms.Form.OnCreateControl() 在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) 在 System.Windows.Forms.Control.CreateControl() 在 System.Windows.Forms.Control.WmShowWindow(Message& m) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ScrollableControl.WndProc(Message& m) 在 System.Windows.Forms.Form.WmShowWindow(Message& m) 在 System.Windows.Forms.Form.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) InnerException: using System; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.SystemUI; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.Display; namespace _2023012505 { public partial class Form1 : Form { private ICommand cmdPan; private ICommand cmdFullExtent; private ICommand cmdZoomIn; private ICommand cmdZoomOut; private bool isDrawing = false; private IPointCollection pointCollection = new MultipointClass(); private IGraphicsContainer graphicsContainer; private IActiveView activeView; private IElement pointElement; private IElement lineElement; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { axMapControl1.AddShapeFile("..\\222\\data", "hlj1.shp"); axMapControl1.AddShapeFile("..\\222\\data", "city.shp"); axTOCControl1.SetBuddyControl(axMapControl1); cmdPan = new ControlsMapPanToolClass(); cmdPan.OnCreate(axMapControl1.Object); cmdFullExtent = new ControlsMapFullExtentCommandClass(); cmdFullExtent.OnCreate(axMapControl1.Object); cmdZoomIn = new ControlsMapZoomInToolClass(); cmdZoomIn.OnCreate(axMapControl1.Object); cmdZoomOut = new ControlsMapZoomOutToolClass(); cmdZoomOut.OnCreate(axMapControl1.Object); graphicsContainer = axMapControl1.Map as IGraphicsContainer; activeView = axMapControl1.ActiveView; LoadPlaceNames(); } private void LoadPlaceNames() { comboBox1.Items.Clear(); IFeatureLayer featureLayer = GetFeatureLayerByName("hlj1"); if (featureLayer == null) return; IFeatureClass featureClass = featureLayer.FeatureClass; IFeatureCursor featureCursor = featureClass.Search(null, true); IFeature feature = featureCursor.NextFeature(); while (feature != null) { int nameFieldIndex = feature.Fields.FindField("name"); if (nameFieldIndex >= 0) { object fieldValue = feature.get_Value(nameFieldIndex); if (fieldValue != null) { comboBox1.Items.Add(fieldValue.ToString()); } } feature = featureCursor.NextFeature(); } } private IFeatureLayer GetFeatureLayerByName(string layerName) { for (int i = 0; i < axMapControl1.LayerCount; i++) { if (axMapControl1.get_Layer(i).Name == layerName) { return axMapControl1.get_Layer(i) as IFeatureLayer; } } return null; } private void axTOCControl1_OnMouseDown(object sender, AxESRI.ArcGIS.Controls.ITOCControlEvents_OnMouseDownEvent e) { } private void button4_Click(object sender, EventArgs e) { axMapControl1.CurrentTool = cmdPan as ITool; } private void button2_Click(object sender, EventArgs e) { axMapControl1.Extent = axMapControl1.FullExtent; axMapControl1.CurrentTool = null; } private void button3_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(comboBox1.Text)) { MessageBox.Show("请先选择一个地名"); return; } IFeatureLayer featureLayer = GetFeatureLayerByName("hlj1"); if (featureLayer == null) { MessageBox.Show("未找到hlj1图层"); return; } IFeatureClass featureClass = featureLayer.FeatureClass; IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = "name='" + comboBox1.Text + "'"; IFeatureCursor featureCursor = featureClass.Search(queryFilter, true); IFeature feature = featureCursor.NextFeature(); if (feature != null) { IFeatureSelection featureSelection = featureLayer as IFeatureSelection; if (featureSelection != null) { featureSelection.Clear(); } IQueryFilter selectFilter = new QueryFilterClass(); selectFilter.WhereClause = "name='" + comboBox1.Text + "'"; if (featureSelection != null) { featureSelection.SelectFeatures(selectFilter, esriSelectionResultEnum.esriSelectionResultNew, false); } axMapControl1.FlashShape(feature.Shape, 3, 500, null); IEnvelope featureExtent = feature.Extent; featureExtent.Expand(1.5, 1.5, true); axMapControl1.Extent = featureExtent; axMapControl1.ActiveView.Refresh(); } else { MessageBox.Show("未找到匹配的要素"); } } private void button5_Click(object sender, EventArgs e) { string searchText = ""; if (!string.IsNullOrEmpty(textBox1.Text)) { searchText = textBox1.Text; } else if (!string.IsNullOrEmpty(comboBox1.Text)) { searchText = comboBox1.Text; } else { MessageBox.Show("请在文本框中输入地名或在组合框中选择地名"); return; } IFeatureLayer featureLayer = GetFeatureLayerByName("hlj1"); if (featureLayer == null) { MessageBox.Show("未找到hlj1图层"); return; } IFeatureClass featureClass = featureLayer.FeatureClass; IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = "name='" + searchText + "'"; IFeatureCursor featureCursor = featureClass.Search(queryFilter, true); IFeature feature = featureCursor.NextFeature(); if (feature != null) { IFeatureSelection featureSelection = featureLayer as IFeatureSelection; if (featureSelection != null) { featureSelection.Clear(); } IQueryFilter selectFilter = new QueryFilterClass(); selectFilter.WhereClause = "name='" + searchText + "'"; if (featureSelection != null) { featureSelection.SelectFeatures(selectFilter, esriSelectionResultEnum.esriSelectionResultNew, false); } axMapControl1.FlashShape(feature.Shape); axMapControl1.Extent = feature.Extent; axMapControl1.ActiveView.Refresh(); MessageBox.Show("已高亮显示"); } else { MessageBox.Show("未找到匹配的要素: {searchText}"); } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog openFile = new OpenFileDialog(); openFile.Filter = "ArcGIS地图文档 (*.mxd)|*.mxd"; switch (openFile.ShowDialog()) { case DialogResult.OK: axMapControl1.LoadMxFile(openFile.FileName); MessageBox.Show("地图加载成功!"); break; case DialogResult.Cancel: default: break; } } private void 放大ToolStripMenuItem_Click(object sender, EventArgs e) { axMapControl1.CurrentTool = cmdZoomIn as ITool; } private void 缩小ToolStripMenuItem_Click(object sender, EventArgs e) { axMapControl1.CurrentTool = cmdZoomOut as ITool; } private void button1_Click(object sender, EventArgs e) { Form2 form = new Form2(this); form.Show(); } private void axMapControl1_OnMouseDown(object sender, AxESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e) { if (isDrawing) { IPoint point = axMapControl1.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x, e.y); pointCollection.AddPoint(point); ISimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbolClass(); markerSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle; markerSymbol.Size = 8; markerSymbol.Color = GetRGBColor(255, 0, 0); IMarkerElement markerElement = new MarkerElementClass(); markerElement.Symbol = markerSymbol; pointElement = markerElement as IElement; pointElement.Geometry = point; graphicsContainer.AddElement(pointElement, 0); if (!string.IsNullOrEmpty(textBox2.Text)) { ITextSymbol textSymbol = new TextSymbolClass(); textSymbol.Color = GetRGBColor(0, 0, 0); textSymbol.Size = 10; IPoint textPoint = new PointClass(); textPoint.X = point.X; textPoint.Y = point.Y + 1000; ITextElement textElement = new TextElementClass(); textElement.Symbol = textSymbol; textElement.Text = textBox2.Text; IElement element = textElement as IElement; element.Geometry = textPoint; graphicsContainer.AddElement(element, 0); } if (pointCollection.PointCount > 1) { IPolyline polyline = new PolylineClass(); polyline.FromPoint = pointCollection.get_Point(pointCollection.PointCount - 2); polyline.ToPoint = point; ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass(); lineSymbol.Style = esriSimpleLineStyle.esriSLSSolid; lineSymbol.Width = 2; lineSymbol.Color = GetRGBColor(0, 0, 255); ILineElement lineElementClass = new LineElementClass(); lineElementClass.Symbol = lineSymbol; lineElement = lineElementClass as IElement; lineElement.Geometry = polyline; graphicsContainer.AddElement(lineElement, 0); } activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); } } private IColor GetRGBColor(int red, int green, int blue) { IRgbColor rgbColor = new RgbColorClass(); rgbColor.Red = red; rgbColor.Green = green; rgbColor.Blue = blue; return rgbColor as IColor; } private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e) { } private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e) { } private void splitContainer2_Panel1_Paint(object sender, PaintEventArgs e) { } private void button7_Click(object sender, EventArgs e) { isDrawing = true; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair; pointCollection = new MultipointClass(); } private void button9_Click(object sender, EventArgs e) { graphicsContainer.DeleteAllElements(); activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); } private void button8_Click(object sender, EventArgs e) { // 重置图形容器迭代器 graphicsContainer.Reset(); // 获取第一个元素 IElement element = graphicsContainer.Next(); // 创建临时列表存储要删除的元素 System.Collections.Generic.List<IElement> elementsToDelete = new System.Collections.Generic.List<IElement>(); // 遍历所有元素 while (element != null) { // 检查元素是否为线元素 if (element is ILineElement) { elementsToDelete.Add(element); } element = graphicsContainer.Next(); } // 删除所有线元素 foreach (IElement elem in elementsToDelete) { graphicsContainer.DeleteElement(elem); } // 刷新视图 activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); } private void textBox2_TextChanged(object sender, EventArgs e) { } private void button6_Click(object sender, EventArgs e) { } private void button10_Click(object sender, EventArgs e) { } private void button10_Click_1(object sender, EventArgs e) { isDrawing = false; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerArrow; } } }把修复后的完整代码给我,不需要注释
11-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值