要求:
編寫程序,顯示Map座標、工具條命令提示信息,並移除圖層。
1、在狀態欄顯示Map中鼠標當前位置的座標信息(MapControl的OnMouseMove)。
2、在狀態欄顯示工具條命令的提示信息(ToolbarControl的OnMouseMove、HitTest)。
3、在TOCControl的圖層上按鼠標右鍵,移除該圖層(TOCControl的OnMouseDown、HitTest)
作答:
1.在狀態欄顯示Map中鼠標當前位置的座標信息
private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
{
//Map中X座標e.mapX,Y座標e.mapY,座標單位 axMapControl1.MapUnits
toolStripStatusLabel4.Text = "當前座標 X =" + Math.Round(e.mapX, 2).ToString() + ",Y ="
+ Math.Round(e.mapY, 2).ToString() +" "
+ this.axMapControl1.MapUnits.ToString().Substring(4);
//顯示當前比例尺
//toolStripStatusLabel3.Text = "比例尺 1:" + ((long)this.axMapControl1.MapScale).ToString();
/* C#保留兩位小數,四捨五入
1. Math.Round(str,2)
2. str.ToString("#0.00")
*/
/* C#去掉字符串的前i個字符
1. str.Remove(0,i)
2. str.SubString(i)
*/
}
2.在狀態欄顯示工具條命令的提示信息
private void axToolbarControl1_OnMouseMove(object sender, IToolbarControlEvents_OnMouseMoveEvent e)
{
//獲得鼠標所在工具的索引號 初始索引爲-1
int index = axToolbarControl1.HitTest(e.x,e.y,false)
if (index != -1)
{
//取得鼠標所在工具的ToolbarItem
IToolbarItem toolbarItem = axToolbarControl1.GetItem(index);
//設置狀態欄信息
toolStripStatusLabel1.Text = toolbarItem.Command.Message;
}
else
{
toolStripStatusLabel1.Text = "就緒";
}
}
3.在TOCControl的圖層上按鼠標右鍵,移除該圖層
①右鍵菜單的實現(控件是contextMenuStrip1,事件是axTOCControl1_OnMouseUp)
public ILayer pLayer; //☆**定義一個全局變量pLayer 選中的圖層
private void axTOCControl1_OnMouseUp(object sender, ITOCControlEvents_OnMouseUpEvent e)
{
//實例化函數中的五個參數
esriTOCControlItem Item = esriTOCControlItem.esriTOCControlItemNone;
IBasicMap map = null; //Map object,if selected
ILayer layer = null; //Layer object,if selected
object group = null; //Group Layer,if selected
object groupIndex = null; //Layer index,if group Layer selected
//根據鼠標所在的位置測試判斷其所指向的對象
//HitTest()函數
axTOCControl1.HitTest(e.x,e.y,ref Item,ref map,ref layer,ref group,ref groupIndex);
if (e.button == 2 && Item == esriTOCControlItem.esriTOCControlItemLayer) //點擊對象是圖層的話,就顯示右鍵菜單 e.button=2代表鼠標右擊,=1是單擊左鍵
{
pLayer = layer;
//顯示右鍵菜單,並定義其相對控件的位置
contextMenuStrip1.Show(axTOCControl1,e.x,e.y);
}
}
②移除圖層功能的實現
private void 移除ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (pLayer != null) //選中圖層
{
axMapControl1.Map.DeleteLayer(pLayer); //調用DeleteLayer方法刪除圖層
pLayer = null;
}
}
③程序響應圖層移除事件(*)
private IActiveViewEvents_Event ave;
private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
{
ave = axMapControl1.Map as IActiveViewEvents_Event;
ave.ItemDeleted += new IActiveViewEvents_ItemDeletedEventHandler(ave_ItemDeleted);
}
void ave_ItemDeleted(object Item)
{
if (Item is ILayer)
{
ILayer pLayer = Item as ILayer;
MessageBox.Show("圖層"+pLayer.Name+"已被移除!");
}
}
4.右鍵打開屬性表(主要在於父子窗體間"傳值")
在項目中新建一個窗體(AttributeTable.cs)
右鍵項目名稱 → 添加 → 新建項 → 選擇Windows窗體,並給窗體命名 → 添加
AttributeTable窗體內添加dataGridView控件,Dock屬性設置爲Fill
☆法1(推薦):
Form1.cs:
private void 打開屬性表ToolStripMenuItem_Click(object sender, EventArgs e)
{
AttributeTable pAttributeTable = new AttributeTable(pLayer); //**
pAttributeTable.Show();
pAttributeTable.Text = "屬性表:" + pLayer.Name;
}
AttributeTable.cs:
public partial class AttributeTable : Form
{
ILayer mLayer; //**
public AttributeTable(ILayer pLayer) //**
{
InitializeComponent();
mLayer = pLayer; //**
}
}
private void AttributeTable_Load(object sender, EventArgs e)
{
OpenAttributeTable(); //自定義方法
}
public void OpenAttributeTable()
{
IFeatureLayer pFeatureLayer = mLayer as IFeatureLayer; //**
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass; //** using ESRI.ArcGIS.Geodatabase;
DataTable dt = new DataTable();
if (pFeatureClass != null)
{
DataColumn dc;
for (int i = 0; i < pFeatureClass.Fields.FieldCount; i++)
{
dc = new DataColumn(pFeatureClass.Fields.get_Field(i).Name);
dt.Columns.Add(dc);//獲取所有列的屬性值
}
IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
IFeature pFeature = pFeatureCursor.NextFeature();
DataRow dr;
while (pFeature != null)
{
dr = dt.NewRow();
for (int j = 0; j < pFeatureClass.Fields.FieldCount; j++)
{
//判斷feature的形狀
if (pFeature.Fields.get_Field(j).Name == "Shape")
{
if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
{
dr[j] = "點";
}
if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
{
dr[j] = "線";
}
if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
{
dr[j] = "面";
}
}
else
{
dr[j] = pFeature.get_Value(j).ToString();//增加行
}
}
dt.Rows.Add(dr);
pFeature = pFeatureCursor.NextFeature();
}
}
this.dataGridView1.DataSource = dt;
}
法2:
Form1.cs:
private void 打開屬性表ToolStripMenuItem_Click(object sender, EventArgs e)
{
AttributeTable pAttributeTable = new AttributeTable();
pAttributeTable.Show(this); //*
pAttributeTable.Text = "屬性表:" + pLayer.Name;
}
AttributeTable.cs:
private void AttributeTable_Load(object sender, EventArgs e)
{
OpenAttributeTable(); //自定義方法
}
public void OpenAttributeTable()
{
Form1 pForm1 = (Form1)this.Owner; //*
ILayer mLayer = pForm1.pLayer; //*
IFeatureLayer pFeatureLayer = mLayer as IFeatureLayer; //*
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass; //* using ESRI.ArcGIS.Geodatabase;
DataTable dt = new DataTable();
if (pFeatureClass != null)
{
DataColumn dc;
for (int i = 0; i < pFeatureClass.Fields.FieldCount; i++)
{
dc = new DataColumn(pFeatureClass.Fields.get_Field(i).Name);
dt.Columns.Add(dc);//獲取所有列的屬性值
}
IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
IFeature pFeature = pFeatureCursor.NextFeature();
DataRow dr;
while (pFeature != null)
{
dr = dt.NewRow();
for (int j = 0; j < pFeatureClass.Fields.FieldCount; j++)
{
//判斷feature的形狀
if (pFeature.Fields.get_Field(j).Name == "Shape")
{
if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
{
dr[j] = "點";
}
if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
{
dr[j] = "線";
}
if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
{
dr[j] = "面";
}
}
else
{
dr[j] = pFeature.get_Value(j).ToString();//增加行
}
}
dt.Rows.Add(dr);
pFeature = pFeatureCursor.NextFeature();
}
}
this.dataGridView1.DataSource = dt;
}