DragDrop is a quick way to operate Windows. As a developer based on Windows
Delphi also supports drag and drop operations, and the drag and drop function of the application system is very convenient, which truly reflects the
The power and convenience of Delphi.
All controls provided by Delphi (Control, which can obtain input focus) support drag and drop operations and have
The corresponding drag and drop properties, drag and drop events, and drag and drop methods. Next, we will introduce the drag-and-drop support of the control first, and then give the
General steps and application examples of sending drag and drop operations.
9.1 Drag and drop support for controls
In drag and drop operations, controls can be divided into two categories: source control and target control. Most controls can be used as source controls
Can also be used as a target control. But there are also some controls that can only support one of them.
9.1.1 Drag and drop properties
There are two main drag and drop properties:
●DragMode: Drag Mode
They are all set in the drag-and-drop source control. DragMode controls the user when pressing on the control during run time
How does the control react when the mouse is down. If DragMode is set to dmAutomatic, then when the user presses the mouse on the control
DragMode starts automatically; if DragMode is set to dmManual (this is the default), the mouse event will be processed
To determine whether a drag can start.
DragCursor is used to select the cursor displayed when dragging. The default value is CrDrag. Generally, do not modify it.
Interface specifications that are common in the programming process should be respected by developers. But sometimes for specific purposes,
Developers can also assign their own cursor to DragCursor.
9.1.2 Drag and drop event
There are three main drag and drop events:
●OnDragOver: Inspire when dragging by
●OnDragDrop: Inspire when dragging and dropping
●OnEndDrop: Inspire when dragging ends
The first two events are responded by the target control, and the latter one is responded by the source control.
The main function of the OnDragOver event is to determine whether the control is acceptable when the user drops the drag in place.
Its parameters include:
Source: TObject; {Source Control} X, Y: Integer; {Cursor Position}
State: TDragState; {Drag status} var Accept: Boolean {Can you accept}
TDragState is an enumeration type that represents the relationship between drag and drop items and target controls.
type TDragState = (dsDragEnter, dsDragLeave, dsDragMove);
The meaning of different values is as follows:
Table 9.1 The value and significance of DragState
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Value meaning
──────────────────────────────────────────
dsDragEnter Drag the object into a control that allows the dragged object to be dropped. is the default state.
dsDragLeave drags the object away from a control that allows the drag object to be dropped.
dsDragMove drag object moves within a control that allows the drag object to be dropped.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
The user can use the provided parameters to determine whether the dropped drag is acceptable, such as:
●Judge source control type:
Accept := Source is TLabel;
●Judge source control object:
Accept := (Source = TabSet1);
●Judge cursor position:
See routines in (9.2), (9.3).
●Judge drag status:
If (Source is TLabel) and (State = dsDragMove) then
begin source.DragIcon := ' New.Ico '; Accept := True;
end
else
Accept := False;
When Accept=True, the target control can respond to the OnDragDrop event, which is used to determine that the program after the drag is dropped
How to deal with it.
The parameters of the OnDragDrop event processing process include the source control and the cursor position. This information can be used for processing
Confirmation.
The OnEndDrag event is responded by the source control after the drag operation is completed, and is used by the source control to perform corresponding
deal with. The end of the drag operation includes both the drag and drop being accepted, and the user releases it on a control that cannot be dropped.
Put the mouse. The parameters of this event processing process include the target control (Target) and the coordinates of the drop position. if
Target=nil, means that the dragged item is not accepted by any control.
In the drag-and-drop moving, drag-and-drop copy operation introduced in Section 3, if the operation is successful, the file list box will be listed below.
The displayed content should be updated. The following program is used to implement this function.
PRocedure TFMForm.FileListEndDrag(Sender, Target: TObject; X, Y: Integer);
Begin
if Target <> nil then FileList.Update;
end;
In addition to the three events described above, there is another event OnMouseDown, which is also commonly used for drag-and-drop responses.
Although OnMouseDown is not a special drag-and-drop event, the beginning of dragging in manual mode is at this
Implemented during the process of event processing.
9.1.3 Drag and drop method
There are three ways to drag and drop:
●BeginDrag: Start a drag in manual mode
●EndDrag:
End a drag
●Dragging: determines whether a control is being dragged
All three methods are used by the source control.
When DragMode is set to dmManual, dragging must call the control's BeginDrag method to start.
BeginDrag has a Boolean parameter Immediate. If the input parameter is True, drag to start immediately,
Change to DragCursor's settings. If the input parameter is False, until the user moves the cursor by a certain distance
(5 pixels) before changing the cursor and starting to drag. This allows the control to accept an OnClick event without starting
Drag the operation.
The EndDrag method aborts the dragged state of an object. It has a Boolean parameter Drop. If Drop is set
To True, the dragged object is dropped in the current position (whether it can be accepted is determined by the target control); if Drop is set
If False, dragging is cancelled in place.
The following program shows that dragging is cancelled when dragging into a control panel.
procedure TForm1.Panel1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
Begin
Accept := False;
if (Source is TLabel) and (State = dsDragEnter) then
(Source as TLabel).EndDrag(False);
end;
The Draging method determines whether a control is being dragged. In the following example, when the user drags a different check box
When the window changes to a different color.
procedure TForm1.FormActivate(Sender: TObject);
Begin
CheckBox1.DragMode := dmAutomatic;
CheckBox2.DragMode := dmAutomatic;
CheckBox3.DragMode := dmAutomatic;
end;
procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
Begin
if CheckBox1.Dragging then
Color := clAqua;
if CheckBox2.Dragging then
Color := clYellow;
if CheckBox3.Dragging then
Color := clLime;
end;