如何在treewview 中 拖放, 在放的时候,放的那个节点不知道给如何确定?
解决方案 »
- Win7环境下打开OpenFileDialog 失败
- 如何判断串口是否被占用?(Visual Studio C#)
- 用C#windows应用程序做一个千千静听样的桌面歌词
- C#从数据库sql数据库把数据导到Excel里
- 托盘问题
- 初学者问题:关于COM与AxtiveX
- 问个问题,。NET做桌面应用程序???????
- 急!请教关于WEB TREEVIEW显示成一长行的问题?
- 奇怪问题:使用Request.QueryString传递一个中文参数,结果少了一个字!
- 请教:如何修改ICON?
- c# winform的datagridview中如何实现以下功能?
- c#如何实现改EXCEL表格中椭圆图形内的值?急
#region TreeView Drag/Drop
// Constants for the SendMessage() method.
private const int WM_HSCROLL = 276;
private const int WM_VSCROLL = 277;
private const int SB_LEFT = 6;
private const int SB_RIGHT = 7;
private const int SB_TOP = 6;
private const int SB_BOTTOM = 7;
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int wMsg,
int wParam, int lParam); // Node being dragged
private TreeNode dragNode = null;
// Temporary drop node for selection
private TreeNode tempDropNode = null; private void treeView_DragDrop(object sender, DragEventArgs e)
{
// Unlock updates
DragHelper.ImageList_DragLeave(this.treeView.Handle); // Get drop node
TreeNode dropNode = this.treeView.GetNodeAt(this.treeView.PointToClient(new Point(e.X, e.Y))); // If drop node isn't equal to drag node, add drag node as child of drop node
if (this.dragNode != dropNode)
{
// Remove drag node from parent
if (this.dragNode.Parent == null)
{
this.treeView.Nodes.Remove(this.dragNode);
}
else
{
this.dragNode.Parent.Nodes.Remove(this.dragNode);
} // Add drag node to drop node
dropNode.Nodes.Add(this.dragNode);
dropNode.ExpandAll(); // Set drag node to null
this.dragNode = null;
}
} private void treeView_DragEnter(object sender, DragEventArgs e)
{
DragHelper.ImageList_DragEnter(this.treeView.Handle, e.X - this.treeView.Left,
e.Y - this.treeView.Top);
} private void treeView_DragLeave(object sender, EventArgs e)
{
DragHelper.ImageList_DragLeave(this.treeView.Handle);
} private void treeView_DragOver(object sender, DragEventArgs e)
{
// Compute drag position and move image
Point formP = this.PointToClient(new Point(e.X, e.Y));
DragHelper.ImageList_DragMove(formP.X - this.treeView.Left, formP.Y - this.treeView.Top); // Get actual drop node
TreeNode dropNode = this.treeView.GetNodeAt(this.treeView.PointToClient(new Point(e.X, e.Y)));
if (dropNode == null)
{
e.Effect = DragDropEffects.None;
return;
} e.Effect = DragDropEffects.Move; // if mouse is on a new node select it
if (this.tempDropNode != dropNode)
{
DragHelper.ImageList_DragShowNolock(false);
this.treeView.SelectedNode = dropNode;
DragHelper.ImageList_DragShowNolock(true);
tempDropNode = dropNode;
} // Avoid that drop node is child of drag node
TreeNode tmpNode = dropNode;
while (tmpNode.Parent != null)
{
if (tmpNode.Parent == this.dragNode) e.Effect = DragDropEffects.None;
tmpNode = tmpNode.Parent;
} Point pt = this.treeView.PointToClient(new Point(e.X, e.Y));
// if mouse is near to the top, scroll up
if (pt.Y < 30)
{
// set actual node to the upper one
if (dropNode.PrevVisibleNode != null)
{
dropNode = dropNode.PrevVisibleNode; // hide drag image
DragHelper.ImageList_DragShowNolock(false); SendMessage(treeView.Handle, WM_VSCROLL, SB_TOP, 0); // show drag image
DragHelper.ImageList_DragShowNolock(true); }
}
// if mouse is near to the bottom, scroll down
else if (pt.Y > this.treeView.Size.Height - 30)
{
if (dropNode.NextVisibleNode != null)
{
dropNode = dropNode.NextVisibleNode; DragHelper.ImageList_DragShowNolock(false); SendMessage(treeView.Handle, WM_VSCROLL, SB_BOTTOM, 0); DragHelper.ImageList_DragShowNolock(true);
}
}
// if mouse is near to the left, scroll left
else if (pt.X < 30)
{
//dropNode = dropNode.NextVisibleNode; DragHelper.ImageList_DragShowNolock(false); SendMessage(treeView.Handle, WM_HSCROLL, SB_LEFT, 0); DragHelper.ImageList_DragShowNolock(true);
}
// if mouse is near to the right, scroll right
else if (pt.X > this.treeView.Size.Width - 30)
{
//dropNode = dropNode.NextVisibleNode; DragHelper.ImageList_DragShowNolock(false); SendMessage(treeView.Handle, WM_HSCROLL, SB_RIGHT, 0); DragHelper.ImageList_DragShowNolock(true);
}
} private void treeView_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
if (e.Effect == DragDropEffects.Move)
{
// Show pointer cursor while dragging
e.UseDefaultCursors = false;
this.treeView.Cursor = Cursors.Default;
}
else e.UseDefaultCursors = true;
} private void treeView_ItemDrag(object sender, ItemDragEventArgs e)
{
// Get drag node and select it
this.dragNode = (TreeNode)e.Item;
this.treeView.SelectedNode = this.dragNode; // Reset image list used for drag image
this.imageListDrag.Images.Clear();
this.imageListDrag.ImageSize = new Size(this.dragNode.Bounds.Size.Width + this.treeView.Indent, this.dragNode.Bounds.Height); // Create new bitmap
// This bitmap will contain the tree node image to be dragged
Bitmap bmp = new Bitmap(this.dragNode.Bounds.Width + this.treeView.Indent, this.dragNode.Bounds.Height); // Get graphics from bitmap
Graphics gfx = Graphics.FromImage(bmp); // Draw node icon into the bitmap
gfx.DrawImage(this.imageListTreeView.Images[dragNode.ImageIndex], 0, 0); // Draw node label into bitmap
gfx.DrawString(this.dragNode.Text,
this.treeView.Font,
new SolidBrush(this.treeView.ForeColor),
(float)this.treeView.Indent, 1.0f); // Add bitmap to imagelist
this.imageListDrag.Images.Add(bmp); // Get mouse position in client coordinates
Point p = this.treeView.PointToClient(Control.MousePosition); // Compute delta between mouse position and node bounds
int dx = p.X + this.treeView.Indent - this.dragNode.Bounds.Left;
int dy = p.Y - this.dragNode.Bounds.Top; // Begin dragging image
if (DragHelper.ImageList_BeginDrag(this.imageListDrag.Handle, 0, dx, dy))
{
// Begin dragging
this.treeView.DoDragDrop(bmp, DragDropEffects.Move);
// End dragging image
DragHelper.ImageList_EndDrag();
}
}
#endregion
#region TreeView拖动
private void tvFormel_ItemDrag(object sender, ItemDragEventArgs e)
{
TreeNode tn = e.Item as TreeNode;
//根节点不允许拖放操作
if ((e.Button == MouseButtons.Left) && (tn != null) && (tn.Parent != null))
{
this.tvFormel.DoDragDrop(tn, DragDropEffects.Copy | DragDropEffects.Move | DragDropEffects.Link);
}
//DoDragDrop(e.Item, DragDropEffects.Move);
} private void tvFormel_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode"))
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
} private void tvFormel_DragDrop(object sender, DragEventArgs e)
{
TreeNode NewNode;
if (e.Data.GetDataPresent(typeof(TreeNode)))
{
Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y)); TreeNode DestinationNode = ((TreeView)sender).GetNodeAt(pt); NewNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode"); DestinationNode.Nodes.Add((TreeNode)NewNode.Clone()); DestinationNode.Expand(); //删除已经移动的节点
NewNode.Remove(); }
}
#endregion