/// <summary> /// Initializes a new instance of the CTreeNode class with the specified name and control. /// </summary> /// <param name="name"></param> /// <param name="control"></param> public CTreeNode(string name, Control control) { Nodes = new CTreeNodeCollection(this); Level = 0; Visible = true; IsExpanded = true; Name = name; Control = control; }
/// <summary> /// Initializes a new instance of the CTreeView class. /// </summary> public CTreeView() : base() { suspendUpdateCount = 0; //InitializeComponent(); BeginUpdate(); Nodes = new CTreeNodeCollection(this); PathSeparator = @"\"; //AutoScroll = true; //AllowDrop = true; ShowPlusMinus = true; ShowLines = true; ShowRootLines = true; _selectedNodes = new List <CTreeNode>(); MinimizeCollapsed = true; SelectionMode = CTreeViewSelectionMode.Multi; DragAndDropMode = CTreeViewDragAndDropMode.ReplaceReorder; IndentDepth = 30; IndentWidth = 10; selectionPen = new Pen(Color.Black, 1.0F); selectionPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; selectionBrush = new SolidBrush(SystemColors.Highlight); _LinesPen = new Pen(Color.Black, 1.0F); _LinesPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; Bitmap imagePlus = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("ControlTreeView.Resources.plus.bmp")); Bitmap imageMinus = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("ControlTreeView.Resources.minus.bmp")); PlusMinus = new CTreeViewPlusMinus(imagePlus, imageMinus); scrollTimer = new Timer(); scrollTimer.Tick += new EventHandler(scrollTimer_Tick); scrollTimer.Interval = 1; dragDropLinePen = new Pen(Color.Black, 2.0F); GraphicsPath path = new GraphicsPath(); path.AddLines(new Point[] { new Point(0, 0), new Point(1, 1), new Point(-1, 1), new Point(0, 0) }); CustomLineCap cap = new CustomLineCap(null, path); cap.WidthScale = 1.0f; dragDropLinePen.CustomStartCap = cap; dragDropLinePen.CustomEndCap = cap; this.DoubleBuffered = true; //this.ResizeRedraw = true; //this.AutoScrollMinSize = new Size(0, 0); EndUpdate(); }
/// <summary> /// Sets the drag destination nodes according to specified cursor position. /// </summary> /// <param name="dragPosition">The position of mouse cursor during drag.</param> internal void SetDragTargetPosition(Point dragPosition) { CTreeNode destinationNode = null; CTreeNodeCollection destinationCollection = Nodes; Nodes.TraverseNodes(node => node.Visible && node.BoundsSubtree.Contains(dragPosition), node => { destinationNode = node; destinationCollection = node.Nodes; }); if (destinationNode != null && destinationNode.Bounds.Contains(dragPosition)) //Drag position within node { //Find drag position within node int delta, coordinate, firstBound, secondBound; if (DrawStyle == CTreeViewDrawStyle.VerticalDiagram) { delta = destinationNode.Bounds.Width / 4; coordinate = dragPosition.X; firstBound = destinationNode.Bounds.Left; secondBound = destinationNode.Bounds.Right; } else { delta = destinationNode.Bounds.Height / 4; coordinate = dragPosition.Y; firstBound = destinationNode.Bounds.Top; secondBound = destinationNode.Bounds.Bottom; } if (coordinate >= firstBound + delta && coordinate <= secondBound - delta) { updateDragTargetPosition(destinationNode); return; } else if (coordinate < firstBound + delta) //before { updateDragTargetPosition(destinationNode.PrevNode, destinationNode); return; } else if (coordinate > secondBound - delta) //after { updateDragTargetPosition(destinationNode, destinationNode.NextNode); return; } } else //Drag position out of the nodes { //Check drag position between two nodes CTreeNode upperNode = null, lowerNode = null; bool isBetween = false; for (int count = 0; count <= destinationCollection.Count - 2; count++) { upperNode = destinationCollection[count]; lowerNode = destinationCollection[count + 1]; Point betweenLocation = Point.Empty; Size betweenSize = Size.Empty; if (DrawStyle == CTreeViewDrawStyle.VerticalDiagram) { betweenLocation = new Point(upperNode.BoundsSubtree.Right, upperNode.BoundsSubtree.Top); betweenSize = new Size(lowerNode.BoundsSubtree.Left - upperNode.BoundsSubtree.Right, Math.Max(upperNode.BoundsSubtree.Height, lowerNode.BoundsSubtree.Height)); } else { betweenLocation = new Point(upperNode.BoundsSubtree.Left, upperNode.BoundsSubtree.Bottom); betweenSize = new Size(Math.Max(upperNode.BoundsSubtree.Width, lowerNode.BoundsSubtree.Width), lowerNode.BoundsSubtree.Top - upperNode.BoundsSubtree.Bottom); } Rectangle betweenRectangle = new Rectangle(betweenLocation, betweenSize); if (betweenRectangle.Contains(dragPosition)) { isBetween = true; break; } } if (isBetween) //Drag position between two nodes { updateDragTargetPosition(upperNode, lowerNode); return; } else if (destinationNode != null) { Rectangle ownerBounds = destinationNode.Bounds; bool isAbove, isBelow; if (DrawStyle == CTreeViewDrawStyle.VerticalDiagram) { isAbove = (dragPosition.X <= ownerBounds.Left); isBelow = (dragPosition.X >= ownerBounds.Right); } else { isAbove = (dragPosition.Y <= ownerBounds.Top); isBelow = (dragPosition.Y >= ownerBounds.Bottom); } if (isAbove) //before { updateDragTargetPosition(destinationNode.PrevNode, destinationNode); return; } else if (isBelow) //after { updateDragTargetPosition(destinationNode, destinationNode.NextNode); return; } } } updateDragTargetPosition(); }