最简单的方法,获取拖动过程中的插入位置:
private DragInsertPosition AjustDirection(object sender, DragEventArgs e)
{
TreeListNode dragNode, targetNode;
TreeList tl = sender as TreeList;
Point p = tl.PointToClient(new Point(e.X, e.Y));
dragNode = e.Data.GetData(typeof(TreeListNode)) as TreeListNode;
TreeListHitInfo hit = tl.CalcHitInfo(p);
PropertyInfo pi = typeof(TreeList).GetProperty("Handler", BindingFlags.Instance | BindingFlags.NonPublic);
TreeListHandler handler = (TreeListHandler)pi.GetValue(tl, null);
return handler.StateData.DragInfo.DragInsertPosition;
}
然后在事件里判断就行
private void TreeList_DragOver(object sender, DragEventArgs e)
{
TreeListNode dragNode, targetNode;
TreeList tl = sender as TreeList;
Point p = tl.PointToClient(new Point(e.X, e.Y));
dragNode = e.Data.GetData(typeof(TreeListNode)) as TreeListNode;
targetNode = tl.CalcHitInfo(p).Node;
e.Effect = GetDragDropEffects(dragNode,targetNode, AjustDirection(sender,e));
}
GetDragDropEffects()方法里进行源、目标、插入位置的综合判断,自己写...
DragInsertPosition为TreeList内置枚举,没啥可说的
public enum DragInsertPosition
{
None = 0,
AsChild = 1,
Before = 2,
After = 3
}
Done!