1.类中定义菜单变量
private IToolbarMenu mapMenu;//toc控件右键地图菜单
private IToolbarMenu layerMenu;//toc控件右键图层菜单
并在菜单中初始化
private void MainForm_Load(object sender, EventArgs e)
{
//get the MapControl
m_mapControl = (IMapControl3)axMapControl1.Object;
tocControl = axTOCControl1.Object as ITOCControl2;
//disable the Save menu (since there is no document yet)
//menuSaveDoc.Enabled = false;
//构造地图右键菜单
mapMenu = new ToolbarMenuClass();
mapMenu.AddItem(new LayerVisibility(), 1, 0, false, esriCommandStyles.esriCommandStyleIconAndText);
mapMenu.AddItem(new LayerVisibility(), 2, 1, false, esriCommandStyles.esriCommandStyleIconAndText);
//构造图层右键菜单
layerMenu = new ToolbarMenuClass();
layerMenu.AddItem(new LayerVisibility(), 1, 0, false, esriCommandStyles.esriCommandStyleIconAndText);
layerMenu.AddItem(new LayerVisibility(), 2, 1, true, esriCommandStyles.esriCommandStyleTextOnly);
//右键菜单绑定
mapMenu.SetHook(m_mapControl);
layerMenu.SetHook(m_mapControl);
}
2.自定义LayerVisibility类
public sealed class LayerVisibility : BaseCommand, ICommandSubType
{
private IHookHelper hookHelper;
private long subType;
public LayerVisibility()
{
}
public override void OnCreate(object hook)
{
hookHelper = new HookHelperClass();
hookHelper.Hook = hook;
}
public override void OnClick()
{
for (int i = 0; i <= hookHelper.FocusMap.LayerCount - 1; i++)
{
if (((hookHelper.FocusMap.get_Layer(i) as IFeatureLayer) as IFeatureSelection) != null)
{
string t = hookHelper.FocusMap.get_Layer(i).Name;
//((hookHelper.FocusMap.get_Layer(i) as IFeatureLayer) as IFeatureSelection).Clear();
}
if (subType == 1) hookHelper.FocusMap.get_Layer(i).Visible = true;
if (subType == 2) hookHelper.FocusMap.get_Layer(i).Visible = false;
}
hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
hookHelper.ActiveView.Refresh();
}
public override string Caption
{
get
{
if (subType == 1) return "显示所有图层";
else return "隐藏所有图层";
}
}
public override bool Enabled
{
get
{
bool enabled = false; int i;
if (subType == 1)
{
for (i = 0; i <= hookHelper.FocusMap.LayerCount - 1; i++)
{
if (hookHelper.ActiveView.FocusMap.get_Layer(i).Visible == false)
{
enabled = true;
break;
}
}
}
else
{
for (i = 0; i <= hookHelper.FocusMap.LayerCount - 1; i++)
{
if (hookHelper.ActiveView.FocusMap.get_Layer(i).Visible == true)
{
enabled = true;
break;
}
}
}
return enabled;
}
}
#region ICommandSubType 成员
public int GetCount()
{
return 2;
}
public void SetSubType(int SubType)
{
subType = SubType;
}
#endregion
}
3.TOCControl中单击事件
esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
IBasicMap map = null;
ILayer layer = null;
object other = null;
object index = null;
tocControl.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);
if (e.button == 2)//右键
{
m_mapControl.CustomProperty = layer;
if (item == esriTOCControlItem.esriTOCControlItemMap)//点击的是地图
{
tocControl.SelectItem(map, null);
mapMenu.PopupMenu(e.x, e.y, tocControl.hWnd);
}
if (item == esriTOCControlItem.esriTOCControlItemLayer)//点击的是图层
{
tocControl.SelectItem(layer, null);
//setSecAndEdit(layer.Name);
layerMenu.PopupMenu(e.x, e.y, tocControl.hWnd);
}
}