ESRI.ArcGIS.Carto.IGraphicsContainer

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

何时使用:

管理图形元素集合的对象实现此接口。例如,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 ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Display; namespace WaterPipelineGIS2 { public partial class Form1 : Form { private IFeatureLayer _selectedFeatureLayer; private System.Drawing.Point _lastRightClickPosition; public Form1() { InitializeComponent(); axMapControl1.OnMouseMove += new IMapControlEvents2_Ax_OnMouseMoveEventHandler(axMapControl1_OnMouseMove); } private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e) { // 获取地图坐标 double mapX = e.mapX; double mapY = e.mapY; // 格式化坐标显示(保留3位小数) lblCoordinate.Text = string.Format("X: {0:F3} Y: {1:F3}", mapX, mapY); // 立即刷新状态栏 statusStrip1.Refresh(); } private void toolStripMenuItem2_Click(object sender, EventArgs e) { using (var rotateForm = new RotateForm()) { if (rotateForm.ShowDialog() == DialogResult.OK) { try { axMapControl1.Rotation = rotateForm.RotationAngle; axMapControl1.ActiveView.Refresh(); // 可选:更新状态栏 lblCoordinate.Text += " 旋转角度:{rotateForm.RotationAngle}°"; } catch (Exception ex) { MessageBox.Show("旋转失败:{ex.Message}"); } } } } private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e) { if (e.button == 2) // 右键 { // 保存点击位置(控件坐标系) _lastRightClickPosition = new System.Drawing.Point(e.x, e.y); ITOCControl2 tocControl = (ITOCControl2)axTOCControl1.Object; // 修改点1:声明为接口类型并初始化为null IBasicMap basicMap = null; ILayer layer = null; // 修改点2:使用Type.Missing代替new object() object other = Type.Missing; object index = Type.Missing; esriTOCControlItem itemType = esriTOCControlItem.esriTOCControlItemNone; // 修改点3:正确传递ref参数 tocControl.HitTest(e.x, e.y, ref itemType, ref basicMap, ref layer, ref other, ref index); if (itemType == esriTOCControlItem.esriTOCControlItemLayer && layer != null) { contextMenuStripTOC.Show(axTOCControl1, e.x, e.y); } } } // 修改后(使用 MouseEventArgs) private void openAttributeTableToolStripMenuItem_Click(object sender, EventArgs e) { ITOCControl2 tocControl = (ITOCControl2)axTOCControl1.Object; IBasicMap basicMap = null; ILayer layer = null; object other = Type.Missing; object index = Type.Missing; esriTOCControlItem itemType = esriTOCControlItem.esriTOCControlItemNone; // 使用保存的控件坐标系位置 tocControl.HitTest( _lastRightClickPosition.X, _lastRightClickPosition.Y, ref itemType, ref basicMap, ref layer, ref other, ref index ); IFeatureLayer featureLayer = layer as IFeatureLayer; if (featureLayer != null) { _selectedFeatureLayer = featureLayer; // 确保使用正确的构造函数 AttributeTableForm attrForm = new AttributeTableForm( _selectedFeatureLayer, axMapControl1.Object as IMapControl2 ); attrForm.Show(); } else { MessageBox.Show("请选择有效的要素图层!"); } } private void SetSelectionSymbol() { // 使用接口创建符号 ISimpleFillSymbol fillSymbol = new SimpleFillSymbol() as ISimpleFillSymbol; fillSymbol.Color = GetRgbColor(255, 0, 0); fillSymbol.Style = esriSimpleFillStyle.esriSFSSolid; ISimpleLineSymbol lineSymbol = new SimpleLineSymbol() as ISimpleLineSymbol; lineSymbol.Color = GetRgbColor(255, 255, 0); lineSymbol.Width = 2; fillSymbol.Outline = lineSymbol; // 设置渲染器 if (_selectedFeatureLayer != null) { IGeoFeatureLayer geoLayer = (IGeoFeatureLayer)_selectedFeatureLayer; ISimpleRenderer renderer = new SimpleRenderer() as ISimpleRenderer; renderer.Symbol = (ISymbol)fillSymbol; geoLayer.Renderer = (IFeatureRenderer)renderer; axMapControl1.ActiveView.Refresh(); } } private IRgbColor GetRgbColor(int r, int g, int b) { IRgbColor color = new RgbColor() as IRgbColor; // 正确方式 color.Red = r; color.Green = g; color.Blue = b; return color; } public object _featureLayer { get; set; } } }三、实习内容 第一部分: 1.供水管线数据的处理:根据某厂区供水管线图(Shapefile格式,见附件1)、供水管道探测数据(见附件2)组织好供水管线的空间与属性数据,建立基于ArcGIS个人地理数据库的供水管网的几何网络模型(有兴趣的同学可以考虑建立基于SQL Server的空间数据库),即网络数据集;将管线图形数据与探测属性数据关联起来;编制相应的MXD地图文档;为后续功能实现打基础。 2.供水管网地理信息系统的功能模块设计与界面设计:根据供水管网地理信息系统功能需求划分好系统的功能模块,并进行系统界面设计,包括菜单、工具条、状态栏等设计。在设计各功能模块界面时,要考虑各窗体间的相互关系。功能需求如下: a)基本GIS操作功能:地图显示、放大、缩小、平移(放大、缩小、平移等工具的图标后面需要显示中文字符)、滚轮缩放、鹰眼导航、地图按某一给定的角度旋转,鼠标位置坐标的状态栏实时显示,地图缩放比例状态栏显示。 b)地图加载功能:加载Mxd地图文档、添加Shapefile图层文件。 c)图层控制功能:图层显示与否,图层对应图形的居中显示,打开图层属性表,属性表中切换记录时当前记录的图形高亮定位、闪烁与居中显示3个功能。 d)空间选择功能:可以绘制矩形、圆形、多边形、折线进行地理要素的选择。 e)空间量测功能:可进行多边形面积与周长量测、折线周长量测、点的坐标量测。 f)空间查询功能:图形与其属性的综合查询,包括构建表达式查询图形及属性、按管线节点类型(如三通、四通、弯头等,参见附件2)查询节点属性及相应图形、点击图形查其对应的属性信息、根据管径大小查询其空间位置及属性信息、按照材质查询相应的管线节点位置及其属性、按照附属物类型查询管线节点空间位置及属性信息。 g)图形编辑功能:管线节点编辑(节点的添加、删除、移动、保存),管线编辑(管线的添加、删除、移动、保存)。 h)管线可视化分析功能:不同类型(如三通、四通、弯头等,参见附件2)的管线节点用不同的颜色符号表示,不同材质(如钢、铸铁等,参见附件2)的管线段用不同颜色表示,此功能的实现需要很好地组织管线探测数据及其与管线的关联关系。 i)对管线、管线节点进行缓冲区构建,分析参数要求界面交互设定。 j)基于建立的供水几何网络,进行简单的最短路径分析。此功能为选作功能项,学有余力的同学可研究如何实现。 k)开发的实验系统要求有登录界面,且输入正确的用户名和密码后,才能看到主界面。用户名登录认证要求基于数据库方式实现,需要在个人地理数据库中要建立一张用户表,表中包含用户名和密码字段。 3.利用C#和ArcEngine实现上述功能:每个学生利用大三上学期课程“GIS应用开发”所学内容独立完成上述功能的开发我现在想要实现d)空间选择功能:可以绘制矩形、圆形、多边形、折线进行地理要素的选择。我现在就是功能菜单打开,点击空间选择,点击绘制矩形选择、绘制圆形选择、绘制多边形选择、绘制折线选择。从而进行选择,给出完整代码以及操作流程
07-12
namespace WaterPipelineGIS2 { partial class main { /// <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() { this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(main)); this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItem3 = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItem4 = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItem5 = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItem6 = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItem7 = new System.Windows.Forms.ToolStripMenuItem(); this.清除选择ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.空间量测ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.面积量测ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.周长量测ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.点坐标量测ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.清除已绘制图形ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.panel1 = new System.Windows.Forms.Panel(); this.splitContainer1 = new System.Windows.Forms.SplitContainer(); this.groupBoxEagleEye = new System.Windows.Forms.GroupBox(); this.statusStrip1 = new System.Windows.Forms.StatusStrip(); this.lblCoordinate = new System.Windows.Forms.ToolStripStatusLabel(); this.lblResult = new System.Windows.Forms.ToolStripStatusLabel(); this.axLicenseControl1 = new ESRI.ArcGIS.Controls.AxLicenseControl(); this.txtRotateAngle = new System.Windows.Forms.ToolStripTextBox(); this.contextMenuStripTOC = new System.Windows.Forms.ContextMenuStrip(this.components); this.openAttributeTableToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.axMapControl2 = new ESRI.ArcGIS.Controls.AxMapControl(); this.axTOCControl1 = new ESRI.ArcGIS.Controls.AxTOCControl(); this.axMapControl1 = new ESRI.ArcGIS.Controls.AxMapControl(); this.axToolbarControl1 = new ESRI.ArcGIS.Controls.AxToolbarControl(); this.折线周长量测ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel(); this.menuStrip1.SuspendLayout(); this.panel1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); this.splitContainer1.Panel1.SuspendLayout(); this.splitContainer1.Panel2.SuspendLayout(); this.splitContainer1.SuspendLayout(); this.groupBoxEagleEye.SuspendLayout(); this.statusStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.axLicenseControl1)).BeginInit(); this.contextMenuStripTOC.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.axMapControl2)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.axTOCControl1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.axMapControl1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.axToolbarControl1)).BeginInit(); this.SuspendLayout(); // // menuStrip1 // this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripMenuItem1}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Size = new System.Drawing.Size(1042, 32); this.menuStrip1.TabIndex = 0; this.menuStrip1.Text = "menuStrip1"; // // toolStripMenuItem1 // this.toolStripMenuItem1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripMenuItem2, this.toolStripMenuItem3, this.空间量测ToolStripMenuItem}); this.toolStripMenuItem1.Name = "toolStripMenuItem1"; this.toolStripMenuItem1.Size = new System.Drawing.Size(58, 28); this.toolStripMenuItem1.Text = "功能"; // // toolStripMenuItem2 // this.toolStripMenuItem2.Name = "toolStripMenuItem2"; this.toolStripMenuItem2.Size = new System.Drawing.Size(152, 28); this.toolStripMenuItem2.Text = "地图旋转"; this.toolStripMenuItem2.Click += new System.EventHandler(this.toolStripMenuItem2_Click); // // toolStripMenuItem3 // this.toolStripMenuItem3.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripMenuItem4, this.toolStripMenuItem5, this.toolStripMenuItem6, this.toolStripMenuItem7, this.清除选择ToolStripMenuItem}); this.toolStripMenuItem3.Name = "toolStripMenuItem3"; this.toolStripMenuItem3.Size = new System.Drawing.Size(152, 28); this.toolStripMenuItem3.Text = "空间选择"; // // toolStripMenuItem4 // this.toolStripMenuItem4.Name = "toolStripMenuItem4"; this.toolStripMenuItem4.Size = new System.Drawing.Size(206, 28); this.toolStripMenuItem4.Text = "绘制矩形选择"; this.toolStripMenuItem4.Click += new System.EventHandler(this.btnRectSelect_Click); // // toolStripMenuItem5 // this.toolStripMenuItem5.Name = "toolStripMenuItem5"; this.toolStripMenuItem5.Size = new System.Drawing.Size(206, 28); this.toolStripMenuItem5.Text = "绘制圆形选择"; this.toolStripMenuItem5.Click += new System.EventHandler(this.btnCircleSelect_Click); // // toolStripMenuItem6 // this.toolStripMenuItem6.Name = "toolStripMenuItem6"; this.toolStripMenuItem6.Size = new System.Drawing.Size(206, 28); this.toolStripMenuItem6.Text = "绘制多边形选择"; this.toolStripMenuItem6.Click += new System.EventHandler(this.btnPolygonSelect_Click); // // toolStripMenuItem7 // this.toolStripMenuItem7.Name = "toolStripMenuItem7"; this.toolStripMenuItem7.Size = new System.Drawing.Size(206, 28); this.toolStripMenuItem7.Text = "绘制折线选择"; this.toolStripMenuItem7.Click += new System.EventHandler(this.btnLineSelect_Click); // // 清除选择ToolStripMenuItem // this.清除选择ToolStripMenuItem.Name = "清除选择ToolStripMenuItem"; this.清除选择ToolStripMenuItem.Size = new System.Drawing.Size(206, 28); this.清除选择ToolStripMenuItem.Text = "清除选择"; this.清除选择ToolStripMenuItem.Click += new System.EventHandler(this.btnClearSelection_Click); // // 空间量测ToolStripMenuItem // this.空间量测ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.面积量测ToolStripMenuItem, this.周长量测ToolStripMenuItem, this.折线周长量测ToolStripMenuItem, this.点坐标量测ToolStripMenuItem, this.清除已绘制图形ToolStripMenuItem}); this.空间量测ToolStripMenuItem.Name = "空间量测ToolStripMenuItem"; this.空间量测ToolStripMenuItem.Size = new System.Drawing.Size(152, 28); this.空间量测ToolStripMenuItem.Text = "空间量测"; // // 面积量测ToolStripMenuItem // this.面积量测ToolStripMenuItem.Name = "面积量测ToolStripMenuItem"; this.面积量测ToolStripMenuItem.Size = new System.Drawing.Size(206, 28); this.面积量测ToolStripMenuItem.Text = "面积量测"; this.面积量测ToolStripMenuItem.Click += new System.EventHandler(this.面积量测ToolStripMenuItem_Click); // // 周长量测ToolStripMenuItem // this.周长量测ToolStripMenuItem.Name = "周长量测ToolStripMenuItem"; this.周长量测ToolStripMenuItem.Size = new System.Drawing.Size(206, 28); this.周长量测ToolStripMenuItem.Text = "周长量测"; this.周长量测ToolStripMenuItem.Click += new System.EventHandler(this.周长量测ToolStripMenuItem_Click); // // 点坐标量测ToolStripMenuItem // this.点坐标量测ToolStripMenuItem.Name = "点坐标量测ToolStripMenuItem"; this.点坐标量测ToolStripMenuItem.Size = new System.Drawing.Size(206, 28); this.点坐标量测ToolStripMenuItem.Text = "点坐标量测"; this.点坐标量测ToolStripMenuItem.Click += new System.EventHandler(this.点坐标量测ToolStripMenuItem_Click); // // 清除已绘制图形ToolStripMenuItem // this.清除已绘制图形ToolStripMenuItem.Name = "清除已绘制图形ToolStripMenuItem"; this.清除已绘制图形ToolStripMenuItem.Size = new System.Drawing.Size(206, 28); this.清除已绘制图形ToolStripMenuItem.Text = "清除已绘制图形"; this.清除已绘制图形ToolStripMenuItem.Click += new System.EventHandler(this.清除已绘制图形ToolStripMenuItem_Click); // // panel1 // this.panel1.Controls.Add(this.splitContainer1); this.panel1.Dock = System.Windows.Forms.DockStyle.Fill; this.panel1.Location = new System.Drawing.Point(0, 60); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(1042, 585); this.panel1.TabIndex = 2; // // splitContainer1 // this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill; this.splitContainer1.Location = new System.Drawing.Point(0, 0); this.splitContainer1.Name = "splitContainer1"; // // splitContainer1.Panel1 // this.splitContainer1.Panel1.Controls.Add(this.groupBoxEagleEye); this.splitContainer1.Panel1.Controls.Add(this.axTOCControl1); // // splitContainer1.Panel2 // this.splitContainer1.Panel2.Controls.Add(this.statusStrip1); this.splitContainer1.Panel2.Controls.Add(this.axLicenseControl1); this.splitContainer1.Panel2.Controls.Add(this.axMapControl1); this.splitContainer1.Size = new System.Drawing.Size(1042, 585); this.splitContainer1.SplitterDistance = 247; this.splitContainer1.TabIndex = 0; // // groupBoxEagleEye // this.groupBoxEagleEye.Controls.Add(this.axMapControl2); this.groupBoxEagleEye.Dock = System.Windows.Forms.DockStyle.Bottom; this.groupBoxEagleEye.Location = new System.Drawing.Point(0, 261); this.groupBoxEagleEye.Name = "groupBoxEagleEye"; this.groupBoxEagleEye.Size = new System.Drawing.Size(247, 324); this.groupBoxEagleEye.TabIndex = 2; this.groupBoxEagleEye.TabStop = false; this.groupBoxEagleEye.Text = "鹰眼导航"; // // statusStrip1 // this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.lblCoordinate, this.lblResult, this.toolStripStatusLabel1}); this.statusStrip1.Location = new System.Drawing.Point(0, 556); this.statusStrip1.Name = "statusStrip1"; this.statusStrip1.Padding = new System.Windows.Forms.Padding(2, 0, 14, 0); this.statusStrip1.Size = new System.Drawing.Size(791, 29); this.statusStrip1.TabIndex = 0; this.statusStrip1.Text = "statusStrip1"; // // lblCoordinate // this.lblCoordinate.Name = "lblCoordinate"; this.lblCoordinate.Size = new System.Drawing.Size(644, 24); this.lblCoordinate.Spring = true; this.lblCoordinate.Text = "lblCoordinate"; this.lblCoordinate.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // lblResult // this.lblResult.Name = "lblResult"; this.lblResult.Size = new System.Drawing.Size(85, 24); this.lblResult.Text = "lblResult"; // // axLicenseControl1 // this.axLicenseControl1.Enabled = true; this.axLicenseControl1.Location = new System.Drawing.Point(757, 528); 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 = 1; // // txtRotateAngle // this.txtRotateAngle.Name = "txtRotateAngle"; this.txtRotateAngle.Size = new System.Drawing.Size(100, 23); // // contextMenuStripTOC // this.contextMenuStripTOC.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.openAttributeTableToolStripMenuItem}); this.contextMenuStripTOC.Name = "contextMenuStripTOC"; this.contextMenuStripTOC.Size = new System.Drawing.Size(171, 32); // // openAttributeTableToolStripMenuItem // this.openAttributeTableToolStripMenuItem.Name = "openAttributeTableToolStripMenuItem"; this.openAttributeTableToolStripMenuItem.Size = new System.Drawing.Size(170, 28); this.openAttributeTableToolStripMenuItem.Text = "打开属性表"; this.openAttributeTableToolStripMenuItem.Click += new System.EventHandler(this.openAttributeTableToolStripMenuItem_Click); // // axMapControl2 // this.axMapControl2.Dock = System.Windows.Forms.DockStyle.Fill; this.axMapControl2.Location = new System.Drawing.Point(3, 24); this.axMapControl2.Name = "axMapControl2"; this.axMapControl2.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMapControl2.OcxState"))); this.axMapControl2.Size = new System.Drawing.Size(241, 297); this.axMapControl2.TabIndex = 0; // // axTOCControl1 // this.axTOCControl1.Dock = System.Windows.Forms.DockStyle.Fill; this.axTOCControl1.Location = new System.Drawing.Point(0, 0); this.axTOCControl1.Name = "axTOCControl1"; this.axTOCControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axTOCControl1.OcxState"))); this.axTOCControl1.Size = new System.Drawing.Size(247, 585); this.axTOCControl1.TabIndex = 0; this.axTOCControl1.OnMouseDown += new ESRI.ArcGIS.Controls.ITOCControlEvents_Ax_OnMouseDownEventHandler(this.axTOCControl1_OnMouseDown); // // axMapControl1 // this.axMapControl1.Dock = System.Windows.Forms.DockStyle.Fill; this.axMapControl1.Location = new System.Drawing.Point(0, 0); this.axMapControl1.Name = "axMapControl1"; this.axMapControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMapControl1.OcxState"))); this.axMapControl1.Size = new System.Drawing.Size(791, 585); this.axMapControl1.TabIndex = 0; this.axMapControl1.OnMouseDown += new ESRI.ArcGIS.Controls.IMapControlEvents2_Ax_OnMouseDownEventHandler(this.axMapControl1_OnMouseDown); this.axMapControl1.OnMouseMove += new ESRI.ArcGIS.Controls.IMapControlEvents2_Ax_OnMouseMoveEventHandler(this.axMapControl1_OnMouseMove); this.axMapControl1.OnDoubleClick += new ESRI.ArcGIS.Controls.IMapControlEvents2_Ax_OnDoubleClickEventHandler(this.axMapControl1_OnDoubleClick); // // axToolbarControl1 // this.axToolbarControl1.Dock = System.Windows.Forms.DockStyle.Top; this.axToolbarControl1.Location = new System.Drawing.Point(0, 32); this.axToolbarControl1.Name = "axToolbarControl1"; this.axToolbarControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axToolbarControl1.OcxState"))); this.axToolbarControl1.Size = new System.Drawing.Size(1042, 28); this.axToolbarControl1.TabIndex = 1; // // 折线周长量测ToolStripMenuItem // this.折线周长量测ToolStripMenuItem.Name = "折线周长量测ToolStripMenuItem"; this.折线周长量测ToolStripMenuItem.Size = new System.Drawing.Size(206, 28); this.折线周长量测ToolStripMenuItem.Text = "折线周长量测"; this.折线周长量测ToolStripMenuItem.Click += new System.EventHandler(this.折线周长量测ToolStripMenuItem_Click); // // toolStripStatusLabel1 // this.toolStripStatusLabel1.Name = "toolStripStatusLabel1"; this.toolStripStatusLabel1.Size = new System.Drawing.Size(46, 24); this.toolStripStatusLabel1.Text = "就绪"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1042, 645); this.Controls.Add(this.panel1); this.Controls.Add(this.axToolbarControl1); this.Controls.Add(this.menuStrip1); this.MainMenuStrip = this.menuStrip1; this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "供水管网地理信息系统"; this.WindowState = System.Windows.Forms.FormWindowState.Maximized; this.Load += new System.EventHandler(this.Form1_Load); this.menuStrip1.ResumeLayout(false); this.menuStrip1.PerformLayout(); this.panel1.ResumeLayout(false); this.splitContainer1.Panel1.ResumeLayout(false); this.splitContainer1.Panel2.ResumeLayout(false); this.splitContainer1.Panel2.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit(); this.splitContainer1.ResumeLayout(false); this.groupBoxEagleEye.ResumeLayout(false); this.statusStrip1.ResumeLayout(false); this.statusStrip1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.axLicenseControl1)).EndInit(); this.contextMenuStripTOC.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.axMapControl2)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.axTOCControl1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.axMapControl1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.axToolbarControl1)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.MenuStrip menuStrip1; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1; private ESRI.ArcGIS.Controls.AxToolbarControl axToolbarControl1; private System.Windows.Forms.Panel panel1; private System.Windows.Forms.SplitContainer splitContainer1; private ESRI.ArcGIS.Controls.AxMapControl axMapControl1; private System.Windows.Forms.StatusStrip statusStrip1; private ESRI.ArcGIS.Controls.AxLicenseControl axLicenseControl1; private ESRI.ArcGIS.Controls.AxTOCControl axTOCControl1; private System.Windows.Forms.ToolStripStatusLabel lblCoordinate; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem2; private System.Windows.Forms.ToolStripTextBox txtRotateAngle; private System.Windows.Forms.GroupBox groupBoxEagleEye; private ESRI.ArcGIS.Controls.AxMapControl axMapControl2; private System.Windows.Forms.ContextMenuStrip contextMenuStripTOC; private System.Windows.Forms.ToolStripMenuItem openAttributeTableToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem3; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem4; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem5; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem6; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem7; private System.Windows.Forms.ToolStripMenuItem 清除选择ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 空间量测ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 面积量测ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 周长量测ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 点坐标量测ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 清除已绘制图形ToolStripMenuItem; private System.Windows.Forms.ToolStripStatusLabel lblResult; private System.Windows.Forms.ToolStripMenuItem 折线周长量测ToolStripMenuItem; private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1; } } 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 ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Display; namespace WaterPipelineGIS2 { public partial class main : Form { private IFeatureLayer _selectedFeatureLayer; private System.Drawing.Point _lastRightClickPosition; private SelectionMode currentMode = SelectionMode.None; private IGraphicsContainer _spatialSelectionGraphics; private IGraphicsContainer _attributeHighlightGraphics; private ToolStripStatusLabel lblStatus; // 添加量测模式枚举 public enum MeasureMode { None, PolygonArea, PolygonPerimeter, PolylineLength, PointCoordinate } // 添加量测相关变量 private MeasureMode currentMeasureMode = MeasureMode.None; private List<IPoint> measurePoints = new List<IPoint>(); private IElement tempMeasureElement; private List<IElement> measurementElements = new List<IElement>(); private bool isMeasuring = false; // 修改后的SelectionMode枚举 public enum SelectionMode { None, Rectangle, Circle, Polygon, Polyline } private void InitializeGraphicsContainers() { try { // 确保地图控件已初始化 if (axMapControl1 == null) return; // 确保ActiveView已初始化 if (axMapControl1.ActiveView == null) { // 尝试刷新视图 axMapControl1.ActiveView.Refresh(); } if (axMapControl1.ActiveView != null) { _spatialSelectionGraphics = axMapControl1.ActiveView.GraphicsContainer; _attributeHighlightGraphics = axMapControl1.ActiveView.GraphicsContainer; } } catch (Exception ex) { MessageBox.Show("初始化图形容器失败: " + ex.Message); } } private void Form1_Load(object sender, EventArgs e) { // 强制创建地图控件 if (!axMapControl1.Created) axMapControl1.CreateControl(); // 延迟初始化图形容器 axMapControl1.ActiveView.Refresh(); InitializeGraphicsContainers(); } public main() { InitializeComponent(); // 确保设计器初始化完成 if (statusStrip1 != null) { lblStatus = new ToolStripStatusLabel(); statusStrip1.Items.Add(lblStatus); lblStatus.Text = "就绪"; } // 添加窗体加载事件处理程序 this.Load += Form1_LoadHandler; // === 修改结束 === // 事件绑定保持不变 axMapControl1.OnMouseMove += axMapControl1_OnMouseMove; axMapControl1.OnMouseDown += axMapControl1_OnMouseDown; axMapControl1.OnDoubleClick += new IMapControlEvents2_Ax_OnDoubleClickEventHandler(axMapControl1_OnDoubleClick); } // === 新增方法 === private void Form1_LoadHandler(object sender, EventArgs e) { // 确保地图控件已创建 if (axMapControl1 != null && !axMapControl1.Created) { axMapControl1.CreateControl(); } // 初始化图形容器 InitializeGraphicsContainers(); // 确保 ActiveView 有效 if (axMapControl1.ActiveView != null) { // 刷新视图以确保图形容器可用 axMapControl1.ActiveView.Refresh(); } } private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e) { try { // 公共坐标显示 double mapX = e.mapX; double mapY = e.mapY; lblCoordinate.Text = "X: {mapX:F3} Y: {mapY:F3}"; // 量测模式预览 if (isMeasuring && measurePoints.Count > 0) { switch (currentMeasureMode) { case MeasureMode.PolygonArea: case MeasureMode.PolygonPerimeter: DrawTempPolygonMeasure(e.x, e.y); break; case MeasureMode.PolylineLength: DrawTempPolylineMeasure(e.x, e.y); break; } } } catch (Exception ex) { Console.WriteLine("鼠标移动错误: " + ex.Message); } } private void toolStripMenuItem2_Click(object sender, EventArgs e) { using (var rotateForm = new RotateForm()) { if (rotateForm.ShowDialog() == DialogResult.OK) { try { axMapControl1.Rotation = rotateForm.RotationAngle; axMapControl1.ActiveView.Refresh(); lblCoordinate.Text += " 旋转角度:" + rotateForm.RotationAngle + "°"; } catch (Exception ex) { MessageBox.Show("旋转失败:" + ex.Message); } } } } private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e) { if (e.button == 2) // 右键 { _lastRightClickPosition = new System.Drawing.Point(e.x, e.y); ITOCControl2 tocControl = (ITOCControl2)axTOCControl1.Object; IBasicMap basicMap = null; ILayer layer = null; object other = Type.Missing; object index = Type.Missing; esriTOCControlItem itemType = esriTOCControlItem.esriTOCControlItemNone; tocControl.HitTest(e.x, e.y, ref itemType, ref basicMap, ref layer, ref other, ref index); if (itemType == esriTOCControlItem.esriTOCControlItemLayer && layer != null) { contextMenuStripTOC.Show(axTOCControl1, e.x, e.y); } } } private void openAttributeTableToolStripMenuItem_Click(object sender, EventArgs e) { ITOCControl2 tocControl = (ITOCControl2)axTOCControl1.Object; IBasicMap basicMap = null; ILayer layer = null; object other = Type.Missing; object index = Type.Missing; esriTOCControlItem itemType = esriTOCControlItem.esriTOCControlItemNone; System.Drawing.Point screenPos = contextMenuStripTOC.PointToClient(Control.MousePosition); System.Drawing.Point controlPos = axTOCControl1.PointToClient(Control.MousePosition); tocControl.HitTest( controlPos.X, controlPos.Y, ref itemType, ref basicMap, ref layer, ref other, ref index ); IFeatureLayer featureLayer = layer as IFeatureLayer; if (featureLayer != null) { _selectedFeatureLayer = featureLayer; AttributeTableForm attrForm = new AttributeTableForm( _selectedFeatureLayer, this ); attrForm.Show(); } } private void SetSelectionSymbol() { ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass(); fillSymbol.Color = GetRgbColor(255, 0, 0); fillSymbol.Style = esriSimpleFillStyle.esriSFSSolid; ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass(); lineSymbol.Color = GetRgbColor(255, 255, 0); lineSymbol.Width = 2; fillSymbol.Outline = lineSymbol; if (_selectedFeatureLayer != null) { IGeoFeatureLayer geoLayer = (IGeoFeatureLayer)_selectedFeatureLayer; ISimpleRenderer renderer = new SimpleRendererClass(); renderer.Symbol = (ISymbol)fillSymbol; geoLayer.Renderer = (IFeatureRenderer)renderer; axMapControl1.ActiveView.Refresh(); } } private IRgbColor GetRgbColor(int r, int g, int b) { IRgbColor color = new RgbColorClass(); color.Red = r; color.Green = g; color.Blue = b; return color; } private IRgbColor GetRgbColor(int r, int g, int b, int alpha) { IRgbColor color = new RgbColorClass(); color.Red = r; color.Green = g; color.Blue = b; color.Transparency = (byte)(255 - alpha); return color; } public void ActivateFeatureLayer(IFeatureLayer featureLayer) { if (featureLayer == null) return; _selectedFeatureLayer = featureLayer; axMapControl1.ActiveView.Refresh(); axTOCControl1.Update(); } public void HighlightAndZoomToFeature(IFeatureLayer featureLayer, int oid) { try { if (featureLayer == null || featureLayer.FeatureClass == null) { MessageBox.Show("图层或要素类无效!"); return; } IFeature feature = featureLayer.FeatureClass.GetFeature(oid); if (feature == null || feature.Shape == null) { MessageBox.Show("要素 OID " + oid + " 不存在或无几何!"); return; } IGeometry geometry = feature.Shape; IEnvelope envelope = geometry.Envelope; if (envelope.IsEmpty || envelope.Width == 0 || envelope.Height == 0) { envelope.Expand(10, 10, true); } else { envelope.Expand(1.5, 1.5, true); } axMapControl1.Extent = envelope; axMapControl1.ActiveView.ScreenDisplay.UpdateWindow(); HighlightGeometry(geometry); } catch (Exception ex) { MessageBox.Show("高亮要素失败: " + ex.Message); } } private void btnRectSelect_Click(object sender, EventArgs e) { currentMeasureMode = MeasureMode.None; // 互斥 currentMode = SelectionMode.Rectangle; axMapControl1.CurrentTool = null; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair; lblStatus.Text = "矩形选择模式"; } private void btnCircleSelect_Click(object sender, EventArgs e) { currentMeasureMode = MeasureMode.None; // 互斥 currentMode = SelectionMode.Circle; axMapControl1.CurrentTool = null; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair; lblStatus.Text = "圆形选择模式"; } private void btnPolygonSelect_Click(object sender, EventArgs e) { currentMeasureMode = MeasureMode.None; // 互斥 currentMode = SelectionMode.Polygon; axMapControl1.CurrentTool = null; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair; lblStatus.Text = "多边形选择模式"; } private void btnLineSelect_Click(object sender, EventArgs e) { currentMeasureMode = MeasureMode.None; // 互斥 currentMode = SelectionMode.Polyline; axMapControl1.CurrentTool = null; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair; lblStatus.Text = "折线选择模式"; } private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) { try { // 量测模式处理 if (isMeasuring) { HandleMeasureMode(e); return; } // 左键点击 if (e.button == 1) { if (currentMode != SelectionMode.None) { HandleSpatialSelection(e); } } } catch (Exception ex) { MessageBox.Show("操作错误: " + ex.Message); } } private void PerformSpatialSelection(IGeometry geometry) { try { IMap map = axMapControl1.Map; if (map == null) return; map.ClearSelection(); for (int i = 0; i < map.LayerCount; i++) { IFeatureLayer featureLayer = map.get_Layer(i) as IFeatureLayer; if (featureLayer == null || !featureLayer.Valid || featureLayer.FeatureClass == null) continue; ISpatialFilter filter = new SpatialFilterClass(); filter.Geometry = geometry; filter.GeometryField = featureLayer.FeatureClass.ShapeFieldName; filter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; IFeatureSelection selection = (IFeatureSelection)featureLayer; selection.SelectFeatures(filter, esriSelectionResultEnum.esriSelectionResultNew, false); } axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null); axMapControl1.ActiveView.Refresh(); } catch (Exception ex) { MessageBox.Show("空间查询失败: " + ex.Message); } } private void HighlightGeometry(IGeometry geometry) { try { if (_spatialSelectionGraphics != null) { _spatialSelectionGraphics.DeleteAllElements(); } IElement element = null; switch (geometry.GeometryType) { case esriGeometryType.esriGeometryPolygon: { ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass(); fillSymbol.Color = GetRgbColor(255, 0, 0, 80); ISimpleLineSymbol outline = new SimpleLineSymbolClass(); outline.Color = GetRgbColor(255, 0, 0); outline.Width = 2; fillSymbol.Outline = outline; element = new PolygonElementClass(); element.Geometry = geometry; ((IFillShapeElement)element).Symbol = fillSymbol; } break; case esriGeometryType.esriGeometryPolyline: { ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass(); lineSymbol.Color = GetRgbColor(255, 0, 0); lineSymbol.Width = 3; element = new LineElementClass(); element.Geometry = geometry; ((ILineElement)element).Symbol = lineSymbol; } break; case esriGeometryType.esriGeometryPoint: { ISimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbolClass(); markerSymbol.Color = GetRgbColor(255, 0, 0); markerSymbol.Size = 12; element = new MarkerElementClass(); element.Geometry = geometry; ((IMarkerElement)element).Symbol = markerSymbol; } break; } if (element != null && _spatialSelectionGraphics != null) { _spatialSelectionGraphics.AddElement(element, 0); axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); } } catch (Exception ex) { MessageBox.Show("高亮显示失败: " + ex.Message); } } private void btnClearSelection_Click(object sender, EventArgs e) { try { if (axMapControl1.Map != null) { axMapControl1.Map.ClearSelection(); } if (_spatialSelectionGraphics != null) { _spatialSelectionGraphics.DeleteAllElements(); } axMapControl1.ActiveView.PartialRefresh( esriViewDrawPhase.esriViewGeoSelection | esriViewDrawPhase.esriViewGraphics, null, null ); axMapControl1.ActiveView.Refresh(); currentMode = SelectionMode.None; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault; lblStatus.Text = "就绪"; } catch (Exception ex) { MessageBox.Show("清除选择失败: " + ex.Message); } } private void axMapControl1_OnDoubleClick(object sender, IMapControlEvents2_OnDoubleClickEvent e) { if (currentMode != SelectionMode.None) { currentMode = SelectionMode.None; axMapControl1.CurrentTool = null; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault; lblStatus.Text = "就绪"; } } private void HandleSpatialSelection(IMapControlEvents2_OnMouseDownEvent e) { IGeometry geometry = null; switch (currentMode) { case SelectionMode.Rectangle: IEnvelope rectEnv = axMapControl1.TrackRectangle(); if (rectEnv != null) { IPolygon polygon = new PolygonClass(); polygon.SpatialReference = axMapControl1.SpatialReference; ISegmentCollection segColl = (ISegmentCollection)polygon; segColl.SetRectangle(rectEnv); geometry = (IGeometry)polygon; } break; case SelectionMode.Circle: IEnvelope envelope = axMapControl1.TrackRectangle(); if (envelope.Width <= 0 || envelope.Height <= 0) { MessageBox.Show("请拖拽有效的矩形范围以创建圆形!"); return; } IPoint center = new PointClass(); center.PutCoords( (envelope.XMin + envelope.XMax) / 2, (envelope.YMin + envelope.YMax) / 2 ); double radius = Math.Max(envelope.Width, envelope.Height) / 2; ICircularArc circularArc = new CircularArcClass(); IConstructCircularArc constructArc = (IConstructCircularArc)circularArc; constructArc.ConstructCircle(center, radius, true); ISegmentCollection circleSegColl = new PolygonClass(); circleSegColl.AddSegment((ISegment)circularArc); geometry = (IGeometry)circleSegColl; geometry.SpatialReference = axMapControl1.SpatialReference; break; case SelectionMode.Polygon: geometry = axMapControl1.TrackPolygon(); break; case SelectionMode.Polyline: geometry = axMapControl1.TrackLine(); break; } if (geometry != null) { PerformSpatialSelection(geometry); HighlightGeometry(geometry); } } // 创建折线几何 private IPolyline CreatePolyline(List<IPoint> points, IPoint currentPoint = null) { try { // 创建点集合 IPointCollection pointCollection = new PolylineClass(); // 添加所有点 foreach (IPoint point in points) { if (point != null) { pointCollection.AddPoint(point); } } // 添加当前鼠标位置 if (currentPoint != null) { pointCollection.AddPoint(currentPoint); } // 转换为折线 IPolyline polyline = pointCollection as IPolyline; if (polyline == null) return null; // 设置空间参考(关键!) if (axMapControl1.SpatialReference != null) { polyline.SpatialReference = axMapControl1.SpatialReference; } // 简化几何 ITopologicalOperator topoOp = polyline as ITopologicalOperator; if (topoOp != null) { topoOp.Simplify(); } return polyline; } catch (Exception ex) { MessageBox.Show("创建折线时出错: " + ex.Message); return null; } } // 创建多边形几何 private IPolygon CreatePolygon(List<IPoint> points, IPoint currentPoint = null) { try { // 创建点集合 IPointCollection pointCollection = new PolygonClass(); // 添加所有点 foreach (IPoint point in points) { if (point != null) { pointCollection.AddPoint(point); } } // 添加当前鼠标位置 if (currentPoint != null) { pointCollection.AddPoint(currentPoint); } // 转换为多边形 IPolygon polygon = pointCollection as IPolygon; if (polygon == null) return null; // 设置空间参考(关键!) if (axMapControl1.SpatialReference != null) { polygon.SpatialReference = axMapControl1.SpatialReference; } // 简化几何 ITopologicalOperator topoOp = polygon as ITopologicalOperator; if (topoOp != null) { topoOp.Simplify(); } return polygon; } catch (Exception ex) { MessageBox.Show("创建多边形时出错: " + ex.Message); return null; } } // 量测菜单项点击事件处理 private void 面积量测ToolStripMenuItem_Click(object sender, EventArgs e) { StartMeasureMode(MeasureMode.PolygonArea); } private void 周长量测ToolStripMenuItem_Click(object sender, EventArgs e) { StartMeasureMode(MeasureMode.PolygonPerimeter); } private void 折线周长量测ToolStripMenuItem_Click(object sender, EventArgs e) { StartMeasureMode(MeasureMode.PolylineLength); } private void 点坐标量测ToolStripMenuItem_Click(object sender, EventArgs e) { StartMeasureMode(MeasureMode.PointCoordinate); } // 开始量测模式 private void StartMeasureMode(MeasureMode mode) { currentMode = SelectionMode.None; // 互斥 // 清除之前的状态 EndMeasureMode(); currentMeasureMode = mode; isMeasuring = true; measurePoints.Clear(); // 设置鼠标指针 axMapControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair; // 设置状态栏提示 switch (mode) { case MeasureMode.PolygonArea: lblStatus.Text = "面积量测: 左键添加顶点,右键完成"; break; case MeasureMode.PolygonPerimeter: lblStatus.Text = "周长量测: 左键添加顶点,右键完成"; break; case MeasureMode.PolylineLength: lblStatus.Text = "长度量测: 左键添加顶点,右键完成"; break; case MeasureMode.PointCoordinate: lblStatus.Text = "点坐标量测: 左键点击地图获取坐标"; break; } } // 结束量测模式 private void EndMeasureMode() { ClearTempMeasureElement(); measurePoints.Clear(); isMeasuring = false; currentMeasureMode = MeasureMode.None; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault; lblStatus.Text = "就绪"; } // 绘制临时多边形量测图形 private void DrawTempPolygonMeasure(int x, int y) { if (measurePoints.Count == 0) return; try { // 清除之前的临时图形 ClearTempMeasureElement(); // 获取当前鼠标位置 IPoint currentPoint = axMapControl1.ToMapPoint(x, y); // 创建临时多边形(包括当前鼠标位置) IPolygon polygon = CreatePolygon(measurePoints, currentPoint); // 创建符号(蓝色边框,半透明填充) ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass(); lineSymbol.Color = GetRgbColor(0, 0, 255); // 蓝色 lineSymbol.Width = 2; ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass(); fillSymbol.Outline = lineSymbol; fillSymbol.Color = GetRgbColor(0, 255, 255, 100); // 青色半透明 // 创建元素 tempMeasureElement = new PolygonElementClass(); tempMeasureElement.Geometry = polygon; ((IFillShapeElement)tempMeasureElement).Symbol = fillSymbol; // 添加到地图 IGraphicsContainer graphics = axMapControl1.Map as IGraphicsContainer; if (graphics != null) { graphics.AddElement(tempMeasureElement, 0); axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); } } catch (Exception ex) { Console.WriteLine("量测预览错误: " + ex.Message); } } // 绘制临时折线量测图形 private void DrawTempPolylineMeasure(int x, int y) { if (measurePoints.Count == 0) return; try { // 清除之前的临时图形 ClearTempMeasureElement(); // 获取当前鼠标位置 IPoint currentPoint = axMapControl1.ToMapPoint(x, y); // 创建临时折线(包括当前鼠标位置) IPolyline polyline = CreatePolyline(measurePoints, currentPoint); // 创建符号(红色粗线) ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass(); lineSymbol.Color = GetRgbColor(255, 0, 0); // 红色 lineSymbol.Width = 3; // 创建元素 tempMeasureElement = new LineElementClass(); tempMeasureElement.Geometry = polyline; ((ILineElement)tempMeasureElement).Symbol = lineSymbol; // 添加到地图 IGraphicsContainer graphics = axMapControl1.Map as IGraphicsContainer; if (graphics != null) { graphics.AddElement(tempMeasureElement, 0); axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); } } catch (Exception ex) { Console.WriteLine("量测预览错误: " + ex.Message); } } // 清除临时量测图形 private void ClearTempMeasureElement() { if (tempMeasureElement != null) { IGraphicsContainer graphics = axMapControl1.Map as IGraphicsContainer; if (graphics != null) { graphics.DeleteElement(tempMeasureElement); axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); } tempMeasureElement = null; } } // 处理量测模式 private void HandleMeasureMode(IMapControlEvents2_OnMouseDownEvent e) { try { // 获取当前鼠标位置 IPoint point = axMapControl1.ToMapPoint(e.x, e.y); // 左键添加点 if (e.button == 1) { switch (currentMeasureMode) { case MeasureMode.PolygonArea: case MeasureMode.PolygonPerimeter: case MeasureMode.PolylineLength: measurePoints.Add(point); break; case MeasureMode.PointCoordinate: CompletePointMeasure(point); break; } } // 右键完成量测 else if (e.button == 2) { switch (currentMeasureMode) { case MeasureMode.PolygonArea: CompletePolygonMeasure(true); break; case MeasureMode.PolygonPerimeter: CompletePolygonMeasure(false); break; case MeasureMode.PolylineLength: CompletePolylineMeasure(); break; } } } catch (Exception ex) { MessageBox.Show("量测错误: " + ex.Message); } } // 完成多边形量测 private void CompletePolygonMeasure(bool isArea) { if (measurePoints.Count < 3) { MessageBox.Show("多边形至少需要3个顶点"); return; } try { // 创建最终多边形 IPolygon polygon = CreatePolygon(measurePoints); if (polygon == null || polygon.IsEmpty) { MessageBox.Show("无法创建有效多边形"); return; } // 计算量测结果 IArea area = polygon as IArea; double result = 0; string unit = ""; string title = ""; if (isArea) { // 面积量测 result = area.Area; // 平方米 unit = "平方米"; title = "面积量测结果"; } else { // 周长量测 ICurve curve = polygon as ICurve; result = curve.Length; // 米 unit = "米"; title = "周长量测结果"; } // 构建结果消息 string message = string.Format("{0:0.##} {1}", result, unit); if (isArea) { double hectareArea = result / 10000; // 公顷 message += string.Format("\n{0:0.##} 公顷", hectareArea); } else { double kilometer = result / 1000; // 千米 message += string.Format("\n{0:0.##} 千米", kilometer); } // 使用自定义窗体显示结果 ShowMeasureResult(title, message); // 绘制最终量测图形 DrawFinalMeasureElement(polygon, isArea); } catch (Exception ex) { MessageBox.Show("量测出错: " + ex.Message); } finally { // 结束量测模式 EndMeasureMode(); } } // 完成折线量测 private void CompletePolylineMeasure() { if (measurePoints.Count < 2) { MessageBox.Show("折线至少需要2个顶点"); return; } try { // 创建最终折线 IPolyline polyline = CreatePolyline(measurePoints); if (polyline == null || polyline.IsEmpty) { MessageBox.Show("无法创建有效折线"); return; } // 计算长度 ICurve curve = polyline as ICurve; double length = curve.Length; // 米 double kilometer = length / 1000; // 千米 // 构建结果消息 string message = string.Format("{0:0.##} 米\n{1:0.##} 千米", length, kilometer); // 使用自定义窗体显示结果 ShowMeasureResult("长度量测结果", message); // 绘制最终量测图形 DrawFinalMeasureElement(polyline); } catch (Exception ex) { MessageBox.Show("量测出错: " + ex.Message); } finally { // 结束量测模式 EndMeasureMode(); } } // 完成点坐标量测 private void CompletePointMeasure(IPoint point) { try { // 获取坐标系信息 string coordSystem = "未知坐标系"; if (axMapControl1.SpatialReference != null) { coordSystem = axMapControl1.SpatialReference.Name; } // 构建结果消息 string message = string.Format("X: {0:0.###}\nY: {1:0.###}\n\n坐标系: {2}", point.X, point.Y, coordSystem); // 使用自定义窗体显示结果 ShowMeasureResult("坐标量测结果", message); // 绘制点标记 DrawPointMarker(point); } catch (Exception ex) { MessageBox.Show("量测出错: " + ex.Message); } finally { // 结束量测模式 EndMeasureMode(); } } // 绘制最终量测图形 private void DrawFinalMeasureElement(IGeometry geometry, bool isArea = true) { IGraphicsContainer graphics = axMapControl1.Map as IGraphicsContainer; if (graphics == null) return; IElement element = null; if (geometry is IPolygon) { // 创建符号(蓝色边框,半透明填充) ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass(); lineSymbol.Color = GetRgbColor(0, 0, 255); // 蓝色 lineSymbol.Width = 2; ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass(); fillSymbol.Outline = lineSymbol; fillSymbol.Color = GetRgbColor(0, 255, 255, 100); // 青色半透明 // 创建元素 element = new PolygonElementClass(); element.Geometry = geometry; ((IFillShapeElement)element).Symbol = fillSymbol; } else if (geometry is IPolyline) { // 创建符号(红色粗线) ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass(); lineSymbol.Color = GetRgbColor(255, 0, 0); // 红色 lineSymbol.Width = 3; // 创建元素 element = new LineElementClass(); element.Geometry = geometry; ((ILineElement)element).Symbol = lineSymbol; } if (element != null) { // 添加到地图 graphics.AddElement(element, 0); measurementElements.Add(element); axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); } } // 绘制点标记 private void DrawPointMarker(IPoint point) { IGraphicsContainer graphics = axMapControl1.Map as IGraphicsContainer; if (graphics == null) return; // 创建符号(绿色十字) ISimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbolClass(); markerSymbol.Style = esriSimpleMarkerStyle.esriSMSCross; markerSymbol.Color = GetRgbColor(0, 255, 0); // 绿色 markerSymbol.Size = 12; markerSymbol.Outline = true; markerSymbol.OutlineColor = GetRgbColor(0, 0, 0); // 黑色边框 markerSymbol.OutlineSize = 1; // 创建元素 IMarkerElement markerElement = new MarkerElementClass(); markerElement.Symbol = markerSymbol; ((IElement)markerElement).Geometry = point; // 添加到地图 graphics.AddElement((IElement)markerElement, 0); measurementElements.Add((IElement)markerElement); axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); } // 清除已绘制图形 private void 清除已绘制图形ToolStripMenuItem_Click(object sender, EventArgs e) { ClearMeasurementGraphics(); } // 清除量测图形 private void ClearMeasurementGraphics() { try { IActiveView activeView = axMapControl1.ActiveView; IGraphicsContainer graphics = activeView.GraphicsContainer; if (graphics == null) return; // 删除所有量测图形元素 foreach (IElement element in measurementElements) { graphics.DeleteElement(element); } // 清空列表 measurementElements.Clear(); // 强制刷新地图视图 activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); activeView.Refresh(); lblStatus.Text = "已清除所有量测图形"; } catch (Exception ex) { MessageBox.Show("清除图形时出错: " + ex.Message); } } // 在 Form1 类中添加新方法 private void ShowMeasureResult(string title, string message) { using (MeasureResultForm resultForm = new MeasureResultForm(title, message)) { resultForm.ShowDialog(this); } } } }此代码我想实现鹰眼导航(即在此代码中,mapcontrol控件(axMapControl1)中显示的是主地图,而左下角的mapcontrol控件(axMapControl2)进行鹰眼导航,axMapControl1中的视野范围在axMapControl2中用深蓝色边框进行框起来。无论是主视图大小缩放还是通过鼠标移动视角,在axMapControl2都要跟着框起来,他固定不动但是蓝色边框跟着移动)注意我的vs版本为2010,给出完整的修改代码(包括designer.cs),要细致的修改操作教程
最新发布
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值