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

  • 相关阅读:
    UESTC 1061 秋实大哥与战争 线段树区间合并
    bzoj 2005: [Noi2010]能量采集 筛法||欧拉||莫比乌斯
    bzoj 1008: [HNOI2008]越狱 数学
    bzoj 1579: [Usaco2009 Feb]Revamping Trails 道路升级 优先队列+dij
    LightOJ 1138 二分
    AIM Tech Round 3 (Div. 2) A , B , C
    Codeforces Round #335 (Div. 2) C. Sorting Railway Cars
    hdu 4542 小明系列故事——未知剩余系 反素数 + 打表
    Codeforces Beta Round #27 (Codeforces format, Div. 2) E. Number With The Given Amount Of Divisors 反素数
    51nod 1060 最复杂的数 反素数
  • 原文地址:https://www.cnblogs.com/hans_gis/p/1896967.html
Copyright © 2020-2023  润新知