一个UltraTree结点拖动到另一个UltraTree上面

来源:互联网 发布:网络清流是什么意思 编辑:程序博客网 时间:2024/04/30 08:13
using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using Infragistics.Win;using Infragistics.Win.UltraWinTree;using System.Diagnostics;namespace UltraTree_DragDrop_CS{/// <summary>/// Summary description for Form1./// </summary>public class Form1 : System.Windows.Forms.Form{private Infragistics.Win.UltraWinTree.UltraTree sourceTree;private Infragistics.Win.UltraWinTree.UltraTree destinationTree;private System.Windows.Forms.Label label1;private System.Windows.Forms.Label label2;private Point lastMouseDownPoint = Point.Empty;/// <summary>/// The KeyState property of the DragEventArgs is annoying to deal/// with since its type is not a .NET enumeration, so define the/// constant that we are interested in here./// </summary>private const int KEYSTATE_CONTROL = 8;/// <summary>/// Required designer variable./// </summary>private System.ComponentModel.Container components = null;public Form1(){//// Required for Windows Form Designer support//InitializeComponent();//// TODO: Add any constructor code after InitializeComponent call//}/// <summary>/// Clean up any resources being used./// </summary>protected override void Dispose( bool disposing ){if( disposing ){if (components != null) {components.Dispose();}}base.Dispose( disposing );}#region Windows Form Designer generated code/// <summary>/// Required method for Designer support - do not modify/// the contents of this method with the code editor./// </summary>private void InitializeComponent(){this.sourceTree = new Infragistics.Win.UltraWinTree.UltraTree();this.destinationTree = new Infragistics.Win.UltraWinTree.UltraTree();this.label1 = new System.Windows.Forms.Label();this.label2 = new System.Windows.Forms.Label();((System.ComponentModel.ISupportInitialize)(this.sourceTree)).BeginInit();((System.ComponentModel.ISupportInitialize)(this.destinationTree)).BeginInit();this.SuspendLayout();// // sourceTree// this.sourceTree.Location = new System.Drawing.Point(8, 24);this.sourceTree.Name = "sourceTree";this.sourceTree.Size = new System.Drawing.Size(192, 232);this.sourceTree.TabIndex = 0;this.sourceTree.MouseDown += new System.Windows.Forms.MouseEventHandler(this.sourceTree_MouseDown);this.sourceTree.MouseMove += new System.Windows.Forms.MouseEventHandler(this.sourceTree_MouseMove);// // destinationTree// this.destinationTree.Location = new System.Drawing.Point(224, 24);this.destinationTree.Name = "destinationTree";this.destinationTree.Size = new System.Drawing.Size(200, 232);this.destinationTree.TabIndex = 1;this.destinationTree.DragOver += new System.Windows.Forms.DragEventHandler(this.destinationTree_DragOver);this.destinationTree.DragDrop += new System.Windows.Forms.DragEventHandler(this.destinationTree_DragDrop);// // label1// this.label1.AutoSize = true;this.label1.Location = new System.Drawing.Point(12, 8);this.label1.Name = "label1";this.label1.Size = new System.Drawing.Size(40, 13);this.label1.TabIndex = 2;this.label1.Text = "Source";// // label2// this.label2.AutoSize = true;this.label2.Location = new System.Drawing.Point(228, 8);this.label2.Name = "label2";this.label2.Size = new System.Drawing.Size(61, 13);this.label2.TabIndex = 3;this.label2.Text = "Destination";// // Form1// this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);this.ClientSize = new System.Drawing.Size(432, 266);this.Controls.AddRange(new System.Windows.Forms.Control[] {  this.label2,  this.label1,  this.destinationTree,  this.sourceTree});this.Name = "Form1";this.Text = "UltraTree_DragDrop";this.Load += new System.EventHandler(this.Form1_Load);((System.ComponentModel.ISupportInitialize)(this.sourceTree)).EndInit();((System.ComponentModel.ISupportInitialize)(this.destinationTree)).EndInit();this.ResumeLayout(false);}#endregion/// <summary>/// The main entry point for the application./// </summary>[STAThread]static void Main() {Application.Run(new Form1());}private void Form1_Load(object sender, System.EventArgs e){//Populate the UltraTree controlsfor ( int i = 1; i <= 10; i ++ ){UltraTreeNode node1 = this.sourceTree.Nodes.Add( null, "Node " + i.ToString() );UltraTreeNode node2 = this.destinationTree.Nodes.Add( null, "Node " + i.ToString() );for ( int j = 1; j <= 10; j ++ ){node1.Nodes.Add( null, node1.Key + "Child " + j.ToString() );node2.Nodes.Add( null, node2.Key + "Child " + j.ToString() );}}//Set the AllowDrop property of the destination tree//to true so it accepts drops.this.destinationTree.AllowDrop = true;//Set the HideSelection property of the destination tree//to false so that its nodes' selected state is depicted//even when the control does not have focus.this.destinationTree.HideSelection = false;}/// <summary>/// Handles the UltraTree's MouseDown event./// </summary>private void sourceTree_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e){//Cache the point at which the mouse was pressed, so we can//compare it to the drag threshold in MouseMovethis.lastMouseDownPoint = new Point( e.X, e.Y );}/// <summary>/// Handles the UltraTree's MouseMove event./// </summary>private void sourceTree_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e){UltraTree sourceTree = sender as UltraTree;//We are only interested in this event when the left//mouse button is pressedif ( e.Button == MouseButtons.Left ){//Get the cursor position (as relative to the control's coordinate//system) and determine whether it is within the drag thresholdPoint cursorPos = new Point( e.X, e.Y );Rectangle dragRect = new Rectangle( this.lastMouseDownPoint, SystemInformation.DragSize );dragRect.X -= SystemInformation.DragSize.Width / 2;dragRect.Y -= SystemInformation.DragSize.Height / 2;//If it is within the drag threshold, initiate the drag/drop operation,//specifying the ActiveNode as the "drag node"if ( dragRect.Contains(cursorPos) )sourceTree.DoDragDrop( sourceTree.ActiveNode, DragDropEffects.All );}}/// <summary>/// Handles the UltraTree's DragOver event/// </summary>private void destinationTree_DragOver(object sender, System.Windows.Forms.DragEventArgs e){UltraTree destinationTree = sender as UltraTree;//Get the cursor position (as relative to the control's coordinate//system) and determine whether there is a node under it.Point cursorPos = destinationTree.PointToClient( new Point(e.X, e.Y) );UltraTreeNode nodeAtPoint = destinationTree.GetNodeFromPoint( cursorPos );//If there is no node under the cursor, show the "no drop" cursorif ( nodeAtPoint == null )e.Effect = DragDropEffects.None;else{//If there is a node under the cursor, show either the 'Move'//or 'Copy' cursor, based on whether the Control key is pressede.Effect = (e.KeyState & KEYSTATE_CONTROL) == KEYSTATE_CONTROL ? DragDropEffects.Copy : DragDropEffects.Move;//Select the node under the cursor, to provide a visual//cue as to what node will receive the drop operation,//and refresh the display so the selection is shown immediately.nodeAtPoint.Selected = true;destinationTree.Refresh();}}/// <summary>/// Handles the UltraTree's DragDrop event/// </summary>private void destinationTree_DragDrop(object sender, System.Windows.Forms.DragEventArgs e){UltraTree destinationTree = sender as UltraTree;//Get the cursor position (as relative to the control's coordinate//system) and determine whether there is a node under it.Point cursorPos = destinationTree.PointToClient( new Point(e.X, e.Y) );UltraTreeNode destinationNode = destinationTree.GetNodeFromPoint( cursorPos );if ( destinationNode != null ){//Retrieve the drag data from the IDataObject and cast it to an UltraTreeNodeUltraTreeNode dragNode = e.Data.GetData( typeof(UltraTreeNode) ) as UltraTreeNode;if ( dragNode != null ){//This is not necessary to complete the drag, but most of//the time the end user will want to begin working with//the tree that received the drop.destinationTree.Select();destinationTree.ActiveNode = destinationNode;//If the Control key is pressed, clone the drag node (and any//descendant nodes) and add them to the destination node's//Nodes collection. If the Control key is not pressed, use//the Reposition method, which removes the nodes from the source//collection and adds them to the destination Nodes collection.if ( (e.KeyState & KEYSTATE_CONTROL) == KEYSTATE_CONTROL ){UltraTreeNode dragNodeClone = dragNode.Clone() as UltraTreeNode;destinationNode.Nodes.Add( dragNodeClone );}elsedragNode.Reposition( destinationNode.Nodes );}}}}}


0 0