axMapControl1_OnMouseDown

本文介绍了一种基于鼠标操作的地图绘制方法,通过捕捉用户的鼠标点击来创建并更新地图上的线要素。当用户点击鼠标左键时,系统会在指定位置放置一个点,并开始绘制一条新的线;继续点击则会在线上增加新的顶点。若点击的是鼠标右键或中键,则系统会调整视图,以用户定义的矩形区域为中心进行缩放。

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


private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
//If left hand mouse button 如果点击鼠标左键
if (e.button == 1)
{
//Create a point and grab hold of the IPoint inteface 实例化一个点,并将实例化的点转化成IPoint 接口
IPoint point = new PointClass();
//Set point properties 设置点的属性
point.X = e.mapX;
point.Y = e.mapY;

//If this is the first point of a new line 如果这个点是新线的起始点
if (m_Polyline == null)
{
//Create the forms private polyline member and grab hold of the IPolyline interface 创新一个Polyline对象,并将Polyline对象转化成IPolyline接口(m_Polyline)。
m_Polyline = new PolylineClass();
}

//QI for the IPointsCollection interface using the IPolyline interface 将IPolyline接口强制转化成一个IPointsCollection接口进行操作。
object o = Type.Missing;
//object o1 = m_PointCollection.PointCount-1;
m_PointCollection = (IPointCollection) m_Polyline;
m_PointCollection.AddPoint(point, ref o, ref o);

//Refresh the foreground thereby removing any text annotation 刷新地图的显示,以屏蔽文本注释。
axMapControl1.Refresh(esriViewDrawPhase.esriViewForeground, Type.Missing,Type.Missing);
}
else
{
//If right or middle mouse button zoom to user defined rectangle 如果点击了鼠标右键或中键,则去自定义矩形
//Create an envelope and grab hold of the IEnvelope interface 将axMapControl1.TrackRectangle转化成 IEnvelope接口
IEnvelope envelope = axMapControl1.TrackRectangle();
//If user dragged a rectangle 如果用户拖动了一个矩形。
if (envelope != null)
{
//Set map controls extent property 设置地图控件 Extent(范围)属性。
axMapControl1.Extent = envelope;
}
}
}
本贴来自ZDNetChina中文社区 http://bbs.zdnet.com.cn ,本贴地址:http://bbs.zdnet.com.cn/viewthread.php?tid=726219

 

 

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
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; private enum SelectionMode { None, Rectangle, Circle, Polygon, Polyline } private SelectionMode currentMode = SelectionMode.None; private enum MeasureMode { None, Area, Length, Point } private MeasureMode currentMeasureMode = MeasureMode.None; private IGeometry tempGeometry; private IPointCollection pointCollection; private Label lblMeasureResult; private IGraphicsContainer _spatialSelectionGraphics; // 空间选择图形容器 private IGraphicsContainer _attributeHighlightGraphics; // 属性表高亮图形容器 private void InitializeGraphicsContainers() { // 初始化空间选择图形容器 _spatialSelectionGraphics = axMapControl1.ActiveView.GraphicsContainer; // 初始化属性表高亮图形容器 _attributeHighlightGraphics = axMapControl1.ActiveView.GraphicsContainer; } public Form1() { InitializeComponent(); InitializeGraphicsContainers(); // 新增初始化 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; // 获取当前鼠标位置(屏幕坐标系) 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 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 void ActivateFeatureLayer(IFeatureLayer featureLayer) { if (featureLayer == null) return; // 1. 设置当前活动图层 _selectedFeatureLayer = featureLayer; // 2. 刷新地图显示(可选) axMapControl1.ActiveView.Refresh(); // 3. 更新TOC控件的选中状态(可选) axTOCControl1.Update(); } // 修改 HighlightAndZoomToFeature 方法实现 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}"); } } public object _featureLayer { get; set; } // 矩形选择 private void btnRectSelect_Click(object sender, EventArgs e) { currentMode = SelectionMode.Rectangle; axMapControl1.CurrentTool = null; } // 圆形选择 private void btnCircleSelect_Click(object sender, EventArgs e) { currentMode = SelectionMode.Circle; axMapControl1.CurrentTool = null; } // 多边形选择 private void btnPolygonSelect_Click(object sender, EventArgs e) { currentMode = SelectionMode.Polygon; axMapControl1.CurrentTool = null; } // 折线选择 private void btnLineSelect_Click(object sender, EventArgs e) { currentMode = SelectionMode.Polyline; axMapControl1.CurrentTool = null; } // 地图鼠标按下事件 private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) { try { IGeometry geometry = null; switch (currentMode) { case SelectionMode.Rectangle: // 绘制矩形(返回的是IEnvelope) geometry = axMapControl1.TrackRectangle(); 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 segColl = new PolygonClass(); segColl.AddSegment((ISegment)circularArc); geometry = (IGeometry)segColl; 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); } } catch (Exception ex) { MessageBox.Show("操作失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } // 执行空间选择 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; // 检查几何类型是否支持空间查询 if (geometry.GeometryType == esriGeometryType.esriGeometryPoint && featureLayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryPoint) continue; ISpatialFilter filter = new SpatialFilterClass { Geometry = geometry, GeometryField = featureLayer.FeatureClass.ShapeFieldName, SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects }; IFeatureSelection selection = (IFeatureSelection)featureLayer; selection.SelectFeatures(filter, esriSelectionResultEnum.esriSelectionResultNew, false); } axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null); } catch (Exception ex) { MessageBox.Show("空间查询失败: {ex.Message}"); } } // 高亮显示选择图形(修复版) private void HighlightGeometry(IGeometry geometry) { try { // 清除旧的空间选择图形 _spatialSelectionGraphics.DeleteAllElements(); // 创建符号和元素 IElement element = null; ISymbol symbol = null; switch (geometry.GeometryType) { case esriGeometryType.esriGeometryPolygon: ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass(); fillSymbol.Color = GetRgbColor(255, 0, 0, 80); // 半透明红色填充 fillSymbol.Outline = new SimpleLineSymbolClass { Color = GetRgbColor(255, 0, 0), Width = 2 }; symbol = (ISymbol)fillSymbol; element = new PolygonElementClass(); break; case esriGeometryType.esriGeometryPolyline: ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass(); lineSymbol.Color = GetRgbColor(255, 0, 0); lineSymbol.Width = 3; symbol = (ISymbol)lineSymbol; element = new LineElementClass(); break; case esriGeometryType.esriGeometryPoint: ISimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbolClass(); markerSymbol.Color = GetRgbColor(255, 0, 0); markerSymbol.Size = 12; symbol = (ISymbol)markerSymbol; element = new MarkerElementClass(); break; } if (element != null) { element.Geometry = geometry; _spatialSelectionGraphics.AddElement(element, 0); axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); } } catch (Exception ex) { MessageBox.Show("高亮显示失败: {ex.Message}"); } } private IColor GetRgbColor(int p, int p_2, int p_3, int p_4) { throw new NotImplementedException(); } // 清除选择 private void btnClearSelection_Click(object sender, EventArgs e) { axMapControl1.Map.ClearSelection(); IGraphicsContainer graphicsContainer = axMapControl1.Map as IGraphicsContainer; graphicsContainer.DeleteAllElements(); axMapControl1.Refresh(); } // 新增双击事件处理 private void axMapControl1_OnDoubleClick(object sender, IMapControlEvents2_OnDoubleClickEvent e) { if (currentMode != SelectionMode.None) { currentMode = SelectionMode.None; axMapControl1.CurrentTool = null; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault; } } // 添加量测按钮点击事件 private void btnMeasureArea_Click(object sender, EventArgs e) { ResetMeasureMode(MeasureMode.Area); lblMeasureResult.Text = "状态:绘制多边形(双击结束)"; } private void btnMeasureLength_Click(object sender, EventArgs e) { ResetMeasureMode(MeasureMode.Length); lblMeasureResult.Text = "状态:绘制折线(双击结束)"; } private void btnMeasurePoint_Click(object sender, EventArgs e) { ResetMeasureMode(MeasureMode.Point); lblMeasureResult.Text = "状态:移动鼠标查看坐标"; } private void ResetMeasureMode(MeasureMode mode) { currentMeasureMode = mode; ClearTempGraphics(); axMapControl1.CurrentTool = null; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair; } private void ClearTempGraphics() { throw new NotImplementedException(); } // 修改后的地图鼠标事件处理 private void axMapControl1_OnMouseDown2(object sender, IMapControlEvents2_OnMouseDownEvent e) { try { if (currentMeasureMode == MeasureMode.None) return; IPoint currentPoint = axMapControl1.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x, e.y); switch (currentMeasureMode) { case MeasureMode.Area: case MeasureMode.Length: if (pointCollection == null) { // 🟢 根据模式创建不同几何类型 pointCollection = currentMeasureMode == MeasureMode.Area ? (IPointCollection)new PolygonClass() : (IPointCollection)new PolylineClass(); pointCollection.AddPoint(currentPoint); } pointCollection.AddPoint(currentPoint); DrawTempGeometry(); break; } } catch (Exception ex) { MessageBox.Show("操作错误:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void DrawTempGeometry() { throw new NotImplementedException(); } l 0 } }空间选择绘制后,可也绘制选择,但是怎么除了矩形都在选择后报错,并且没有了绘制后的红色区域,给出完整的修改后的代码,不要省略!全部都给我
07-13
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; private enum SelectionMode { None, Rectangle, Circle, Polygon, Polyline } private SelectionMode currentMode = SelectionMode.None; private enum MeasureMode { None, Area, Length, Point } private MeasureMode currentMeasureMode = MeasureMode.None; private IGeometry tempGeometry; private IPointCollection pointCollection; private ToolStripStatusLabel lblMeasureResult; // 修改为ToolStripStatusLabel private IGraphicsContainer _spatialSelectionGraphics; private IGraphicsContainer _attributeHighlightGraphics; private void InitializeGraphicsContainers() { try { 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) { InitializeGraphicsContainers(); // 初始化量测结果标签 lblMeasureResult = new ToolStripStatusLabel(); statusStrip1.Items.Add(lblMeasureResult); } public Form1() { InitializeComponent(); // 确保设计器初始化完成 if (statusStrip1 != null) { lblMeasureResult = new ToolStripStatusLabel(); statusStrip1.Items.Add(lblMeasureResult); } InitializeGraphicsContainers(); axMapControl1.OnMouseMove += new IMapControlEvents2_Ax_OnMouseMoveEventHandler(axMapControl1_OnMouseMove); axMapControl1.OnMouseDown += new IMapControlEvents2_Ax_OnMouseDownEventHandler(axMapControl1_OnMouseDown); axMapControl1.OnDoubleClick += new IMapControlEvents2_Ax_OnDoubleClickEventHandler(axMapControl1_OnDoubleClick); } private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e) { double mapX = e.mapX; double mapY = e.mapY; lblCoordinate.Text = "X: " + mapX.ToString("F3") + " Y: " + mapY.ToString("F3"); 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; 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) { currentMode = SelectionMode.Rectangle; axMapControl1.CurrentTool = null; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair; } private void btnCircleSelect_Click(object sender, EventArgs e) { currentMode = SelectionMode.Circle; axMapControl1.CurrentTool = null; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair; } private void btnPolygonSelect_Click(object sender, EventArgs e) { currentMode = SelectionMode.Polygon; axMapControl1.CurrentTool = null; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair; } private void btnLineSelect_Click(object sender, EventArgs e) { currentMode = SelectionMode.Polyline; axMapControl1.CurrentTool = null; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair; } private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) { try { if (currentMode != SelectionMode.None) { HandleSpatialSelection(e); return; } if (currentMeasureMode != MeasureMode.None) { HandleMeasurement(e); } } catch (Exception ex) { MessageBox.Show("操作错误:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } 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(); } ClearTempGraphics(); axMapControl1.ActiveView.PartialRefresh( esriViewDrawPhase.esriViewGeoSelection | esriViewDrawPhase.esriViewGraphics, null, null ); axMapControl1.ActiveView.Refresh(); currentMode = SelectionMode.None; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault; currentMeasureMode = MeasureMode.None; } 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; } if (currentMeasureMode == MeasureMode.Area || currentMeasureMode == MeasureMode.Length) { if (pointCollection != null && pointCollection.PointCount > 1) { ShowMeasureResult(); pointCollection = null; currentMeasureMode = MeasureMode.None; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault; } } } private void btnMeasureArea_Click(object sender, EventArgs e) { ResetMeasureMode(MeasureMode.Area); if (lblMeasureResult != null) lblMeasureResult.Text = "状态:绘制多边形(双击结束)"; } private void btnMeasureLength_Click(object sender, EventArgs e) { ResetMeasureMode(MeasureMode.Length); if (lblMeasureResult != null) lblMeasureResult.Text = "状态:绘制折线(双击结束)"; } private void btnMeasurePoint_Click(object sender, EventArgs e) { ResetMeasureMode(MeasureMode.Point); if (lblMeasureResult != null) lblMeasureResult.Text = "状态:移动鼠标查看坐标"; } private void ResetMeasureMode(MeasureMode mode) { currentMeasureMode = mode; ClearTempGraphics(); axMapControl1.CurrentTool = null; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair; } private void ClearTempGraphics() { try { if (_spatialSelectionGraphics != null) { _spatialSelectionGraphics.DeleteAllElements(); axMapControl1.ActiveView.PartialRefresh( esriViewDrawPhase.esriViewGraphics, null, null ); } pointCollection = null; } catch (Exception ex) { MessageBox.Show("清除临时图形失败: " + ex.Message); } } 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 void HandleMeasurement(IMapControlEvents2_OnMouseDownEvent e) { if (currentMeasureMode == MeasureMode.None) return; IPoint currentPoint = axMapControl1.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x, e.y); switch (currentMeasureMode) { case MeasureMode.Area: case MeasureMode.Length: if (pointCollection == null) { pointCollection = currentMeasureMode == MeasureMode.Area ? (IPointCollection)new PolygonClass() : (IPointCollection)new PolylineClass(); pointCollection.AddPoint(currentPoint); } pointCollection.AddPoint(currentPoint); DrawTempGeometry(); break; } } private void DrawTempGeometry() { if (pointCollection == null) return; ClearTempGraphics(); IElement element = null; if (pointCollection is IPolygon) { ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass(); fillSymbol.Color = GetRgbColor(0, 255, 0, 50); ISimpleLineSymbol outline = new SimpleLineSymbolClass(); outline.Color = GetRgbColor(0, 255, 0); outline.Width = 2; fillSymbol.Outline = outline; element = new PolygonElementClass(); element.Geometry = (IGeometry)pointCollection; ((IFillShapeElement)element).Symbol = fillSymbol; } else if (pointCollection is IPolyline) { ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass(); lineSymbol.Color = GetRgbColor(0, 255, 0); lineSymbol.Width = 2; element = new LineElementClass(); element.Geometry = (IGeometry)pointCollection; ((ILineElement)element).Symbol = lineSymbol; } if (element != null && _spatialSelectionGraphics != null) { _spatialSelectionGraphics.AddElement(element, 0); axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); } } private void ShowMeasureResult() { try { if (pointCollection == null) return; double result = 0; string unit = ""; ISpatialReference spatialRef = axMapControl1.SpatialReference; if (pointCollection is IPolygon) { IPolygon polygon = pointCollection as IPolygon; ITopologicalOperator topoOp = polygon as ITopologicalOperator; topoOp.Simplify(); IArea area = polygon as IArea; result = area.Area; unit = "平方米"; } else if (pointCollection is IPolyline) { IPolyline polyline = pointCollection as IPolyline; ICurve curve = polyline as ICurve; result = curve.Length; unit = "米"; } else { return; } if (lblMeasureResult != null) lblMeasureResult.Text = "测量结果: " + result.ToString("F2") + " " + unit; } catch (Exception ex) { MessageBox.Show("计算量测结果失败: " + ex.Message); } } } }点击空间量测后怎么进行量测,我怎么右击mapcontrol控件(axMapControl1)不进行绘制量测呢,给出修改后的完整代码,不要省略任何部分,注意vs版本为2010
07-13
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); } } } }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; } } 此代码我想实现鹰眼导航(即在此代码中,mapcontrol控件(axMapControl1)中显示的是主地图,而左下角的mapcontrol控件(axMapControl2)进行鹰眼导航,axMapControl1中的视野范围在axMapControl2中用深蓝色边框进行框起来。无论是主视图大小缩放还是通过鼠标移动视角,在axMapControl2都要跟着框起来,他固定不动但是蓝色边框跟着移动)注意我的vs版本为2010,ArcGIS Engine 10.2给出修改后的不省略的修改代码,要细致的修改操作教程
最新发布
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值