将上篇文章进行整理,将功能进一步封装到Treeview控件中,开发出一个可以进行拖拽,并且在拖拽过程中可以显示图片的TreeView控件,名称叫做TreeviewExtend。
代码如下:
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();
}
}
/// <summary>
/// TreeViewDragImage
/// this Class Extended Treeview,
/// When TreeNode Drag this will Show the Image Draged
/// </summary>
public class TreeViewExtend : TreeView
{
#region Private Type
/// <summary>
/// Timer for scrolling
/// </summary>
private Timer timer = new Timer();
/// <summary>
/// Node being dragged
/// </summary>
private TreeNode dragNode = null;
/// <summary>
/// Temporary drop node for selection
/// </summary>
private TreeNode tempDropNode = null;
#endregion
#region Property
/// <summary>
/// Drag 过程中的图片
/// </summary>
private ImageList _imageListDrag;
/// <summary>
/// Drag 过程中的图片
/// </summary>
public ImageList ImageListDrag
{
get
{
if (this._imageListDrag == null)
{
this._imageListDrag = new ImageList();
}
return _imageListDrag
代码如下:
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();
}
}
/// <summary>
/// TreeViewDragImage
/// this Class Extended Treeview,
/// When TreeNode Drag this will Show the Image Draged
/// </summary>
public class TreeViewExtend : TreeView
{
#region Private Type
/// <summary>
/// Timer for scrolling
/// </summary>
private Timer timer = new Timer();
/// <summary>
/// Node being dragged
/// </summary>
private TreeNode dragNode = null;
/// <summary>
/// Temporary drop node for selection
/// </summary>
private TreeNode tempDropNode = null;
#endregion
#region Property
/// <summary>
/// Drag 过程中的图片
/// </summary>
private ImageList _imageListDrag;
/// <summary>
/// Drag 过程中的图片
/// </summary>
public ImageList ImageListDrag
{
get
{
if (this._imageListDrag == null)
{
this._imageListDrag = new ImageList();
}
return _imageListDrag