📄 form_main.cs
字号:
case 10: //开始编辑
if(MainMap.LayerCount==0)
{
System.Windows.Forms.MessageBox.Show("请先加载数据!");
return;
}
m_Map=MainMap.Map;
comboBox_EditTask.Enabled=true; //编辑任务comboBox可用
comboBox_Target.Enabled=true; //编辑图层comboBox可用
comboBox_Target.Items.Clear();
for(int i=0;i<=m_Map.LayerCount - 1;i++)
{
comboBox_Target.Items.Add(m_Map.get_Layer(i).Name); //编辑图层comboBox添加各图层名
comboBox_Target.SelectedIndex=0;
}
for(int i=0;i<=m_Map.LayerCount - 1;i++)
{
if(m_Map.get_Layer(i).Name==comboBox_Target.Text) //默认设定最新添加的图层为可编辑图层
{
m_CurrentLayer = m_Map.get_Layer(i);
break;
}
}
comboBox_EditTask.SelectedIndex=0; //默认设定编辑任务为新建要素
toolBarButton_ZoomIn.Pushed=false;
toolBarButton_ZoomOut.Pushed=false;
toolBarButton_Pan.Pushed=false;
toolBarButton_Select.Pushed=false;
toolBarButton_Sketch.Pushed=true;
menuItem_StartEdit.Enabled=false;
menuItem_SaveEdit.Enabled=true;
menuItem_StopEdit.Enabled=true;
StartEditing(); //开始编辑
Cstest1.Class.CreateShape m_CreateShapeStart=new Cstest1.Class.CreateShape(m_CurrentLayer);
m_CreateShapeStart.OnCreate(this.MainMap);
MainMap.CurrentTool=m_CreateShapeStart; //默认使用画图新建要素工具
break;
case 11: //保存编辑
if(MainMap.LayerCount==0)
{
System.Windows.Forms.MessageBox.Show("请先加载数据!");
return;
}
if (SaveEditing() == 1) //调用保存编辑函数
{
MainMap.Map.ClearSelection();
MainMap.ActiveView.Refresh();
menuItem_SaveEdit.Enabled=false;
}
break;
case 12: //停止编辑
if(MainMap.LayerCount==0)
{
System.Windows.Forms.MessageBox.Show("请先加载数据!");
return;
}
if (StopEditing() == 1) //调用停止编辑函数
{
MainMap.Map.ClearSelection();
MainMap.ActiveView.Refresh();
MainMap.MousePointer= esriControlsMousePointer.esriPointerDefault;
toolBarButton_Sketch.Pushed=false;
comboBox_EditTask.Enabled=false;
comboBox_Target.Enabled=false;
menuItem_StartEdit.Enabled=true;
menuItem_SaveEdit.Enabled=false;
menuItem_StopEdit.Enabled=false;
//停止编辑后,需要默认选取一个浏览工具
Pan m_Pan=new Pan();
m_Pan.OnCreate(this.MainMap);
this.MainMap.CurrentTool=m_Pan;
}
break;
case 13: //草图画笔工具
if(MainMap.LayerCount==0)
{
System.Windows.Forms.MessageBox.Show("请先加载数据!");
toolBarButton_Sketch.Pushed=false;
return;
}
toolBarButton_ZoomIn.Pushed=false;
toolBarButton_ZoomOut.Pushed=false;
toolBarButton_Pan.Pushed=false;
toolBarButton_Select.Pushed=false;
if(m_bSketch)
{
Cstest1.Class.CreateShape m_CreateShapeSketch=new Cstest1.Class.CreateShape(m_CurrentLayer);
m_CreateShapeSketch.OnCreate(this.MainMap);
MainMap.CurrentTool=m_CreateShapeSketch; //创建画图新建要素工具
}
else if(m_bModify)
{
Cstest1.Class.ModifyShape m_ModifyShape=new Cstest1.Class.ModifyShape(m_CurrentLayer);
m_ModifyShape.OnCreate(this.MainMap);
MainMap.CurrentTool=m_ModifyShape; //创建修改要素工具
}
else
{
MessageBox.Show("编辑操作尚未开始,请先开始编辑!");
toolBarButton_Sketch.Pushed=false;
return;
}
StartEditing(); //开始编辑
break;
case 14: //复制
if(MainMap.LayerCount==0)
{
System.Windows.Forms.MessageBox.Show("请先加载数据!");
return;
}
CopySelectedFeatures(); //调用复制要素方法
break;
case 15: //粘贴
if(MainMap.LayerCount==0)
{
System.Windows.Forms.MessageBox.Show("请先加载数据!");
return;
}
PasteSelectedFeatures(); //调用粘贴要素方法
break;
case 16: //删除
if(MainMap.LayerCount==0)
{
System.Windows.Forms.MessageBox.Show("请先加载数据!");
return;
}
DeleteSelectedFeatures(); //调用删除要素方法
break;
}
}
//******************************** 实时显示当前点坐标 ***********************************************
private void MainMap_OnMouseMove(object sender, ESRI.ArcGIS.MapControl.IMapControlEvents2_OnMouseMoveEvent e)
{
// string[] degree = e.mapX.ToString().Split(new Char[]{'.'}); //十进制地图坐标,以小数点划分: degree[0]为度
//
// string minute = "0." + degree[1];
// double dblminute = Convert.ToDouble(minute) * 60;
//
// string[] coords = dblminute.ToString().Split(new Char[]{'.'});//度数的分,以小数点划分:coords[0]为分
// minute = coords[0];
//
// string second = "0." + coords[1];
// double dblsecond = Convert.ToDouble(second) * 60; //分的小数点余数为秒,转换成秒数
// second = second.Substring(0, 5);
// statusBar_Info.Panels[1].Text = degree[0] + '°' + minute + '′' + second + '″';
if(e.mapX>0 && e.mapX<180 && e.mapY>0 && e.mapY<90)
statusBar_Info.Panels[1].Text=ConvertToGeopraphicCoordinate(e.mapX.ToString().Substring(0,8)) + "E " + ConvertToGeopraphicCoordinate(e.mapY.ToString().Substring(0,8)) + "N";
}
private string ConvertToGeopraphicCoordinate(string Coordinate) //以经纬度形式显示
{
string[] degree = Coordinate.Split(new Char[]{'.'}); //十进制地图坐标,以小数点划分: degree[0]为度
string minute = "0." + degree[1];
double dblminute = Convert.ToDouble(minute) * 60;
string[] coords = dblminute.ToString().Split(new Char[]{'.'});//度数的分,以小数点划分:coords[0]为分
minute = coords[0];
string second = "0." + coords[1];
double dblsecond = Convert.ToDouble(second) * 60; //分的小数点余数为秒,转换成秒数
second = second.Substring(2, 2);
return degree[0] + '°' + minute + '′' + second + '″';
}
//******************************* 地图比例尺设置 ********************************************************
private void comboBox_MapScale_SelectedIndexChanged(object sender, System.EventArgs e)
{
//选择下拉框里面已设定的比例尺
string MapScaleStr;
MapScaleStr=this.comboBox_MapScale.Text.Substring(2,comboBox_MapScale.Text.Length-2);
this.MainMap.Map.MapScale=double.Parse(MapScaleStr);
MainMap.ActiveView.Refresh();
}
private void comboBox_MapScale_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
//用户输入的比例尺
if((e.KeyChar< (char)48|| e.KeyChar> (char)57) && e.KeyChar!=(char)8 || e.KeyChar==(char)13)
{
e.Handled=true;
}
if(e.KeyChar==(char)13)
{
if(comboBox_MapScale.Text=="")
{
comboBox_MapScale.Text="1:" + Convert.ToString((int)MainMap.Map.MapScale);
comboBox_MapScale.Focus();
comboBox_MapScale.SelectAll();
return;
}
for(int i=0;i<comboBox_MapScale.Text.Length;i++)
{
if(!char.IsDigit(comboBox_MapScale.Text[i]))
{
MessageBox.Show("只需输入数字!");
comboBox_MapScale.Text="1:" + Convert.ToString((int)MainMap.Map.MapScale);
comboBox_MapScale.Focus();
comboBox_MapScale.SelectAll();
return;
}
}
this.MainMap.Map.MapScale=double.Parse(this.comboBox_MapScale.Text);
comboBox_MapScale.Focus();
comboBox_MapScale.SelectAll();
MainMap.ActiveView.Refresh();
}
}
private void MainMap_OnViewRefreshed(object sender, ESRI.ArcGIS.MapControl.IMapControlEvents2_OnViewRefreshedEvent e)
{
//地图刷新时,更新比例尺的显示
comboBox_MapScale.Text="1:" + Convert.ToString((int)MainMap.Map.MapScale);
}
//************************************* 地图编辑任务设定 ************************************************
private void comboBox_EditTask_SelectedIndexChanged(object sender, System.EventArgs e)
{
// 编辑状态设置
this.toolBarButton_Sketch.Pushed=true;
MainMap.MousePointer = esriControlsMousePointer.esriPointerDefault;
m_bModify = false;
m_bSketch = false;
// 选择编辑任务
switch(comboBox_EditTask.SelectedIndex)
{
case 0:
MainMap.MousePointer = esriControlsMousePointer.esriPointerPencil;
m_bSketch = true;
break;
case 1:
MainMap.MousePointer = esriControlsMousePointer.esriPointerCrosshair;
m_bModify = true;
break;
}
if(m_bSketch)
{
Cstest1.Class.CreateShape m_CreateShape=new Cstest1.Class.CreateShape(m_CurrentLayer);
m_CreateShape.OnCreate(this.MainMap);
MainMap.CurrentTool=m_CreateShape;
}
else if(m_bModify)
{
Cstest1.Class.ModifyShape m_ModifyShape=new Cstest1.Class.ModifyShape(m_CurrentLayer);
m_ModifyShape.OnCreate(this.MainMap);
MainMap.CurrentTool=m_ModifyShape;
}
}
//************************************ 编辑目标图层设定 ************************************************
private void comboBox_Target_SelectedIndexChanged(object sender, System.EventArgs e) //选择需要编辑的目标图层
{
this.toolBarButton_Sketch.Pushed=true;
for(int i=0;i<=m_Map.LayerCount - 1;i++)
{
if(m_Map.get_Layer(i).Name==comboBox_Target.SelectedItem.ToString())
{
m_CurrentLayer = m_Map.get_Layer(i);
break;
}
}
if(m_bSketch)
{
Cstest1.Class.CreateShape m_CreateShape=new Cstest1.Class.CreateShape(m_CurrentLayer);
m_CreateShape.OnCreate(this.MainMap);
MainMap.CurrentTool=m_CreateShape;
}
else if(m_bModify)
{
Cstest1.Class.ModifyShape m_ModifyShape=new Cstest1.Class.ModifyShape(m_CurrentLayer);
m_ModifyShape.OnCreate(this.MainMap);
MainMap.CurrentTool=m_ModifyShape;
}
}
//************************************ Shapefile数据加载菜单 ************************************************
private void menuItem_AddData_Click(object sender, System.EventArgs e) //加载Shapefile菜单
{
if(AddData()==1) //数据加载成功
{
this.comboBox_MapScale.Text="1:" + Convert.ToString((int)MainMap.Map.MapScale);
menuItem_ClearLayers.Enabled=true;
}
}
private int AddData()
{
OpenFileDialog dlg=new OpenFileDialog();
dlg.Filter="Shapefiles文件(*.shp)|*.shp|所有文件 (*.*)|*.*";
dlg.DefaultExt = "*.shp";
dlg.Title="加载数据";
dlg.Multiselect=true;
//定义存放打开 IFeatureClass 的字符串数组
string[] FilePath;
if(dlg.ShowDialog()==DialogResult.OK)
{
MainMap.MousePointer=esriControlsMousePointer.esriPointerArrowHourglass;//数据加载过程,鼠标变为沙漏状态
FilePath=new string[dlg.FileNames.Length]; //获取多个文件名
FilePath=dlg.FileNames;
if(FilePath.Length > 0)
{
string WorkSpacePath =System.IO.Path.GetDirectoryName(FilePath[0]);
string[] ShapeFilePath=new string[FilePath.Length] ;
//获得打开 IFeatureClass 的字符串数组
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -