www.pudn.com > ArcgisMapOperation.rar > MainForm.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ArcgisMapOperation.EsriLib;
using ArcgisMapOperation.Struct;
using ArcgisMapOperation.Enum;
namespace ArcgisMapOperation
{
public sealed partial class MainForm : Form
{
#region 私有成员
///
/// 主图指针
///
private IMapControl3 mapControlMain ;
///
/// 鹰眼图指针
///
private IMapControl3 mapControlOverview ;
///
/// 工作区的名称
///
private string strMapDocumentName ;
///
/// 主图操作标志
///
private MapViewOperation Flag;
///
/// 判断是开始点还是结束点
///
private bool bStartOrEndPoint;
///
/// 屏幕操作对象
///
private IScreenDisplay sceenDisplay;
///
/// 画图
///
private IPolygon polygonDraw;
///
/// 工作区
///
private IWorkspace workSpace;
#endregion
#region 初始化
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
mapControlMain = (IMapControl3)axMapControlMain.Object;
mapControlOverview = (IMapControl3)axMapControlOverview.Object;
menuSaveDoc.Enabled = false;
}
#endregion
#region “文件”菜单事件
///
/// 新工作区
///
///
///
private void menuNewDoc_Click(object sender, EventArgs e)
{
ICommand command = new CreateNewDocument();
command.OnCreate(mapControlMain.Object);
command.OnClick();
}
///
/// 打开工作区
///
///
///
private void menuOpenDoc_Click(object sender, EventArgs e)
{
ICommand command = new ControlsOpenDocCommandClass();
command.OnCreate(mapControlMain.Object);
command.OnClick();
}
///
/// 保存工作区
///
///
///
private void menuSaveDoc_Click(object sender, EventArgs e)
{
if (mapControlMain.CheckMxFile(strMapDocumentName))
{
//create a new instance of a MapDocument
IMapDocument mapDoc = new MapDocumentClass();
mapDoc.Open(strMapDocumentName, string.Empty);
//Make sure that the MapDocument is not readonly
if (mapDoc.get_IsReadOnly(strMapDocumentName))
{
MessageBox.Show("Map document is read only!");
mapDoc.Close();
return;
}
//Replace its contents with the current map
mapDoc.ReplaceContents((IMxdContents)mapControlMain.Map);
//save the MapDocument in order to persist it
mapDoc.Save(mapDoc.UsesRelativePaths, false);
//close the MapDocument
mapDoc.Close();
}
}
///
/// 另存工作区
///
///
///
private void menuSaveAs_Click(object sender, EventArgs e)
{
ICommand command = new ControlsSaveAsDocCommandClass();
command.OnCreate(mapControlMain.Object);
command.OnClick();
}
///
/// 退出
///
///
///
private void menuExitApp_Click(object sender, EventArgs e)
{
Application.Exit();
}
#endregion
#region "Database"菜单事件
///
private void tsmiLoadData_Click(object sender, EventArgs e)
{
try
{
//首先清除所有的图层
this.axMapControlMain.Map.ClearLayers();
string strAccessFile = SystemConfig.DataBaseCon;
workSpace = UserDatabaseOperation.GetWorkspaceBySetPerproty(strAccessFile);
ILayer pLayer = UserFeatureOperation.AddFeature(workSpace, "test_py");
this.axMapControlMain.Map.AddLayer(pLayer);
pLayer = UserFeatureOperation.AddFeature(workSpace, "test_pt");
this.axMapControlMain.Map.AddLayer(pLayer);
pLayer = UserFeatureOperation.AddFeature(workSpace, "test_pl");
this.axMapControlMain.Map.AddLayer(pLayer);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region "SpatialFilter"菜单事件
///
/// 测试空间过滤
///
///
///
private void tsmiFilterTest_Click(object sender, EventArgs e)
{
Flag = MapViewOperation.DrawGeometry;
bStartOrEndPoint = false;
}
#endregion
#region "Query"菜单事件
///
/// 查询
///
///
///
private void tsmiQueryTest_Click(object sender, EventArgs e)
{
IQueryFilter pQueryFilter = new QueryFilter();
pQueryFilter.WhereClause = "name ='A'";
if (mapControlMain.LayerCount > 0)
{
IFeatureSelection featureSelection = mapControlMain.get_Layer(0) as IFeatureSelection;
featureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
featureSelection.SelectionSet.Refresh();
mapControlMain.ActiveView.Refresh();
}
}
///
/// 内存中添加新记录
///
///
///
private void tsmiAddNewRecord_Click(object sender, EventArgs e)
{
IFeatureLayer featureLayer = mapControlMain.get_Layer(0) as IFeatureLayer;
IPoint pointStart = new PointClass();
pointStart.X = 80;
pointStart.Y = 67;
IPoint pointEnd = new PointClass();
pointEnd.X = 121;
pointEnd.Y = 26;
IPolyline polyLine = UserGeometryGenerate.CreatePolyline(pointStart, pointEnd);
List lstFieldName = new List();
lstFieldName.Add("name");
List