liutingxiang的个人博客分享 http://blog.sciencenet.cn/u/liutingxiang

博文

ArcGIS Engine二次开发学习(5)TOC控件

已有 4216 次阅读 2018-4-10 11:26 |个人分类:地理信息系统二次开发|系统分类:教学心得

 所要实现的功能:

(1)鼠标点击图层,拖动图层调整显示顺序;

(2)鼠标右键图层,弹出右键菜单,实现“打开属性表”、“删除图层”、“缩放至图层”等功能。



1、定义TOC中鼠标down和up的事件变量

ITOCControlEvents_OnMouseDownEvent ted;
public ITOCControlEvents_OnMouseDownEvent Ted
{
      get { return ted; }
      set { ted = value; }
}
ITOCControlEvents_OnMouseUpEvent teu;
public ITOCControlEvents_OnMouseUpEvent Teu
{
      get { return teu; }
      set { teu = value; }
}


2、定义TOC中鼠标操作的方法

ILayer pMovelayer;
int toIndex;
public void AdjLayMd()
{
      esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
      if (ted.button == 1)
      {
           IBasicMap map = null;
           ILayer layer = null;
           object other = null;
           object index = null;
           axTOCControl1.HitTest(ted.x, ted.y, ref item, ref map, ref layer, ref other, ref index);
           if (item == esriTOCControlItem.esriTOCControlItemLayer)
           {
               if (layer is IAnnotationSublayer)
                   return;
               else
               {
                   pMovelayer = layer;
                }
            }
       }
            //鼠标右键按下
      else if (ted.button == 2)
       {
           if (axMapControl1.LayerCount > 0) //主视图中有地理数据
           {
               return;
            }
        }
}
public void AdjLayMu()
{
    if (teu.button == 1)
    {
        esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
        IBasicMap map = null;
        ILayer layer = null;
        object other = null;
        object index = null;
        axTOCControl1.HitTest(teu.x, teu.y, ref item, ref map, ref layer, ref other, ref index);
        IMap pMap = axMapControl1.ActiveView.FocusMap;
        if (item == esriTOCControlItem.esriTOCControlItemLayer || layer != null)
        {
            if (pMovelayer != null)//!=null??
            {
                ILayer pTempLayer;
                for (int i = 0; i < pMap.LayerCount; i++)
                {
                    pTempLayer = pMap.get_Layer(i);
                    if (pTempLayer == layer)
                        toIndex = i;//获取鼠标点击位置的图层索引号
                 }
                 pMap.MoveLayer(pMovelayer, toIndex);
                 axMapControl1.ActiveView.Refresh();
                 axTOCControl1.Update();
               }
          }
      }
}

3、TOC事件代码中调用该方法

private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
     ted = e;
     AdjLayMd();
}
private void axTOCControl1_OnMouseUp(object sender, ITOCControlEvents_OnMouseUpEvent e)
{
     teu = e;
     AdjLayMu();
}

TOC中右键菜单的设计步骤:

4、在TOC中添加一个contxtmenustrip控件,设计几个菜单,如打开属性表、删除图层等。

在TOC的onmousedown中添加代码,调出这个控件。

ILayer SelectedLayer_TOC;
private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
    ted = e;
    AdjLayMd();
            
    if (e.button == 2)
    {
        ESRI.ArcGIS.Controls.esriTOCControlItem Item = ESRI.ArcGIS.Controls.esriTOCControlItem.esriTOCControlItemNone;
        IBasicMap pBasicMap = null;
        ILayer pLayer = null;
        object other = null;
        object index = null;
        axTOCControl1.HitTest(e.x, e.y, ref Item, ref pBasicMap, ref pLayer, ref other, ref index);          //实现赋值
        if (Item == esriTOCControlItem.esriTOCControlItemLayer)//点击的是图层的话,就显示右键菜单
       {
            SelectedLayer_TOC = pLayer;
            contextMenuStrip1.Show(axTOCControl1, new System.Drawing.Point(e.x, e.y));
        //显示右键菜单,并定义其相对控件的位置,正好在鼠标出显示
       }
    }
}
5、删除图层的代码

private void 删除图层ToolStripMenuItem_Click(object sender, EventArgs e)
{
  if (SelectedLayer_TOC != null)
  {
    axMapControl1.Map.DeleteLayer(SelectedLayer_TOC);
    axMapControl2.Map.DeleteLayer(SelectedLayer_TOC);
        SelectedLayer_TOC = null;
  }
}


6、添加一个新的windows窗体GeoMapAttribute,在上面添加个datagridview控件,用来显示图层属性,Modifiers为public。form1.cs中打开属性表的代码如下:

private void 打开属性表ToolStripMenuItem_Click(object sender, EventArgs e)
{
    GeoMapAttribute newMapAttribute = new GeoMapAttribute(SelectedLayer_TOC);
    newMapAttribute.Show();
}

7、GeoMapAttribute.cs 中构造函数

public GeoMapAttribute(ILayer pLyr)
{
    ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
    InitializeComponent();
    pLayer = pLyr;
}

8、定义几个变量

private ILayer pLayer;//打开属性表的图层
private IFeatureLayer pFeatureLayer;
private IFeatureClass pFeatureClass;
private ILayerFields pLayerFields;


9、在新的windows窗体GeoMapAttribute的load事件中添加如下代码,打开属性表。

private void GeoMapAttribute_Load(object sender, EventArgs e)
{
  try
  {
    pFeatureLayer = pLayer as IFeatureLayer;
    pFeatureClass = pFeatureLayer.FeatureClass;
    pLayerFields = pFeatureLayer as ILayerFields;
    DataSet ds = new DataSet("dsTest");
    DataTable dt = new DataTable(pFeatureLayer.Name);
    DataColumn dc = null;
    for (int i = 0; i < pLayerFields.FieldCount; i++)
    {
       dc = new DataColumn(pLayerFields.get_Field(i).Name);
       dt.Columns.Add(dc);
       dc = null;
    }
    IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
    IFeature pFeature = pFeatureCursor.NextFeature();
    while (pFeature != null)
    {
       DataRow dr = dt.NewRow();
       for (int j = 0; j < pLayerFields.FieldCount; j++)
       {
          if (pLayerFields.FindField(pFeatureClass.ShapeFieldName) == j)
          {
            dr[j] = pFeatureClass.ShapeType.ToString();
          }
          else
          {
            dr[j] = pFeature.get_Value(j);
          }
       }
          dt.Rows.Add(dr);
          pFeature = pFeatureCursor.NextFeature();
      }
    dataGridView1.DataSource = dt;
    }
  catch (Exception exc)
  {
    MessageBox.Show("读取属性表失败:" + exc.Message);
    this.Dispose();
  }            
}



10、缩放至图层


private void 缩放至本图层ToolStripMenuItem_Click(object sender, EventArgs e)

{

            IGeoDataset pGeoDataset = SelectedLayer_TOC as IGeoDataset;

            IEnvelope pEnvelope = pGeoDataset.Extent;

            axMapControl1.Extent = pEnvelope;

 }




https://blog.sciencenet.cn/blog-3373120-1108354.html

上一篇:ArcGIS Engine二次开发学习(4)地图控件和布局控件联动
下一篇:ArcGIS Engine二次开发学习(6)属性表操作
收藏 IP: 121.69.12.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-26 21:57

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部