在应用程序中,是通过处理一系列事件,如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"));
}
}
}
新闻热点
疑难解答
图片精选