9.1.3 drag-and-drop method
There are three drag-and-drop methods:
BeginDrag: Start a drag in manual way
EndDrag: End a drag
Dragging: Determine if a control is being dragged
These three methods are used by the source control.
When DragMode is set to dmmanual, drag must call the control's BeginDrag method to start. BeginDrag has a Boolean parameter immediate. If the input parameter is true, drag starts immediately, and the cursor changes to the Dragcursor setting. If the input parameter is false until the user moves the cursor to a certain distance (5 pixel points) before changing the cursor, start dragging. This allows the control to accept an OnClick event without starting the drag operation.
The EndDrag method aborts the dragged state of an object. It has a Boolean parameter drop. If the drop is set to True, the dragged object is dropped at the current position (whether it is accepted by the target control), and if the drop is set to False, the drag is canceled in place.
The following procedure indicates that dragging is canceled when dragged 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, the window changes to a different color when the user drags a different check box.
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
9.2 General steps for developing drag-and-drop functionality
Drag-and-drop as a convenient operation of Windows provides a function of the object, in Delphi can be easily developed. Depending on the process of the drag-and-drop operation, the development steps can be divided into four phases, namely:
Start the drag operation
Receive drag items
Drop Drag Item
Terminate drag operation
During the introduction we will combine a drag-and-drop operation instance of a Tabset (tab set). The interface design is as shown. At run time, when a user drags a label to the position of another label, the label moves to that location and causes the label set to be reset.
9.2.1 Start the drag operation
When drag mode (DragMode) is set to Dmautomatic, the user drags automatically when the mouse is pressed on the source control and determines whether the drag starts when set to dmmanual by handling mouse events. If you want to start dragging call the BeginDrag method.
In Tabset drag-and-drop, we use the following MouseDown event handler to start a tag drag. First, the key is to determine whether the left button, and then to determine whether the project is legal.
Procedure Tform1.tabset1mousedown (Sender:tobject; Button:tmousebutton;
Shift:tshiftstate; X, Y:integer);
Var
Dragitem:integer;
Begin
If Button = Mbleft Then
Begin
Dragitem: = Tabset1.itematpos (Point (X, Y));
if (Dragitem >-1) and (Dragitem < TabSet1.Tabs.Count) Then
Tabset1.begindrag (False);
End
End