ArcEngine开发——从TocControl上获取鼠标点击位置的信息(转载)

来源:互联网 发布:文件夹改名linux 编辑:程序博客网 时间:2024/05/17 05:17

原文参见:ArcEngine开发——从TocControl上获取鼠标点击位置的信息  


为了使TOCControl具有更多的功能,必须编写自己的代码。一个首当其冲的问题就是获取鼠标在TOCControl上点击的位置上所对应的信息,包括地图、图层或者图层符号等。实现这个功能,ArcEngine中提供了两个方法。其一是TOCControl封装的HitTest()方法,其二是GetSelectedItem()方法。

      先定义以下变量:

代码
1 private IBasicMap pBasicMap = new MapClass();
2 private ILayer pLayer = new FeatureLayerClass();
3 object oLegendGroup = new object();
4 object oIndex = new object();
5 esriTOCControlItem pTocItem = new esriTOCControlItem();

      使用HitTest()方法:

复制代码
代码
1 axTOCControl1.HitTest(e.x, e.y, ref pTocItem, ref pBasicMap, ref pLayer, ref oLegendGroup, ref oIndex);
2 if (e.button == 1)
3 {
4 if (pTocItem == esriTOCControlItem.esriTOCControlItemMap)
5 {
6 txtOutMsg.Text = "当前单击的是地图" + Environment.NewLine + "地图名称:" + pBasicMap.Name + " 地图中图层数为:" + pBasicMap.LayerCount.ToString();
7 }
8 else if (pTocItem == esriTOCControlItem.esriTOCControlItemLayer)
9 {
10 txtOutMsg.Text = "当前单击的是图层" + Environment.NewLine + "地图名称:" + pBasicMap.Name + " 地图中图层数为:" + pBasicMap.LayerCount.ToString() + Environment.NewLine + "所点击的图层名称:" + pLayer.Name;
11 }
12 else if (pTocItem == esriTOCControlItem.esriTOCControlItemLegendClass)
13 {
14 txtOutMsg.Text = "当前单击的是图层符号" + Environment.NewLine + "地图名称:" + pBasicMap.Name + " 地图中图层数为:" + pBasicMap.LayerCount.ToString() + Environment.NewLine + "所点击的图层名称:" + pLayer.Name;
15 }
16 else if (pTocItem == esriTOCControlItem.esriTOCControlItemNone)
17 {
18 txtOutMsg.Text = "当前单击为空白区域";
19 }
复制代码

      GetSelectedItem()方法相比HitTest()方法少了鼠标点击位置参数(e.x和e.y),其余参数一致,代码可按上照搬。

      二者的区别。要获取鼠标在TOCControl控件中点击位置所对应的信息,在TOCControl的MouseDown和MouseUp事件中使用HitTest()方法都是可以的;如果使用GetSelectedItem()方法,就只能在MouseUp事件中来写代码了。这是因为在发生MouseDown事件时,还没有完成TOCControl中Item的选中,如果在MouseDown事件中使用GetSelectedItem()方法获取的结果是上一次点击选中的Item,而不是当前这次点击的Item。还有一个小小的区别就是这两种方法对鼠标在TOCControl中点击的位置的“敏感度”不同。自己体验一下就知道了。

0 0
原创粉丝点击