private void _DrawFolderTopPlaceholders(TreeNode hoveredNode) { var treeView = hoveredNode.TreeView; Contract.Assert(treeView != null, "Node must belong to a TreeView"); var g = treeView.CreateGraphics(); var nodeImage = hoveredNode.GetImage(); var imageWidth = nodeImage == null ? 0 : nodeImage.Size.Width + 8; var leftPos = hoveredNode.Bounds.Left - imageWidth; var rightPos = treeView.Width - 4; var leftTriangle = new[] { new Drawing.Point(leftPos, hoveredNode.Bounds.Top - 4), new Drawing.Point(leftPos, hoveredNode.Bounds.Top + 4), new Drawing.Point(leftPos + 4, hoveredNode.Bounds.Y), new Drawing.Point(leftPos + 4, hoveredNode.Bounds.Top - 1), new Drawing.Point(leftPos, hoveredNode.Bounds.Top - 5) }; var rightTriangle = new[] { new Drawing.Point(rightPos, hoveredNode.Bounds.Top - 4), new Drawing.Point(rightPos, hoveredNode.Bounds.Top + 4), new Drawing.Point(rightPos - 4, hoveredNode.Bounds.Y), new Drawing.Point(rightPos - 4, hoveredNode.Bounds.Top - 1), new Drawing.Point(rightPos, hoveredNode.Bounds.Top - 5) }; g.FillPolygon(Brushes.Black, leftTriangle); g.FillPolygon(Brushes.Black, rightTriangle); g.DrawLine(new Pen(Color.Black, 2), new Drawing.Point(leftPos, hoveredNode.Bounds.Top), new Drawing.Point(rightPos, hoveredNode.Bounds.Top)); }
private void _DrawLeafBottomPlaceholders(TreeNode hoveredNode, TreeNode parentNodeDragDrop) { var treeView = hoveredNode.TreeView; Contract.Assert(treeView != null, "Node must belong to a TreeView"); var g = treeView.CreateGraphics(); var nodeImage = hoveredNode.GetImage(); var imageWidth = nodeImage == null ? 0 : nodeImage.Size.Width + 8; // Once again, we are not dragging to node over, draw the placeholder using the ParentDragDrop bounds int leftPos, rightPos; if (parentNodeDragDrop != null) { leftPos = parentNodeDragDrop.Bounds.Left - (parentNodeDragDrop.GetImage().Size.Width + 8); } else { leftPos = hoveredNode.Bounds.Left - imageWidth; } rightPos = treeView.Width - 4; var leftTriangle = new[] { new Drawing.Point(leftPos, hoveredNode.Bounds.Bottom - 4), new Drawing.Point(leftPos, hoveredNode.Bounds.Bottom + 4), new Drawing.Point(leftPos + 4, hoveredNode.Bounds.Bottom), new Drawing.Point(leftPos + 4, hoveredNode.Bounds.Bottom - 1), new Drawing.Point(leftPos, hoveredNode.Bounds.Bottom - 5) }; var rightTriangle = new[] { new Drawing.Point(rightPos, hoveredNode.Bounds.Bottom - 4), new Drawing.Point(rightPos, hoveredNode.Bounds.Bottom + 4), new Drawing.Point(rightPos - 4, hoveredNode.Bounds.Bottom), new Drawing.Point(rightPos - 4, hoveredNode.Bounds.Bottom - 1), new Drawing.Point(rightPos, hoveredNode.Bounds.Bottom - 5) }; g.FillPolygon(Brushes.Black, leftTriangle); g.FillPolygon(Brushes.Black, rightTriangle); g.DrawLine(new Pen(Color.Black, 2), new Drawing.Point(leftPos, hoveredNode.Bounds.Bottom), new Drawing.Point(rightPos, hoveredNode.Bounds.Bottom)); }
/// <summary> /// Gets called when the mouse moves with a dragged item. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void _DragOver(object sender, DragEventArgs e) { var treeView = sender as TreeView; if (treeView == null || treeView != this._treeView) { return; } var currentScreenPoint = new Drawing.Point(e.X, e.Y); // Compute drag position and move image var form = treeView.FindForm(); if (form == null) { DragHelper.ImageList_DragShowNolock(false); } else { var tvP = treeView.GetLocationOnForm(); var formP = form.PointToClient(currentScreenPoint); formP.Offset(-tvP.X, -tvP.Y); DragHelper.ImageList_DragMove(formP.X, formP.Y); } var hoveredNode = treeView.GetNodeAt(treeView.PointToClient(currentScreenPoint)); if (hoveredNode == null) { return; } var draggedNode = this._draggedNode; #region If hoveredNode is a child of the dragged node then cancel if (hoveredNode.IsChildOf(draggedNode)) { this.NodeMap = string.Empty; return; } #endregion // A bit long, but to summarize, process the following code only if the nodeover is null // and either the nodeover is not the same thing as nodemoving UNLESSS nodeover happens // to be the last node in the branch (so we can allow drag & drop below a parent branch) if (hoveredNode == draggedNode && (hoveredNode.Parent == null || hoveredNode.Index != (hoveredNode.Parent.Nodes.Count - 1))) { return; } var offsetY = treeView.PointToClient(Cursor.Position).Y - hoveredNode.Bounds.Top; this._ResetDraw(); if (!this._IsFolderNode(hoveredNode)) { #region Standard Node if (offsetY < (hoveredNode.Bounds.Height / 2)) { //this.lblDebug.Text = "top"; #region Store the placeholder info into a pipe delimited string this.SetNewNodeMap(hoveredNode, false); if (this.SetMapsEqual()) { return; } #endregion this._SetAndDraw(PlaceHolderType.LeafTop, hoveredNode); } else { //this.lblDebug.Text = "bottom"; #region Allow drag drop to parent branches TreeNode ParentDragDrop = null; // If the node the mouse is over is the last node of the branch we should allow // the ability to drop the "nodemoving" node BELOW the parent node if (hoveredNode.Parent != null && hoveredNode.Index == (hoveredNode.Parent.Nodes.Count - 1)) { var XPos = treeView.PointToClient(Cursor.Position).X; if (XPos < hoveredNode.Bounds.Left) { ParentDragDrop = hoveredNode.Parent; var image = ParentDragDrop.GetImage(); if (XPos < (ParentDragDrop.Bounds.Left - (image?.Size.Width ?? 0))) { if (ParentDragDrop.Parent != null) { ParentDragDrop = ParentDragDrop.Parent; } } } } #endregion #region Store the placeholder info into a pipe delimited string // Since we are in a special case here, use the ParentDragDrop node as the current "nodeover" this.SetNewNodeMap(ParentDragDrop ?? hoveredNode, true); if (this.SetMapsEqual()) { return; } #endregion this._SetAndDraw(PlaceHolderType.LeafBottom, hoveredNode, ParentDragDrop); } #endregion } else { #region Folder Node if (offsetY < (hoveredNode.Bounds.Height / 3)) { //this.lblDebug.Text = "folder top"; #region Store the placeholder info into a pipe delimited string this.SetNewNodeMap(hoveredNode, false); if (this.SetMapsEqual()) { return; } #endregion this._SetAndDraw(PlaceHolderType.FolderTop, hoveredNode); } else if ((hoveredNode.Parent != null && hoveredNode.Index == 0) && (offsetY > (hoveredNode.Bounds.Height - (hoveredNode.Bounds.Height / 3)))) { //this.lblDebug.Text = "folder bottom"; #region Store the placeholder info into a pipe delimited string this.SetNewNodeMap(hoveredNode, true); if (this.SetMapsEqual()) { return; } #endregion this._SetAndDraw(PlaceHolderType.FolderTop, hoveredNode); } else { //this.lblDebug.Text = "folder over"; if (hoveredNode.Nodes.Count > 0) { DragHelper.ImageList_DragShowNolock(false); hoveredNode.Expand(); this._SetAndDraw(PlaceHolderType.AddToFolder, hoveredNode); } else { #region Prevent the node from being dragged onto itself if (draggedNode == hoveredNode) { return; } #endregion #region If hoveredNode is a child then cancel if (hoveredNode.IsChildOf(draggedNode)) { this.NodeMap = string.Empty; return; } #endregion #region Store the placeholder info into a pipe delimited string this.SetNewNodeMap(hoveredNode, false); this.NewNodeMap = this.NewNodeMap.Insert(this.NewNodeMap.Length, _NODEMAP_DELIMITER + "0"); if (this.SetMapsEqual()) { return; } #endregion this._SetAndDraw(PlaceHolderType.AddToFolder, hoveredNode); } } #endregion } }