📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.ToolbarControl;
using ESRI.ArcGIS.TOCControl;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.ControlCommands;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Utility.CATIDs;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.MapControl;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.PageLayoutControl;
using ESRI.ArcGIS.Utility;
namespace PointSelection
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private ESRI.ArcGIS.MapControl.AxMapControl axMapControl1;
private ESRI.ArcGIS.ToolbarControl.AxToolbarControl axToolbarControl1;
private System.Windows.Forms.Button button1;
private IAoInitialize m_AoInitialize = new AoInitializeClass();
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.axMapControl1 = new ESRI.ArcGIS.MapControl.AxMapControl();
this.axToolbarControl1 = new ESRI.ArcGIS.ToolbarControl.AxToolbarControl();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.axMapControl1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.axToolbarControl1)).BeginInit();
this.SuspendLayout();
//
// axMapControl1
//
this.axMapControl1.Location = new System.Drawing.Point(24, 40);
this.axMapControl1.Name = "axMapControl1";
this.axMapControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMapControl1.OcxState")));
this.axMapControl1.Size = new System.Drawing.Size(880, 632);
this.axMapControl1.TabIndex = 0;
this.axMapControl1.OnMouseDown += new ESRI.ArcGIS.MapControl.IMapControlEvents2_OnMouseDownEventHandler(this.axMapControl1_OnMouseDown);
this.axMapControl1.OnMouseUp += new ESRI.ArcGIS.MapControl.IMapControlEvents2_OnMouseUpEventHandler(this.axMapControl1_OnMouseUp);
//
// axToolbarControl1
//
this.axToolbarControl1.Location = new System.Drawing.Point(32, 8);
this.axToolbarControl1.Name = "axToolbarControl1";
this.axToolbarControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axToolbarControl1.OcxState")));
this.axToolbarControl1.Size = new System.Drawing.Size(192, 26);
this.axToolbarControl1.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(288, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(40, 24);
this.button1.TabIndex = 2;
this.button1.Text = "点选";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(928, 694);
this.Controls.Add(this.button1);
this.Controls.Add(this.axToolbarControl1);
this.Controls.Add(this.axMapControl1);
this.Name = "Form1";
this.Text = "Form1";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.axMapControl1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.axToolbarControl1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
axToolbarControl1.Enabled=true;
string filename=@"C:\\Program Files\\ArcGIS\\DeveloperKit\\samples\\data\\usa\\usa.mxd";
axToolbarControl1.SetBuddyControl(axMapControl1);
this.SetStyle(ControlStyles.EnableNotifyMessage,true);
this.axMapControl1.LoadMxFile(filename,"","");
}
private void PointSelection(int x,int y,IMap m_pMap,ILayer m_CurrentLayer)
{
IGeoFeatureLayer pGFeat=m_CurrentLayer as IGeoFeatureLayer;
IFeatureClass pFeatureClass=pGFeat.FeatureClass;
IActiveView activeView=m_pMap as IActiveView;
IPoint pPoint = activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
IGeometry pGeometry=pPoint as IGeometry;
double length=ConvertPixelsToMapUnits(activeView,4);
ITopologicalOperator pTopo=pGeometry as ITopologicalOperator;
IGeometry pBuffer=pTopo.Buffer(length);
pGeometry=pBuffer.Envelope;
ISpatialFilter pSpatialFiler=new SpatialFilterClass();
//IFilter pFilter=null;
switch(pFeatureClass.ShapeType)
{
case esriGeometryType.esriGeometryPoint:
pSpatialFiler.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
break;
case esriGeometryType.esriGeometryPolyline:
pSpatialFiler.SpatialRel = esriSpatialRelEnum.esriSpatialRelCrosses;
break;
case esriGeometryType.esriGeometryPolygon:
pSpatialFiler.SpatialRel = esriSpatialRelEnum.esriSpatialRelEnvelopeIntersects;
break;
}
pSpatialFiler.GeometryField=pFeatureClass.ShapeFieldName;
//pFilter=pSpatialFiler;
IFeatureCursor pCursor=pGFeat.Search(pSpatialFiler,false);
IFeature pFeature=pCursor.NextFeature();
while(pFeature!=null)
{
m_pMap.SelectFeature(m_CurrentLayer,pFeature);
pFeature=pCursor.NextFeature();
}
}
private double ConvertPixelsToMapUnits(IActiveView pActiveView,double pixlUnits)
{
double realWorldDisplayEntent;
int pixelExtent;
double sizeOfOnePixel;
//IDisplayTransformation pDispTranf=pActiveView.ScreenDisplay.DisplayTransformation;
pixelExtent=pActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame().right-pActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame().left;
realWorldDisplayEntent=pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.Width;
sizeOfOnePixel=realWorldDisplayEntent/pixelExtent;
return pixlUnits*sizeOfOnePixel;
}
private void button1_Click(object sender, System.EventArgs e)
{
// IGeoFeatureLayer pgf2=this.axMapControl1 .Map.get_Layer(3) as IGeoFeatureLayer;
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
ESRI.ArcGIS.Utility.COMSupport.AOUninitialize.Shutdown();
m_AoInitialize.Shutdown();
}
private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.MapControl.IMapControlEvents2_OnMouseDownEvent e)
{
//IGeoFeatureLayer pgf2=this.axMapControl1 .Map.get_Layer(3) as IGeoFeatureLayer;
IMap pmap1=this.axMapControl1 .Map;
ILayer pLayer=this.axMapControl1 .Map.get_Layer(0);
PointSelection(e.x,e.y ,pmap1, pLayer);
}
private void axMapControl1_OnMouseUp(object sender, ESRI.ArcGIS.MapControl.IMapControlEvents2_OnMouseUpEvent e)
{
IMap pmap1=this.axMapControl1 .Map;
ILayer pLayer=this.axMapControl1 .Map.get_Layer(0);
PointSelection(e.x,e.y ,pmap1, pLayer);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -