基本思路如下:利用鼠標左鍵將需要調整順序的圖層移動至目標位置。
①需要涉及到TOCControl的OnMouseDown事件來獲取要調整的圖層,
②OnMouseUp事件獲得目標圖層和索引號,
③再利用IMap提供的MoveLayer方法,將相應調整axMapControl中的圖層的顯示順序。
④最后使用TOCControl的Update方法來更新TOCControl控件中的顯示順序;
具體實現代碼如下:
//全局變量
public ITOCControl mTOCControl;
public ILayer pMoveLayer;//需要被調整的圖層;
public int toIndex;//將要調整到的目標圖層的索引;
//窗體加載
private void Form1_Load(object sender, EventArgs e)
{
this.axTOCControl1.SetBuddyControl(this.axMapControl1);
mTOCControl=this.axTOCControl1.Object as ITOCControl;
}
//TOCControl控件的OnMouseDown事件,獲得需要被調整的圖層
private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
esriTOCControlItem item=esriTOCControlItem.esriTOCControlItemNone;
if (e.button == 1)
{
IBasicMap map = null;
ILayer layer = null;
object other = null;
object index = null;
mTOCControl.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);
if (item == esriTOCControlItem.esriTOCControlItemLayer)
{
if(layer is IAnnotationSublayer)
{
return;
}
else
{
pMoveLayer = layer;
}
}
}
}
//TOCControl控件的OnMouseUp事件,獲得目標圖層及索引
//並實現axMapControl中視圖顯示的索引的變化,並更新TOCControl控件中的顯示順序
private void axTOCControl1_OnMouseUp(object sender, ITOCControlEvents_OnMouseUpEvent e)
{
if (e.button == 1)
{
esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
IBasicMap map = null;
ILayer layer = null;
object other = null;
object index = null;
mTOCControl.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);
IMap pMap = this.axMapControl1.ActiveView.FocusMap;
if(item==esriTOCControlItem.esriTOCControlItemLayer||layer!=null)
{
if (pMoveLayer != null)
{
ILayer pTempLayer;
for (int i = 0; i < pMap.LayerCount; i++)
{
pTempLayer = pMap.get_Layer(i);
if (pTempLayer == layer)
{
toIndex = i;
}
}
pMap.MoveLayer(pMoveLayer,toIndex);
axMapControl1.ActiveView.Refresh();
mTOCControl.Update();
}
}
}
}