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;
public Form1()
{
InitializeComponent();
axMapControl1.OnMouseMove += new IMapControlEvents2_Ax_OnMouseMoveEventHandler(axMapControl1_OnMouseMove);
}
private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
{
// 获取地图坐标
double mapX = e.mapX;
double mapY = e.mapY;
// 格式化坐标显示(保留3位小数)
lblCoordinate.Text = string.Format("X: {0:F3} Y: {1:F3}", mapX, mapY);
// 立即刷新状态栏
statusStrip1.Refresh();
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
using (var rotateForm = new RotateForm())
{
if (rotateForm.ShowDialog() == DialogResult.OK)
{
try
{
axMapControl1.Rotation = rotateForm.RotationAngle;
axMapControl1.ActiveView.Refresh();
// 可选:更新状态栏
lblCoordinate.Text += " 旋转角度:{rotateForm.RotationAngle}°";
}
catch (Exception ex)
{
MessageBox.Show("旋转失败:{ex.Message}");
}
}
}
}
private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
if (e.button == 2) // 右键
{
// 保存点击位置(控件坐标系)
_lastRightClickPosition = new System.Drawing.Point(e.x, e.y);
ITOCControl2 tocControl = (ITOCControl2)axTOCControl1.Object;
// 修改点1:声明为接口类型并初始化为null
IBasicMap basicMap = null;
ILayer layer = null;
// 修改点2:使用Type.Missing代替new object()
object other = Type.Missing;
object index = Type.Missing;
esriTOCControlItem itemType = esriTOCControlItem.esriTOCControlItemNone;
// 修改点3:正确传递ref参数
tocControl.HitTest(e.x, e.y, ref itemType, ref basicMap, ref layer, ref other, ref index);
if (itemType == esriTOCControlItem.esriTOCControlItemLayer && layer != null)
{
contextMenuStripTOC.Show(axTOCControl1, e.x, e.y);
}
}
}
// 修改后(使用 MouseEventArgs)
private void openAttributeTableToolStripMenuItem_Click(object sender, EventArgs e)
{
ITOCControl2 tocControl = (ITOCControl2)axTOCControl1.Object;
IBasicMap basicMap = null;
ILayer layer = null;
object other = Type.Missing;
object index = Type.Missing;
esriTOCControlItem itemType = esriTOCControlItem.esriTOCControlItemNone;
// 使用保存的控件坐标系位置
tocControl.HitTest(
_lastRightClickPosition.X,
_lastRightClickPosition.Y,
ref itemType,
ref basicMap,
ref layer,
ref other,
ref index
);
IFeatureLayer featureLayer = layer as IFeatureLayer;
if (featureLayer != null)
{
_selectedFeatureLayer = featureLayer;
// 确保使用正确的构造函数
AttributeTableForm attrForm = new AttributeTableForm(
_selectedFeatureLayer,
axMapControl1.Object as IMapControl2
);
attrForm.Show();
}
else
{
MessageBox.Show("请选择有效的要素图层!");
}
}
private void SetSelectionSymbol()
{
// 使用接口创建符号
ISimpleFillSymbol fillSymbol = new SimpleFillSymbol() as ISimpleFillSymbol;
fillSymbol.Color = GetRgbColor(255, 0, 0);
fillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
ISimpleLineSymbol lineSymbol = new SimpleLineSymbol() as ISimpleLineSymbol;
lineSymbol.Color = GetRgbColor(255, 255, 0);
lineSymbol.Width = 2;
fillSymbol.Outline = lineSymbol;
// 设置渲染器
if (_selectedFeatureLayer != null)
{
IGeoFeatureLayer geoLayer = (IGeoFeatureLayer)_selectedFeatureLayer;
ISimpleRenderer renderer = new SimpleRenderer() as ISimpleRenderer;
renderer.Symbol = (ISymbol)fillSymbol;
geoLayer.Renderer = (IFeatureRenderer)renderer;
axMapControl1.ActiveView.Refresh();
}
}
private IRgbColor GetRgbColor(int r, int g, int b)
{
IRgbColor color = new RgbColor() as IRgbColor; // 正确方式
color.Red = r;
color.Green = g;
color.Blue = b;
return color;
}
public object _featureLayer { get; set; }
// 矩形选择
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:
// 绘制矩形
geometry = axMapControl1.TrackRectangle();
break;
case SelectionMode.Circle:
// 获取用户拖拽的矩形范围
IEnvelope envelope = axMapControl1.TrackRectangle();
// 创建圆心点
IPoint center = new PointClass();
center.PutCoords(envelope.XMin + envelope.Width / 2,
envelope.YMin + envelope.Height / 2);
// 正确创建圆形几何(修复部分)
ICircularArc circularArc = new CircularArcClass();
IConstructCircularArc constructArc = (IConstructCircularArc)circularArc;
double radius = Math.Min(envelope.Width, envelope.Height) / 2;
constructArc.ConstructCircle(center, radius, true);
// 创建多边形表示圆形
IGeometryCollection geomColl = new PolygonClass();
geomColl.AddGeometry((IGeometry)circularArc);
geometry = (IGeometry)geomColl;
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)
{
IMap map = axMapControl1.Map;
// 创建空间过滤器
ISpatialFilter spatialFilter = new SpatialFilterClass();
spatialFilter.Geometry = geometry;
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
// 清除之前的选择
map.ClearSelection();
// 遍历所有图层执行选择
for (int i = 0; i < map.LayerCount; i++)
{
IFeatureLayer featureLayer = map.get_Layer(i) as IFeatureLayer;
if (featureLayer == null) continue;
spatialFilter.GeometryField = featureLayer.FeatureClass.ShapeFieldName;
IFeatureSelection featureSelection = (IFeatureSelection)featureLayer;
featureSelection.SelectFeatures(spatialFilter,
esriSelectionResultEnum.esriSelectionResultNew, false);
}
axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
}
// 高亮显示选择图形
private void HighlightGeometry(IGeometry geometry)
{
IMap map = axMapControl1.Map;
IActiveView activeView = map as IActiveView;
IGraphicsContainer graphicsContainer = map as IGraphicsContainer;
// 清除之前的高亮
graphicsContainer.DeleteAllElements();
// 创建高亮符号
ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass();
fillSymbol.Color = GetRGBColor(255, 0, 0, 50); // 半透明红色
fillSymbol.Outline.Color = GetRGBColor(0, 0, 255);
ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass();
lineSymbol.Color = GetRGBColor(0, 0, 255);
lineSymbol.Width = 2;
// 创建元素
IElement element = null;
if (geometry.GeometryType == esriGeometryType.esriGeometryPolygon)
{
IFillShapeElement fillElement = new PolygonElementClass();
fillElement.Symbol = fillSymbol;
element = (IElement)fillElement;
}
else if (geometry.GeometryType == esriGeometryType.esriGeometryPolyline)
{
ILineElement lineElement = new LineElementClass();
lineElement.Symbol = lineSymbol;
element = (IElement)lineElement;
}
else if (geometry.GeometryType == esriGeometryType.esriGeometryPoint)
{
// 圆形选择使用点符号
IMarkerElement markerElement = new MarkerElementClass();
ISimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbolClass();
markerSymbol.Color = fillSymbol.Color;
markerSymbol.Size = 10;
markerElement.Symbol = markerSymbol;
element = (IElement)markerElement;
}
if (element != null)
{
element.Geometry = geometry;
graphicsContainer.AddElement(element, 0);
activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
}
// 创建颜色对象
private IRgbColor GetRGBColor(int r, int g, int b, int alpha = 255)
{
IRgbColor color = new RgbColorClass();
color.Red = r;
color.Green = g;
color.Blue = b;
color.Transparency = (byte)(255 - alpha);
return color;
}
// 清除选择
private void btnClearSelection_Click(object sender, EventArgs e)
{
axMapControl1.Map.ClearSelection();
IGraphicsContainer graphicsContainer = axMapControl1.Map as IGraphicsContainer;
graphicsContainer.DeleteAllElements();
axMapControl1.Refresh();
}
}
}还是点击空间选择中的多边形选择、圆形选择等无法进行选择呢,如何结局