在應用程序中,是通過處理一系列事件,如DragEnter,DragLeave和DragDrop事件來實現在Windows應用程序中的拖放操作的。通過使用這些事件參數中的可用信息,可以輕松實現拖放操作。
拖放操作在代碼中是通過三步實現的,首先是啟動拖放操作,在需要拖動數據的控件上實現MouseDown事件響應代碼,並調用DoDragDrop()方法;其次是實現拖放效果,在目標控件上添加DragEnter事件響應代碼,使用DragDropEffects枚舉類型實現移動或復制等拖動效果;最後是放置數據操作,在目標控件上添加DragDrop響應代碼,把數據添加到目標控件中。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace DragDrop
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.ListBox listBox2;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.listBox2 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(32, 24);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(120, 280);
this.listBox1.TabIndex = 0;
this.listBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseDown);
//
// listBox2
//
this.listBox2.ItemHeight = 12;
this.listBox2.Location = new System.Drawing.Point(248, 24);
this.listBox2.Name = "listBox2";
this.listBox2.Size = new System.Drawing.Size(120, 280);
this.listBox2.TabIndex = 0;
this.listBox2.DragDrop += new System.Windows.Forms.DragEventHandler(this.listBox2_DragDrop);
this.listBox2.DragEnter += new System.Windows.Forms.DragEventHandler(this.listBox2_DragEnter);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClIEntSize = new System.Drawing.Size(408, 333);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.listBox2);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.listBox1.AllowDrop = true;
this.listBox2.AllowDrop = true;
this.listBox1.Items.Add("a");
this.listBox1.Items.Add("b");
this.listBox1.Items.Add("c");
}
private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.listBox1.DoDragDrop(this.listBox1.Items[this.listBox1.SelectedIndex],DragDropEffects.Move);
}
private void listBox2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if(e.Data.GetDataPresent("Text"))
{
e.Effect = DragDropEffects.Move;
}
}
private void listBox2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
this.listBox2.Items.Add(e.Data.GetData("Text"));
this.listBox1.Items.Remove(e.Data.GetData("Text"));
}
}
}