Beispiel #1
0
        private void BetterTreeView_ItemDrag(object sender, ItemDragEventArgs e)
        {
            // Get mouse position in client coordinates
            Point p = PointToClient(MousePosition);

            // Get drag node and select it
            dragNode     = (BetterTreeNode)e.Item;
            SelectedNode = dragNode;

            // Create the Graphics object from the node being dragged.
            Bitmap   bmp = new Bitmap(dragNode.Bounds.Width + Indent, dragNode.Bounds.Height);
            Graphics gfx = Graphics.FromImage(bmp);

            // Draw node icon into the bitmap
            gfx.DrawImage(ImageList.Images[dragNode.ImageIndex], 0, 0);

            // Draw node label into bitmap
            gfx.DrawString(dragNode.Text, Font, new SolidBrush(ForeColor), 18f, 1f);

            int dragImageWidth = dragNode.Bounds.Size.Width + Indent;

            if (dragImageWidth > 256)
            {
                dragImageWidth = 256;
            }

            imageListDrag = Helpers.GenerateImageList(new Bitmap[] { bmp },
                                                      dragImageWidth, dragNode.Bounds.Height);

            // Compute delta between mouse position and node bounds
            int dx = p.X + Indent - dragNode.Bounds.Left - Location.X;
            int dy = p.Y - dragNode.Bounds.Top - Location.Y;

            // Begin dragging image
            if (DragHelper.ImageList_BeginDrag(imageListDrag.Handle, 0, dx, dy))
            {
                // Begin dragging
                DoDragDrop(bmp, DragDropEffects.Move);
                // End dragging image
                DragHelper.ImageList_EndDrag();
            }
        }
Beispiel #2
0
        void TemplateTree_ItemDrag(object sender, ItemDragEventArgs e)
        {
            TreeNode Node     = e.Item as TreeNode;
            Boolean  IsModule = Convert.ToBoolean(Node.Tag);

            if (IsModule)
            {
                dragNode = e.Item as TreeNode;
                TemplateTree.SelectedNode = dragNode;

                int imageWidth = dragNode.Bounds.Size.Width + TemplateTree.Indent;

                imageListDrag.Images.Clear();
                imageListDrag.ImageSize = new Size(imageWidth > 255 ? 255 : imageWidth, dragNode.Bounds.Height);

                Bitmap   bmp = new Bitmap(dragNode.Bounds.Width + TemplateTree.Indent, dragNode.Bounds.Height);
                Graphics gfx = Graphics.FromImage(bmp);
                gfx.DrawImage(imageList1.Images[dragNode.ImageIndex], 0, 0);
                gfx.DrawString(dragNode.Text, TemplateTree.Font,
                               new SolidBrush(TemplateTree.ForeColor),
                               (float)TemplateTree.Indent, 1.0f);

                imageListDrag.Images.Add(bmp);

                Point p = TemplateTree.PointToClient(Control.MousePosition);

                int dx = p.X + TemplateTree.Indent - dragNode.Bounds.Left;
                int dy = p.Y - dragNode.Bounds.Top;

                if (DragHelper.ImageList_BeginDrag(imageListDrag.Handle, 0, dx, dy))
                {
                    TemplateTree.DoDragDrop(bmp, DragDropEffects.Move);
                    DragHelper.ImageList_EndDrag();
                }
            }
            else
            {
                return;
            }
        }
            /// <summary>
            /// Gets called when an item starts to be dragged.
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void _ItemDrag(object sender, ItemDragEventArgs e)
            {
                var treeView = sender as TreeView;

                if (treeView == null || treeView != this._treeView)
                {
                    return;
                }

                // do not allow root nodes to be dragged
                if (!this._canDragRootNodes && treeView.Nodes.Contains((TreeNode)e.Item))
                {
                    return;
                }

                // Get drag node and select it
                var treeNode = (TreeNode)e.Item;

                this._draggedNode     = treeNode;
                treeView.SelectedNode = this._draggedNode;

                // Reset image list used for drag image
                var imageList = this._dragImageList;

                imageList.Images.Clear();

                var nodeImage = treeNode.GetImage();

                imageList.ImageSize = new Drawing.Size(Math.Min(256, treeNode.Bounds.Size.Width + (nodeImage?.Width + 1 ?? 0)), Math.Min(256, treeNode.Bounds.Height));

                // Create new bitmap
                // This bitmap will contain the tree node image to be dragged
                var bmp = new Bitmap(imageList.ImageSize.Width, imageList.ImageSize.Height);

                // Get graphics from bitmap
                var gfx = Graphics.FromImage(bmp);

                // Draw node icon into the bitmap
                if (nodeImage != null)
                {
                    gfx.DrawImage(nodeImage, 0, 0);
                }

                // Draw node label into bitmap
                gfx.DrawString(treeNode.Text, treeView.Font, new SolidBrush(treeView.ForeColor), nodeImage?.Width + 1 ?? 0, 1.0f);

                // Add bitmap to imagelist
                imageList.Images.Add(bmp);

                // Compute hotspot
                const int dx = 16;
                const int dy = 16;

                // Begin dragging image
                if (!DragHelper.ImageList_BeginDrag(imageList.Handle, 0, -dx, -dy))
                {
                    return;
                }

                treeView.DoDragDrop(treeNode, DragDropEffects.Move);

                // End dragging image
                DragHelper.ImageList_EndDrag();
            }