Csharp实例编程百例.rar
源代码在线查看: form1.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace DragDropApp
{
///
/// Form1 的摘要说明。
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.ImageList imageList1;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.treeView1 = new System.Windows.Forms.TreeView();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.SuspendLayout();
//
// treeView1
//
this.treeView1.AllowDrop = true;
this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.treeView1.ImageList = this.imageList1;
this.treeView1.Name = "treeView1";
this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("节点0", new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("节点1", new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("节点4"),
new System.Windows.Forms.TreeNode("节点5")}),
new System.Windows.Forms.TreeNode("节点2", new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("节点6"),
new System.Windows.Forms.TreeNode("节点7")}),
new System.Windows.Forms.TreeNode("节点3", new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("节点8"),
new System.Windows.Forms.TreeNode("节点9")}),
new System.Windows.Forms.TreeNode("节点10")})});
this.treeView1.SelectedImageIndex = 1;
this.treeView1.Size = new System.Drawing.Size(292, 273);
this.treeView1.TabIndex = 0;
this.treeView1.DragOver += new System.Windows.Forms.DragEventHandler(this.treeView1_DragOver);
this.treeView1.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeView1_DragEnter);
this.treeView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView1_ItemDrag);
this.treeView1.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeView1_DragDrop);
//
// imageList1
//
this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.treeView1});
this.Name = "Form1";
this.Text = "DragDropApp";
this.ResumeLayout(false);
}
#endregion
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void treeView1_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
{
//启动拖放操作,设置拖放类型为Move(移动)
DoDragDrop(e.Item ,DragDropEffects.Move );
}
private TreeNode oldenterNode; //保存前一个鼠标进入的TreeNode
private void treeView1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
//获取TreeNode类型的数据内容
object data = e.Data.GetData (typeof(TreeNode));
//如果有数据,拖放目标允许移动
if (data!=null)
{
e.Effect = DragDropEffects.Move ;
}else e.Effect = DragDropEffects.None;
//获得当前TreeView的引用
TreeView theTree = (TreeView)sender;
//得到鼠标进入的TreeNode的引用
TreeNode enterNode = theTree.GetNodeAt (treeView1.PointToClient (new Point(e.X ,e.Y)));
if (enterNode!=null)
{ //改变进入TreeNode的背景色
enterNode.BackColor = Color.Blue ;
//保存此TreeNode,进入下一个时还原背景色
oldenterNode = enterNode;
}
}
private void treeView1_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{ //修改鼠标进入TreeNode的背景色,还原上一个TreeNode的背景色
TreeView theTree = (TreeView)sender;
TreeNode enterNode = theTree.GetNodeAt (treeView1.PointToClient (new Point(e.X ,e.Y)));
if ((enterNode != null)&&(enterNode!=oldenterNode)){
oldenterNode.BackColor = Color.White ;
enterNode.BackColor = Color.Blue ;
oldenterNode = enterNode;
}
}
private void treeView1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{ //得到拖放数据,并转换为TreeNode型
TreeNode theNode = (TreeNode)e.Data .GetData (typeof(TreeNode));
TreeView theTree = (TreeView)sender;
//得到当前鼠标进入的TreeNode,即拖放目标TreeNode
TreeNode targetNode = theTree.GetNodeAt (treeView1.PointToClient (new Point(e.X ,e.Y)));
if (targetNode!= null)
{
TreeNode targetParent = targetNode.Parent ;
//删除拖放的TreeNode
theNode.Remove ();
//添加到目标TreeNode下
targetNode.Nodes .Add (theNode);
targetNode.BackColor = Color.White ;
theTree.SelectedNode = targetNode;
}
}
}
}