WinForm TreeView中拖拽过程中显示图片 TreeViewExtend

本文介绍了如何扩展WinForm TreeView控件,实现拖拽节点时显示拖拽图片的功能。通过自定义DragHelper类和TreeViewExtend类,实现了在拖拽过程中显示图片的效果,同时包含了滚动、拖放操作等细节处理。
摘要由CSDN通过智能技术生成
      将上篇文章进行整理,将功能进一步封装到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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值