zoukankan      html  css  js  c++  java
  • ArcEngine开发——从TocControl上获取鼠标点击位置的信息

          承接前一内容(ArcEngine开发——地图浏览)稍作界面调整,添加TOCControl、TextBox和两个Button控件,调整后如下:

         

          为了使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中点击的位置的“敏感度”不同。自己体验一下就知道了。

  • 相关阅读:
    SQL Server 2008中如何为XML字段建立索引
    比如取得一个div得innerHTML
    C#生成CHM文件(入门篇)
    jquery outerhtml
    WCF 中状态的保存
    MVC进阶学习HtmlHelper控件解析(一)
    MVC进阶学习HtmlHelper控件解析(四)
    MVC进阶学习HtmlHelper之GridView控件拓展(一)
    MVC进阶学习HtmlHelper控件解析(三)
    MVC进阶学习表单提交总结
  • 原文地址:https://www.cnblogs.com/hans_gis/p/1896967.html
Copyright © 2011-2022 走看看