副标题#e#
Windows中的拖放成果各人必然很熟悉了,如文件的转移,拷贝等操纵用鼠标轻轻一拖即可,在编写措施中有时也用到拖放,那么如何实现呢?现以C++ Builder5(简称CB5)为例,阐明拖放成果的详细实现。
一.东西条的拖放
—- 要实现拖放成果,首先必需相识几个与拖放有关的属性和要领, 对付TControl控件,CB5提供了三个属性,DockSite,DragKind和DragMode。机动运用这三个属性会获得意想不到的结果。这三个属性的意义是:
—- DockSite:指定当前控件是否接管Drag-and-Dock范例的操纵
—- DragKind:拖放种类,分为dkDrag和dkDock两种
—- DragMode:拖放模式,分为自动和手动模式两种
—- 个中Dock操纵是指某控件离开它的Parent,转而成为另一个控件的Child,也就是两个控件归并。若某一控件的DockSite为True,表白它接管执行Dock操纵的某控件,并成为它的Parent。
—- 著名的Office东西条可以随意拖放,其实实现起来很简朴:在Form上放一CoolBar控件,再在CoolBar控件上随意放几个ToolBar控件,它们的属性配置代码如下:
CoolBar1.DockSite=true;
ToolBar1.DragKind=dkDock;
ToolBar1.DragMode= dmAutomatic;
—- 其它ToolBar的属性配置与ToolBar1的属性配置沟通,编译运行措施,拖动东西条试试,Cool极了吧。
二、任何两上控件间的拖放
—- 与此操纵有关的几个函数有:
—- BeginDrag:开始执行拖放操纵,假如控件的DragMode为dmManual,则必需挪用此函数,假如DragMode为dmAutomatic,则不消挪用。
—- OnDragOver:当被拖放的工具颠末此控件时触发此事件,个中的参数Accept暗示是否接管拖放的工具。
#p#副标题#e#
—- OnDragDrop:当放下被拖放的工具时触发此事件。
—- 下面举例说明拖放的实现进程:
—- 在CB5中新建一工程,在Form1上放两个ListBox,别离定名为ListBox1,ListBox2,打开ListBox1的Items属性框,随便输入几行字符串。
—- 其属性配置如下:
ListBox1->MultiSelect=true; // MultiSelect属性设为true,暗示可以多选
ListBox1->DragMode= dmAutomatic;
ListBox2->MultiSelect=true;
ListBox2->DragMode= dmAutomatic; //两个ListBox拖放事件沟通,可以相互拖放
ListBox2->OnDragOver= ListBox1DragOver;
ListBox2->OnDragDrop= ListBox1DragDrop;
ListBox2->OnStartDrag= ListBox1StartDrag;
在头文件中配置两个int型变量CurIndex,NewIndex
措施代码如下:
//-----------------------------------------------------------
#include < vcl.h >
#pragma hdrstop
#include "unit1.h"
#include "FileCtrl.hpp"
//-----------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//----------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//———————————————————-
void __fastcall TForm1::ListBox1StartDrag(TObject *Sender, TDragObject *&DragObject)
{
//开始执行拖放事件时记录ListBox->ItemIndex;
CurIndex=((TListBox *)Sender)->ItemIndex;
}
//———————————————————-
void __fastcall TForm1::ListBox1DragDrop(TObject *Sender, TObject *Source, int X, int Y)
{
int index;
if(Sender==Source) //假如Sender便是Source,表白在同一控件内执行操纵,本例用来互换ListBox中的任意两个Items
{
NewIndex=Y/(ListBox1->ItemHeight);//获得拖放后的ItemIndex
//假如ItemIndex大于ListBox中的Item数,暗示拖到最后一个
NewIndex=NewIndex< ((TListBox *)Sender)- >Items->Count?
NewIndex:((TListBox *)Sender)->Items->Count-1;
//执行Move操纵,移动Item
((TListBox *)Sender)->Items->Move(CurIndex,NewIndex);
}
//假如Sender不便是Source,表白在两个控件间执行操纵
//此例是将数据从一ListBox拖到另一个ListBox
else
{ //若只选中一项
if(((TListBox *)Source)->SelCount==1)
{
((TListBox *)Sender)->Items->Add(((TListBox *)Source)-> Items->Strings[((TListBox *)Source)->ItemIndex]);
((TListBox *)Source)->Items->Delete(((TListBox *)Source)-> ItemIndex);
}
//多选操纵
if(((TListBox *)Source)->SelCount>=1)
{
//轮回操纵,测试哪些项被选中
for(index=0;index< ((TListBox *)Source)- >Items->Count; index++)
if(((TListBox *)Source)->Selected[index])
((TListBox *)Sender)->Items->Add(((TListBox *)Source)-> Items->Strings[index]);
//从后向前删除Source控件中数据
for(index=((TListBox *)Source)->Items->Count-1;index>=0;index--)
if(((TListBox *)Source)->Selected[index])
((TListBox *)Source)->Items->Delete(index);
}
}
}
//———————————————————-
#p#分页标题#e#
void __fastcall TForm1::ListBox1DragOver(TObject *Sender, TObject *Source,int X, int Y, TDragState State, bool &Accept)
{
//本例中假如原控件各方针控件都为ListBox控件,则接管拖放
Accept = Source->ClassNameIs("TListBox")&& Sender->ClassNameIs("TListBox");
}
运行措施,用鼠标拖拖看。
//———————————————————-
本措施在PWin98+BCB5下编译运行通过