コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDragDrop(System.Windows.Forms.DragEventArgs e)
        {
            // Custom cursor handling
            if (this._dragCursorType == DragCursorType.DragIcon)
            {
                this.Cursor = Cursors.Default;
            }

            this._formDrag.Visible = false;

            // Check it's a treenode being dragged
            if (e.Data.GetDataPresent("UIMapToolbox.UI.DragData", false))
            {
                DragData        dragData  = (DragData)e.Data.GetData("UIMapToolbox.UI.DragData");
                List <TreeNode> dragNodes = dragData.Nodes;

                // Get the target node from the mouse coords
                Point    pt         = ((TreeView)this).PointToClient(new Point(e.X, e.Y));
                TreeNode targetNode = this.GetNodeAt(pt);

                // De-color it
                targetNode.BackColor = SystemColors.HighlightText;
                targetNode.ForeColor = SystemColors.ControlText;

                CustomDragCompleteEventArgs ea = new CustomDragCompleteEventArgs();

                foreach (TreeNode dragNode in dragNodes)
                {
                    // 1) Check we're not dragging onto ourself
                    // 2) Check we're not dragging onto one of our children
                    // (this is the lazy way, will break if there are nodes with the same name,
                    // but it's quicker than checking all nodes below is)
                    // 3) Check we're not dragging onto our parent
                    if (targetNode != dragNode && !targetNode.FullPath.StartsWith(dragNode.FullPath) && dragNode.Parent != targetNode)
                    {
                        // Copy the node, add as a child to the destination node
                        TreeNode newTreeNode = (TreeNode)dragNode.Clone();
                        targetNode.Nodes.Add(newTreeNode);
                        targetNode.Expand();

                        // Remove Original Node, set the dragged node as selected
                        dragNode.Remove();
                        this.SelectedNode = newTreeNode;

                        this.Cursor = Cursors.Default;

                        ea.SourceNodes.Add(dragNode);
                        ea.SourceUIMapFile = dragData.UIMapFile;
                        ea.TargetNode      = targetNode;
                    }
                }

                // Call drag complete event
                if ((this.DragComplete != null) && (ea.SourceNodes.Count != 0))
                {
                    this.DragComplete(this, ea);
                }
            }
        }
コード例 #2
0
        private void tvUIMap_DragComplete(object sender, CustomDragCompleteEventArgs e)
        {
            if ((e == null) || (e.TargetNode == null))
            {
                return;
            }

            string    destElementParentPath = (string)e.TargetNode.Tag;
            UIMapFile sourceUIMapFile       = e.SourceUIMapFile;

            if (sourceUIMapFile == null)
            {
                return;
            }

            try
            {
                // move selected nodes
                foreach (TreeNode sourceNode in e.SourceNodes)
                {
                    string srcElementPath = (string)sourceNode.Tag;
                    if (sourceUIMapFile == null)
                    {
                        // same UIMap
                        _uiMapFile.MoveUIObject(srcElementPath, destElementParentPath);
                    }
                    else
                    {
                        // from other UIMap
                        _uiMapFile.MoveUIObject(sourceUIMapFile, srcElementPath, destElementParentPath);
                    }
                }

                RedrawTree(destElementParentPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Program.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }