TOCControl本身没有实现此功能,需要开发人员编码实现,主要是利用HisTest()方法获取要移动的图层及图层索引:
①在OnMouseDown事件中获取需要移动的图层
②在OnMouseUp事件中,获取移动后的图层索引,利用IMap接口封装的MoveLayer()方法进行图层移动,完成后刷新TOCControl;
代码实现如下:
public ITOCControl mTOCControl;
public ILayer pMoveLayer;
public int toIndex;
private void MainFrm_Load(object sender, EventArgs e)
{
mTOCControl = axTOCControl1.Object as ITOCControl;
}
private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
esriTOCControlItem pItem = esriTOCControlItem.esriTOCControlItemNone;
IBasicMap pBasMap = null;
ILayer pLayer = null;
object pOther = null;
object pIndex = null;
if (e.button == 1)
{
mTOCControl.HitTest(e.x, e.y, ref pItem, ref pBasMap, ref pLayer, ref pOther, ref pIndex);
if (pItem == esriTOCControlItem.esriTOCControlItemLayer)
{
if (pLayer is IAnnotationSublayer) return;//如果是注记图层则返回
else
pMoveLayer = pLayer;
}
}
}
private void axTOCControl1_OnMouseUp(object sender, ITOCControlEvents_OnMouseUpEvent e)
{
esriTOCControlItem pItem = esriTOCControlItem.esriTOCControlItemNone;
IBasicMap pBasMap = null;
ILayer pLayer = null;
object pOther = null;
object pIndex = null;
if (e.button == 1)
{
mTOCControl.HitTest(e.x, e.y, ref pItem, ref pBasMap, ref pLayer, ref pOther, ref pIndex);
if (pMoveLayer!=pLayer)//如果是原图层则不用操作
{
IMap pMap = axMapControl1.Map;
ILayer pTempLayer;
for (int i = 0; i < pMap.LayerCount; i++)
{
pTempLayer = pMap.get_Layer(i);
if (pTempLayer == pLayer)//获取移动后的图层索引
toIndex = i;
}
pMap.MoveLayer(pMoveLayer, toIndex);
axMapControl1.ActiveView.Refresh();
mTOCControl.Update();
}
}
}