最近在写一个Windows应用程序,在系统中使用到了TreeView控件,但是Treeview在移动Node过程中显示和Window中显示的不一样,没有使用图片进行拖拽显示。无奈之余,想起Windows系统中还有这个功能,应该在系统存在API可以使用,最终获得了这个文件,在comctl32.dll中含有这些方法,于是完成这项功能,代码如下:
DragHelper.cs
public class DragHelper
{
[DllImport("comctl32.dll")]
public static extern bool InitCommonControls();
[DllImport("comctl32.dll", CharSet=CharSet.Auto)]
public static extern bool ImageList_BeginDrag(IntPtr himlTrack, int
iTrack, int dxHotspot, int dyHotspot);
[DllImport("comctl32.dll", CharSet=CharSet.Auto)]
public static extern bool ImageList_DragMove(int x, int y);
[DllImport("comctl32.dll", CharSet=CharSet.Auto)]
public static extern void ImageList_EndDrag();
[DllImport("comctl32.dll", CharSet=CharSet.Auto)]
public static extern bool ImageList_DragEnter(IntPtr hwndLock, int x, int y);
[DllImport("comctl32.dll", CharSet=CharSet.Auto)]
public static extern bool ImageList_DragLeave(IntPtr hwndLock);
[DllImport("comctl32.dll", CharSet=CharSet.Auto)]
public static extern bool ImageList_DragShowNolock(bool fShow);
static DragHelper()
{
InitCommonControls();
}
}
FormTest.cs中的关键代码:
private void treeView_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
{
// Get drag node and select it
this.dragNode = (TreeNode)e.Item;
this.treeView1.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.treeView1.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.treeView1.Indent, this.dragNode.Bounds.Height);
// Get graphics from bitmap
Graphics gfx = Graphics.FromImage(bmp);
//
DragHelper.cs
public class DragHelper
{
[DllImport("comctl32.dll")]
public static extern bool InitCommonControls();
[DllImport("comctl32.dll", CharSet=CharSet.Auto)]
public static extern bool ImageList_BeginDrag(IntPtr himlTrack, int
iTrack, int dxHotspot, int dyHotspot);
[DllImport("comctl32.dll", CharSet=CharSet.Auto)]
public static extern bool ImageList_DragMove(int x, int y);
[DllImport("comctl32.dll", CharSet=CharSet.Auto)]
public static extern void ImageList_EndDrag();
[DllImport("comctl32.dll", CharSet=CharSet.Auto)]
public static extern bool ImageList_DragEnter(IntPtr hwndLock, int x, int y);
[DllImport("comctl32.dll", CharSet=CharSet.Auto)]
public static extern bool ImageList_DragLeave(IntPtr hwndLock);
[DllImport("comctl32.dll", CharSet=CharSet.Auto)]
public static extern bool ImageList_DragShowNolock(bool fShow);
static DragHelper()
{
InitCommonControls();
}
}
FormTest.cs中的关键代码:
private void treeView_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
{
// Get drag node and select it
this.dragNode = (TreeNode)e.Item;
this.treeView1.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.treeView1.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.treeView1.Indent, this.dragNode.Bounds.Height);
// Get graphics from bitmap
Graphics gfx = Graphics.FromImage(bmp);
//