ArcEngine在MapControl中使用符号选择器(Symbol Selector

ArcEngine在MapControl中使用符号选择器(Symbol Selector)

在ArcEngine中使用符号选择器一般有两种方式,一种是直接调用ArcMap的ISymbolSelector接口;一种是自定义实现。两种方式网上资源都很多,可以自行查找。本文主要讲符号选择器在MapControl中的使用(网上大部分资源都是在Toccontrol中使用)。其本质就是就是怎么获取LegendClass的问题

1.在TocControl中使用(也是使用方式最多的一种)

首先需要在TOCControl的鼠标单击或者双击事件中调用HitTest方法捕捉相应图层的LegendClass,这种方法是通过ArcEngine的TocControl获取LegendClass。方法如下:

public void HitTest ( int X, int Y, ref esriTOCControlItem ItemType, ref IBasicMap BasicMap, ref ILayerLayer, ref object Unk, ref object Data );

其中参数意义如下:

  • X,Y :鼠标点击的坐标;
  • ITemType: esriTOCControlItem枚举常量
  • BasicMap:绑定MapControl的IBasicMap接口
  • Layer:被点击的图层
  • Unk:TOCControl的LegendGroup对象
  • Data:LegendClass在LegendGroup中的Index

常用的代码如下:


    private void TOCLayer_OnDoubleClick(object sender, ITOCControlEvents_OnDoubleClickEvent e)
        {
            if (e.button != 1)
            {
                return;
            }
            esriTOCControlItem itemType = esriTOCControlItem.esriTOCControlItemNone;
            IBasicMap pBasicMap = null;
            ILayer pLayer = null;
            object unk = null;
            object data = null;
            TOCLayer.HitTest(e.x, e.y, ref itemType, ref pBasicMap, ref pLayer, ref unk, ref data);

            if (e.button == 1)
            {
                if (itemType == esriTOCControlItem.esriTOCControlItemLegendClass)
                {
                    ILegendClass pLegendClass = ((ILegendGroup)unk).get_Class((int)data);

                    FrmSymbolSelect SymbolSelectorFrm = new FrmSymbolSelect(pLegendClass, pLayer);

                    if (SymbolSelectorFrm.ShowDialog() == DialogResult.OK)
                    {
                        m_MapMain.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
                        pLegendClass.Symbol = SymbolSelectorFrm.pSymbol;
                        m_MapMain.ActiveView.Refresh();
                        TOCLayer.Refresh();
                    }
                }
            }
        }

2.自定义目录树,不用TOCControl控件

有时不想用ArcEngine的TocControl,想要使用自定义的目录树,但同样需要获取LengendClass,此时不能用TocControl的HitTest方法了。此时可以使用可以通过LegendInfo来获取:

参考博客:博客


    IFeatureLayer pFeatureLayer = treeFrame.SelectedNode.Tag as IFeatureLayer;
    ILegendInfo li = (ILegendInfo)pFeatureLayer;  
    ILegendGroup lg = li.get_LegendGroup(0);  
    SymbolSelectorFrm frm = new SymbolSelectorFrm(pLegend, pFeatureLayer);

3.既不使用Toccontrol,也不使用目录树,直接利用图层(layer)来使用

有时候自己不想使用Toccontrol,也不使用自定义目录树,只想在MapControl的的右键菜单中使用,或者把符号选择器做到ToolBar中使用,那么咱们通过一个图层来操作呢?其实质就是通过图层(layer)获取LengendClass,这里面用到了一些列的接口查询(QI),用到的接口有IFeatureLayer、ILegendInfo 、ILegendGroup、ILegendClass,本质和自定义目录树是一样的。

关键代码如下:



    ILayer pLayer = this.MapFrom.get_Layer(0);
    //中间通过一系列的接口查询把ILayer转为ILegendClass
    IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
    ILegendInfo lengendInfo = (ILegendInfo)pFeatureLayer;
    ILegendGroup legendGroup = lengendInfo.get_LegendGroup(0);
    ILegendClass pLegendClass = legendGroup.get_Class(0); //获取到LegendClass  

    FrmSymbolSelect SymbolSelectorFrm = new FrmSymbolSelect(pLegendClass, pLayer);

    if (SymbolSelectorFrm.ShowDialog() == DialogResult.OK)
    {
        MapFrom.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
        pLegendClass.Symbol = SymbolSelectorFrm.pSymbol;
        MapFrom.ActiveView.Refresh();
    }

我这里是自定义符号选择器,然后不使用Toccontrol,也不使用目录树,直接利用图层(layer),并做成了ToolBar的样式,最终的效果图如下所示:

ToolBar样式
自定义符号选择器

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ArcEngine使用Select Elements选择元素的步骤如下: 1. 获取MapControl或PageLayoutControl对象。 2. 创建一个新的SelectionEnvironment对象,并设置选择条件、符号等属性。 3. 调用MapControl或PageLayoutControl的ClearSelection方法,清空当前选择集。 4. 调用MapControl或PageLayoutControlSelectByShape或SelectByPoint等方法,根据指定的几何图形或位置选择元素。 5. 遍历MapControl或PageLayoutControlSelection集合,获取所选元素的ID号。 6. 根据ID号获取元素对象,执行相应操作。 以下是一个示例代码: ```C# // 获取MapControl对象 ESRI.ArcGIS.Controls.AxMapControl mapControl = this.axMapControl1; // 创建SelectionEnvironment对象 ESRI.ArcGIS.Carto.ISelectionEnvironment selectionEnv = new ESRI.ArcGIS.Carto.SelectionEnvironmentClass(); selectionEnv.CombinationMethod = ESRI.ArcGIS.Carto.esriSelectionResultEnum.esriSelectionResultNew; selectionEnv.SelectionColor = GetRGBColor(255, 0, 0); // 设置选择符号颜色 // 清空当前选择集 mapControl.Map.ClearSelection(); // 创建选择几何图形并进行选择 ESRI.ArcGIS.Geometry.IPoint point = new ESRI.ArcGIS.Geometry.PointClass(); point.PutCoords(x, y); // 设置点的坐标 ESRI.ArcGIS.Display.IScreenDisplay screenDisplay = mapControl.ActiveView.ScreenDisplay; double tolerance = screenDisplay.DisplayTransformation.FromPoints(5); // 设置选择容差 ESRI.ArcGIS.Display.IDisplayFeedback displayFeedback = new ESRI.ArcGIS.Display.RubberBandFeedbackClass(); displayFeedback.Display = screenDisplay; ESRI.ArcGIS.Geometry.IGeometry geometry = displayFeedback.TrackPoint(); // 获取选择几何图形 mapControl.Map.SelectByShape(geometry, selectionEnv, false); // 根据几何图形进行选择 // 遍历选择集合并获取元素对象 ESRI.ArcGIS.Carto.IEnumFeature enumFeature = (ESRI.ArcGIS.Carto.IEnumFeature)mapControl.Map.FeatureSelection; ESRI.ArcGIS.Geodatabase.IFeature feature; enumFeature.Reset(); while ((feature = enumFeature.Next()) != null) { // 根据ID号获取元素对象并执行操作 int featureID = feature.OID; ESRI.ArcGIS.Carto.IFeatureLayer featureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)mapControl.Map.get_Layer(0); ESRI.ArcGIS.Carto.IFeatureSelection featureSelection = (ESRI.ArcGIS.Carto.IFeatureSelection)featureLayer; ESRI.ArcGIS.Geodatabase.IQueryFilter queryFilter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass(); queryFilter.WhereClause = "OBJECTID = " + featureID; ESRI.ArcGIS.Geodatabase.IFeatureCursor featureCursor = featureLayer.Search(queryFilter, false); ESRI.ArcGIS.Geodatabase.IFeature feature2 = featureCursor.NextFeature(); // 执行操作 // ... } ``` 在上述代码,首先获取MapControl对象,并创建SelectionEnvironment对象,设置选择条件。然后调用MapControl的ClearSelection方法,清空当前选择集。接着创建选择几何图形,使用MapControlSelectByShape方法进行选择。最后遍历选择集合,根据ID号获取元素对象,并执行相应操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值