在應(yīng)用程序中,是通過(guò)處理一系列事件,如dragenter,dragleave和dragdrop事件來(lái)實(shí)現(xiàn)在windows應(yīng)用程序中的拖放操作的。通過(guò)使用這些事件參數(shù)中的可用信息,可以輕松實(shí)現(xiàn)拖放操作。
拖放操作在代碼中是通過(guò)三步實(shí)現(xiàn)的,首先是啟動(dòng)拖放操作,在需要拖動(dòng)數(shù)據(jù)的控件上實(shí)現(xiàn)mousedown事件響應(yīng)代碼,并調(diào)用dodragdrop()方法;其次是實(shí)現(xiàn)拖放效果,在目標(biāo)控件上添加dragenter事件響應(yīng)代碼,使用dragdropeffects枚舉類型實(shí)現(xiàn)移動(dòng)或復(fù)制等拖動(dòng)效果;最后是放置數(shù)據(jù)操作,在目標(biāo)控件上添加dragdrop響應(yīng)代碼,把數(shù)據(jù)添加到目標(biāo)控件中。
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
namespace dragdrop
{
/// <summary>
/// form1 的摘要說(shuō)明。
/// </summary>
public class form1 : system.windows.forms.form
{
private system.windows.forms.listbox listbox1;
private system.windows.forms.listbox listbox2;
/// <summary>
/// 必需的設(shè)計(jì)器變量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
//
// windows 窗體設(shè)計(jì)器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </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>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </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"));
}
}
}
新聞熱點(diǎn)
疑難解答
圖片精選