Главная страница | назад





Article #17566: Drag and Drop using a TDragObject

 Question and Answer Database
FAQ2566C.txt Drag and Drop using a TDragObject
Category :VCL
Platform :All
Product :C++Builder 3.x
Question:
How do I use a TDragObject with the DragDrop functionality built
into CBuilder?
Answer:
Here is an example for TDragObject. This will drag & drop from
the ListBox1 to the Edit1.
Create a form : Form1
Add a listbox : ListBox1
and a EditBox : Edit1.
and a TImageList: ImageList1.
Set the dragMode to dmAutomitic for the ListBox, Edit and Form.
Insert a few lines in the ListBox1.
Set the Imagelist Width and Height to 32x32.
Load one bitmap in it.
In the .h file (Unit1.h) of the form add the following :
//---------------------------------------------------------------------------
class TMyDragObject : public TDragObject
{
TCustomImageList* __fastcall GetDragImages(void);
public:
String Valeur;
__fastcall TMyDragObject(void);
};
//---------------------------------------------------------------------------
In the cpp file add the following code :
//---------------------------------------------------------------------------
__fastcall TMyDragObject::TMyDragObject(void)
{
TDragObject();
}
//---------------------------------------------------------------------------
TCustomImageList* __fastcall TMyDragObject::GetDragImages(void)
{
return(Form1->ImageList1);
}
In the FormCreate event add the following code :
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
ControlStyle << csDisplayDragImage;
Edit1->ControlStyle << csDisplayDragImage;
ListBox1->ControlStyle << csDisplayDragImage;
}
//---------------------------------------------------------------------------
The ListBox StartDrag should be:
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1StartDrag(TObject *Sender,
TDragObject *&DragObject)
{
TListBox *tlb;
TMyDragObject *tm;
tlb = dynamic_cast(Sender);
if (tlb != NULL) {
if (tlb->ItemIndex != -1) {
tm = new TMyDragObject();
tm->Valeur = tlb->Items->Strings[tlb->ItemIndex];
DragObject = tm;
}
}
}
//---------------------------------------------------------------------------
The Edit Drag Over should be:
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1DragOver(TObject *Sender, TObject *Source,
int X, int Y, TDragState State, bool &Accept)
{
Accept = true;
}
//---------------------------------------------------------------------------
The Edit1DragDrop event will be:
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1DragDrop(TObject *Sender, TObject
*Source, int X,
int Y)
{
TMyDragObject *tmydrag;
tmydrag = dynamic_cast(Source);
if (tmydrag) {
Edit1->Text = tmydrag->Valeur;
}
}
if all is well you will see that Builder
merges your bitmap with its own drag & drop
cursors.
7/2/98 10:32:32 AM

Last Modified: 01-SEP-99