-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicalDragAndDrop.cpp
More file actions
200 lines (174 loc) · 6.71 KB
/
DynamicalDragAndDrop.cpp
File metadata and controls
200 lines (174 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Unit1.h
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // Von der IDE verwaltete Komponenten
TListBox *ListBox1;
TEdit *Edit1;
void __fastcall ListBox1StartDrag(TObject *Sender, TDragObject *&DragObject);
void __fastcall Edit1StartDrag(TObject *Sender, TDragObject *&DragObject);
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormDestroy(TObject *Sender);
void __fastcall FormDragOver(TObject *Sender, TObject *Source, int X, int Y, TDragState State,
bool &Accept);
void __fastcall FormDragDrop(TObject *Sender, TObject *Source, int X, int Y);
private: // Benutzer-Deklarationen
void __fastcall AddItemToListBox(TControl *SourceControl);
public: // Benutzer-Deklarationen
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
// Unit1.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1StartDrag(TObject *Sender, TDragObject *&DragObject)
{
ListBox1->BeginDrag(false);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1StartDrag(TObject *Sender, TDragObject *&DragObject)
{
Edit1->BeginDrag(false);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
ListBox1->DragMode = dmAutomatic;
Edit1->DragMode = dmAutomatic;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
ListBox1->Items->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDragOver(TObject *Sender, TObject *Source, int X, int Y, TDragState State,
bool &Accept)
{
Accept = Source == ListBox1 || Source == Edit1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDragDrop(TObject *Sender, TObject *Source, int X, int Y)
{
if (Source == ListBox1)
AddItemToListBox(ListBox1);
else if (Source == Edit1)
AddItemToListBox(Edit1);
}
void __fastcall TForm1::AddItemToListBox(TControl *SourceControl)
{
UnicodeString ItemText = "";
if (SourceControl == ListBox1)
ItemText = ListBox1->Items->Strings[ListBox1->ItemIndex];
else if (SourceControl == Edit1)
ItemText = Edit1->Text;
ListBox1->Items->Add(ItemText);
}
//---------------------------------------------------------------------------
//###########################################################################
/*In diesem Beispiel gibt es ein Hauptformular (Form1), das eine ListBox (ListBox1)
und ein Edit-Feld (Edit1) enthält. Beide Komponenten wurden so konfiguriert,
dass sie automatisch Drag-and-Drop unterstützen (DragMode = dmAutomatic).
Außerdem gibt es Ereignisse für das Starten des Ziehvorgangs für ListBox1 und Edit1.
Wenn ein Drag-and-Drop-Vorgang von einer dieser Komponenten gestartet wird, fügt das
Programm den Text des ausgewählten Elements der ListBox oder den Text des Edit-Felds
zur ListBox hinzu.
Es gibt auch Ereignisse für das Formular selbst, um das Ziehen und Ablegen zu verwalten
(FormDragOver und FormDragDrop). Das Formular akzeptiert nur Ziehvorgänge von der
ListBox oder dem Edit-Feld.*/
//###########################################################################
// Unit1.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1StartDrag(TObject *Sender, TDragObject *&DragObject)
{
if (!Button1->Enabled) return; // Wenn die Schaltfläche deaktiviert ist, wird das Drag-and-Drop nicht gestartet
ListBox1->BeginDrag(false);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1StartDrag(TObject *Sender, TDragObject *&DragObject)
{
if (!Button1->Enabled) return; // Wenn die Schaltfläche deaktiviert ist, wird das Drag-and-Drop nicht gestartet
Edit1->BeginDrag(false);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
// Die Schaltfläche wurde geklickt, um das Drag-and-Drop zu sperren oder zu entsperren
ListBox1->AllowDrag = !ListBox1->AllowDrag;
Edit1->AllowDrag = !Edit1->AllowDrag;
if (ListBox1->AllowDrag)
Button1->Caption = "Drag & Drop sperren";
else
Button1->Caption = "Drag & Drop entsperren";
}
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
// Erstellen Sie ein neues TLabel
TLabel *label = new TLabel(this);
label->Parent = this;
label->Caption = "Drag me!";
label->Left = 50;
label->Top = 50;
// Einstellen der Drag-Eigenschaften
label->DragMode = dmAutomatic;
label->DragKind = dkDrag;
// Event-Handler für das DragOver-Ereignis
label->OnDragOver = LabelDragOver;
}
void __fastcall TForm1::LabelDragOver(TObject *Sender, TObject *Source, int X, int Y, TDragState State, bool &Accept)
{
Accept = true;
}
void __fastcall TForm1::FormDragDrop(TObject *Sender, TObject *Source, int X, int Y)
{
TLabel *label = dynamic_cast<TLabel *>(Source);
if (label)
{
label->Left = X;
label->Top = Y;
}
}