コード例 #1
0
ファイル: TreeViewDrager.cs プロジェクト: binCodeCW/IOT
        /// <summary>
        /// 当用户开始拖动节点时发生。
        /// </summary>
        private void treeView_ItemDrag(object sender, ItemDragEventArgs e)
        {
            // 取得拖动的节点,并选中它。
            this.dragNode = (TreeNode)e.Item;
            this.tempTreeView.SelectedNode = this.dragNode;
            //this.nodeImage =  //知道被拖动的节点,就应该知道节点的图标是什么?

            // 重新设置拖动节点的图标
            this.imageListDrag.Images.Clear();
            this.imageListDrag.ImageSize = new Size(this.dragNode.Bounds.Size.Width + this.tempTreeView.Indent, this.dragNode.Bounds.Height);

            // 创建新图标
            // 这个图标将包含被拖动的树节点的图象
            Bitmap bitmap = new Bitmap(this.dragNode.Bounds.Width + this.tempTreeView.Indent, this.dragNode.Bounds.Height);

            Graphics graphics = Graphics.FromImage(bitmap);

            // 在图标里包含被拖动节点的图象
            if (this.imageListTreeView != null)
            {
                graphics.DrawImage(this.imageListTreeView.Images[0], 0, 0);
            }

            // 把被拖动节点的标签
            graphics.DrawString(this.dragNode.Text,
                                this.tempTreeView.Font,
                                new SolidBrush(this.tempTreeView.ForeColor),
                                (float)this.tempTreeView.Indent, 1.0f);

            this.imageListDrag.Images.Add(bitmap);

            Point point     = this.tempTreeView.PointToClient(Control.MousePosition);
            int   dxHotspot = point.X + this.tempTreeView.Indent - this.dragNode.Bounds.Left;
            int   dyHotspot = point.Y - this.dragNode.Bounds.Top;

            // 开始拖拽图象
            if (DragHelper.ImageList_BeginDrag(this.imageListDrag.Handle, 0, dxHotspot, dyHotspot))
            {
                // 开始
                this.tempTreeView.DoDragDrop(bitmap, DragDropEffects.Move);
                // 结束
                DragHelper.ImageList_EndDrag();
            }
        }