IMapControl2 m_pMapCtrl = axMapControl.Object as IMapControl2;
IMap m_pMap = axMapControl.Map;
ITOCControl m_pTocCtrl = axTOCControl1.Object as ITOCControl;
IToolbarControl m_pToolbarCtrl = axToolbarControl1.Object as IToolbarControl;
m_pTocCtrl.SetBuddyControl(axMapControl.Object);//关联MapControl控件
m_pToolbarCtrl.SetBuddyControl(axMapControl.Object);//关联MapControl控件
private void OnTocCtrlMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
try
{
esriTOCControlItem eTocCtrlItem = esriTOCControlItem.esriTOCControlItemNone;
IBasicMap pBasicMap = null;
ILayer pLyrSelect = null;
object other = null;
object index = null;
axTOCControl1.HitTest(e.x, e.y, ref eTocCtrlItem, ref pBasicMap, ref pLyrSelect, ref other, ref index);
if (e.button == 2)
{ //右键
if (eTocCtrlItem == esriTOCControlItem.esriTOCControlItemMap)
{
axTOCControl1.SelectItem(pBasicMap, null);
}
else if (eTocCtrlItem == esriTOCControlItem.esriTOCControlItemLayer)
{
axTOCControl1.SelectItem(pLyrSelect, null);
(m_pMapCtrl as IMapControl3).CustomProperty = pLyrSelect;//保存当前选中的层
}
if (eTocCtrlItem == esriTOCControlItem.esriTOCControlItemMap)//右击选中根节点
{
contextMapMenuStrip.Show(axTOCControl1, e.x, e.y);
}
if (eTocCtrlItem == esriTOCControlItem.esriTOCControlItemLayer)//右击选中子节点
{
contextLayerMenuStrip.Show(axTOCControl1, e.x, e.y);
}
}
else if (e.button == 1)
{ //左键
if (eTocCtrlItem == esriTOCControlItem.esriTOCControlItemLegendClass)
{ //显示图例
//new ESRI.ArcGIS.Display.Symbol.SymbolSelectionDialog().ShowDialog();
}
if (eTocCtrlItem == esriTOCControlItem.esriTOCControlItemLayer)
{
axTOCControl1.SelectItem(pLyrSelect, null);
if (pLyrSelect is GroupLayerClass) //组合图层
{
bool bVisible = pLyrSelect.Visible;
GroupLayerClass pGroupLayer = pLyrSelect as GroupLayerClass;
for (int i = 0; i <= pGroupLayer.Count - 1; i++)
{
pGroupLayer.get_Layer(i).Visible = !bVisible;
}
}
else // 单一的图层
{
if (pBasicMap != null)
{
ILayer pLyrTmp = null;
int iCount = pBasicMap.LayerCount;
bool bHasFindGroupLayer = false;
for (int index1 = 0; index1 < iCount; index1++)
{
pLyrTmp = pBasicMap.get_Layer(index1);
if (pLyrTmp is GroupLayerClass)
{
GroupLayerClass pGroupLayer = pLyrTmp as GroupLayerClass;
for (int i = 0; i <= pGroupLayer.Count - 1; i++)
{
if (pGroupLayer.get_Layer(i).Name == pLyrSelect.Name)
{
pGroupLayer.Visible = true;
bHasFindGroupLayer = true;
break;
}
}
}
if (bHasFindGroupLayer == true)
break;
}
}
}
}
}
}
catch (Exception ee)
{
System.Diagnostics.Debug.WriteLine(ee.ToString());
}
}