m_hookHelper.Hook = hook; }
public int GetCount() {
return 2; }
public void SetSubType(int SubType) {
m_subType = SubType; }
public override string Caption {
get {
if (m_subType == 1) return \ else return \ } }
public override bool Enabled {
get {
bool enabled = false; int i; if (m_subType == 1) {
for (i=0;i<=m_hookHelper.FocusMap.LayerCount - 1;i++) {
if(m_hookHelper.ActiveView.FocusMap.get_Layer(i).Visible== false)
{
enabled = true; break; } } } else {
for (i=0;i<=m_hookHelper.FocusMap.LayerCount - 1;i++) {
if(m_hookHelper.ActiveView.FocusMap.get_Layer(i).Visible== true)
{
enabled = true; break; } } }
return enabled; } } } }
移除图层类RemoveLayer代码:
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; namespace _sdnMap {
///
public sealed class RemoveLayer : BaseCommand {
private IMapControl3 m_mapControl; public RemoveLayer() {
base.m_caption = \ }
public override void OnClick() {
ILayer layer = (ILayer)m_mapControl.CustomProperty; m_mapControl.Map.DeleteLayer(layer); }
public override void OnCreate(object hook) {
m_mapControl = (IMapControl3)hook; } } }
放大至整个图层类ZoomToLayer:
using ESRI.ArcGIS.ADF.BaseClasses; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; namespace _sdnMap {
///
/// 放大至整个图层 ///
public sealed class ZoomToLayer : BaseCommand {
private IMapControl3 m_mapControl; public ZoomToLayer() {
base.m_caption = \ }
public override void OnClick() {
ILayer layer = (ILayer)m_mapControl.CustomProperty; m_mapControl.Extent = layer.AreaOfInterest; }
public override void OnCreate(object hook) {
m_mapControl = (IMapControl3)hook; } } }
以上三个工具或命令的实现代码比较简单,在此不过多的分析,请读者自行理解。
下面在Form1_Load函数中进行菜单项的添加,代码如下: //添加自定义菜单项到TOCCOntrol的Map菜单中 //打开文档菜单
m_menuMap.AddItem(new OpenNewMapDocument(m_controlsSynchronizer), -1, 0, false, esriCommandStyles.esriCommandStyleIconAndText); //添加数据菜单
m_menuMap.AddItem(new ControlsAddDataCommandClass(), -1, 1, false, esriCommandStyles.esriCommandStyleIconAndText); //打开全部图层菜单
m_menuMap.AddItem(new LayerVisibility(), 1, 2, false, esriCommandStyles.esriCommandStyleTextOnly); //关闭全部图层菜单
m_menuMap.AddItem(new LayerVisibility(), 2, 3, false, esriCommandStyles.esriCommandStyleTextOnly); //以二级菜单的形式添加内置的“选择”菜单
m_menuMap.AddSubMenu(\//以二级菜单的形式添加内置的“地图浏览”菜单
m_menuMap.AddSubMenu(\//添加自定义菜单项到TOCCOntrol的图层菜单中 m_menuLayer = new ToolbarMenuClass(); //添加“移除图层”菜单项
m_menuLayer.AddItem(new RemoveLayer(), -1, 0, false, esriCommandStyles.esriCommandStyleTextOnly); //添加“放大到整个图层”菜单项
m_menuLayer.AddItem(new ZoomToLayer(), -1, 1, true, esriCommandStyles.esriCommandStyleTextOnly); //设置菜单的Hook
m_menuLayer.SetHook(m_mapControl); m_menuMap.SetHook(m_mapControl);
3、弹出右键菜单
顾名思义,右键菜单是在鼠标右键按下的时候弹出,所以我们要添加TOCControl1控件的OnMouseDown事件,实现代码如下:
private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e) {
//如果不是右键按下直接返回 if (e.button != 2) return;
esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone; IBasicMap map = null; ILayer layer = null; object other = null; object index = null; //判断所选菜单的类型
m_tocControl.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index); //确定选定的菜单类型,Map或是图层菜单
if (item == esriTOCControlItem.esriTOCControlItemMap) m_tocControl.SelectItem(map, null); else
m_tocControl.SelectItem(layer, null);
//设置CustomProperty为layer (用于自定义的Layer命令) m_mapControl.CustomProperty = layer; //弹出右键菜单
if (item == esriTOCControlItem.esriTOCControlItemMap) m_menuMap.PopupMenu(e.x, e.y, m_tocControl.hWnd); if (item == esriTOCControlItem.esriTOCControlItemLayer) m_menuLayer.PopupMenu(e.x, e.y, m_tocControl.hWnd); }
同样的方法,我们也可以实现主地图控件的右键菜单,以方便地图浏览。添加MapControl1控件的OnMouseDown事件,实现代码如下:
///
/// 主地图控件的右键响应函数 ///
///
private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) {
if (e.button == 2) {
//弹出右键菜单
m_menuMap.PopupMenu(e.x,e.y,m_mapControl.hWnd); } }
4、编译运行
按F5编译运行程序,你会发现,原来右键菜单实现起来是这么的简单啊!
下一讲中,我将给大家带的是TOCControl控件中图层拖拽(Drag and Drop)的实现。
第七讲 图层符号选择器的实现(1)
在上一讲中,我们实现了右键菜单(ContextMenu)的添加与实现,在最后我预留给下一讲的问题是TOCControl控件图层拖拽的实现。后来发现此功能的实现异常简单,只要在TOCControl的属性页中,勾选“Enable Layer Drag and Drop”即可。 (教程Bug及优化方案1查看这里:http://www.3sdn.net/gis2dev/ae/2009-04-11/437.html) 这一讲,我们要实现的是图层符号选择器,与ArcMap中的Symbol Selector的类似。本讲较前几讲而言,些许有些复杂,不过只要仔细琢磨,认真操作,你就很容易实现如下所示的符号选择器。因为本讲篇幅较长,故我将其分成两个阶段,本文是第一阶段。
图1: 图2:
在AE开发中,符号选择器有两种实现方式。一是在程序中直接调用ArcMap中的符号选择器,如图2所示: 二是自定义符号选择器,如图1所示。 由于第一种方式前提是必须安装ArcGIS Desktop,其界面还是英文的,而对二次开发来说,大部分用户希望应该是中文界面。因此开发人员通常选择第二种方式,本讲也着重讲解第二种方式。 通过对《ArcGIS Engine+C#实例开发教程》前六讲的学习,我已经假定你已经基本熟悉C#语言和VS2005的操作,故在下面的教程中,我不准备说明每一步骤的具体操作方法,而只是说明操作步骤,以节省时间和篇幅。
1.直接调用ArcMap中的符号选择器
(1)添加ESRI.ArcGIS.DisplayUI的引用。分别在解决方案管理器和代码中添加引用。 (2)添加TOCControl的Double_Click事件。 (3)实现TOCControl的Double_Click事件。
因为种方法不是本讲的重点,故不对代码进行分析,有兴趣的读者请自行理解或结合后面的内容理解。代码如下: private void axTOCControl1_OnDoubleClick(object sender, ITOCControlEvents_OnDoubleClickEvent e) {
esriTOCControlItem toccItem = esriTOCControlItem.esriTOCControlItemNone; ILayer iLayer = null;
IBasicMap iBasicMap = null; object unk = null; object data = null; if (e.button == 1) {
axTOCControl1.HitTest(e.x, e.y, ref toccItem, ref iBasicMap, ref iLayer, ref unk, ref data);
System.Drawing.Point pos = new System.Drawing.Point(e.x, e.y); if (toccItem == esriTOCControlItem.esriTOCControlItemLegendClass) {
ESRI.ArcGIS.Carto.ILegendClass pLC = new LegendClassClass(); ESRI.ArcGIS.Carto.ILegendGroup pLG = new LegendGroupClass(); if (unk is ILegendGroup) {
pLG = (ILegendGroup)unk; }
pLC = pLG.get_Class((int)data); ISymbol pSym;
pSym = pLC.Symbol;
ESRI.ArcGIS.DisplayUI.ISymbolSelector pSS = new