Drag and drop operation in Delphi

xiaoxiao2021-03-06  74

Dragdrop is a quick way of operation provided by Windows. As a Windows-based development tool, Delphi also supports drag and drop operations, and the development of the application system is very convenient, truly reflects the powerful functions and convenience of Delphi.

All controls provided by Delphi (Control, all those who can get input focus) support drag and drop operations, and have corresponding drag and drop properties, drag and drop events, and drag and drop methods. Let's first introduce the drag and drop support of the control, and then give the general steps and application instances of the development drag and drop operation.

9.1 Control of the drag and drop support

The control in the drag and drop operation can be divided into two types of source controls and target controls. Most of the controls can be used as a source control as a target control. But there is also a part of the control only one of them.

9.1.1 Drag and drop properties

There are two main drag and drop attributes:

● DragMode: Drag mode

They are all set in the source control of drag and drop. DragMode Controls how the user reacts when the control is pressed on the runtime. If DragMode is set to DMAUTOMATIC, then the user drags automatically when the user presses the mouse; if DragMode is set to DMMANUAL (this is default), it will determine whether an drag can begin by handling the mouse event.

Dragcursor is used to select the cursor that is displayed when dragging, the default value is CRDRAG, and it is generally not to modify it. The general interface specification in the program design process should be respected by developers. But sometimes for specific purposes, developers can also assign their design to Dragcursor.

9.1.2 Drag and drop event

There are three main plus and drop events:

● OnDragover: Drag is stimulated

● OnDragDrop: Drag and put down

● OneendDrop: Excited at the end of the drag

The first two events respond by the target control, and the latter event responds by the source control.

The main function of the OnDragover event is to determine if the control can be accepted when the user is discharged. Its parameters include:

Source: TOBJECT; {Source Control} x, y: integer; {cursor position} State: tdragState; {Drag status} var accept: boolean {can accept}

TDRagState is an enumeration type that represents the relationship between drag and drop projects and target controls.

Type tdragState = (DSDragenter, DSDRAGLEAVE, DSDRAGMOVE);

The significance of different values ​​is as follows:

Table 9.1 DragState's value and meaning

━━━━━━━━━━━━━━━━━━━━━━━━━━━

Value meaning

─────────────────────

DSDRAGENTER Drag Objects Enter a control that allows drag objects to be placed. For the default state.

DSDRAGLEAVE Drag Objects Leave a control that allows dragging objects.

DSDRAGMOVE Drag objects move within a control that allows drag objects.

━━━━━━━━━━━━━━━━━━━━━━━━━━━

Users can use the provided parameters to determine if the dropped drag can be accepted, such as:

● Judgment Source Control Type:

Accept: = source is TLABEL;

● Judgment source control object:

Accept: = (Source = Tabset1);

● Judgment the cursor position:

See (9.2), (9.3) in the routine.

● Judgment 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 to determine how the drag is put down the postup how the program is processed. The parameters of the OnDragDrop event handling process include source controls and cursor locations. This information can be used for the determination of how to process.

OneundDrag event is to respond to the source control after the drag operation is over, and the source control is used for the corresponding processing. The drag operation end includes both drag and put down, including the user releases the mouse on a control that cannot be accepted. The parameters of the event process include the target control (TARGET) and the coordinates of the lower position. If target = nil, it means that the drag item is not accepted by any controls.

Drag and drop the files described in Section 3, drag and drop the copy operation, if the operation is successful, then the file list box should update the display. The following program is used to achieve this.

Procedure Tfmform.FileListendDrag (Sender, Target: TOBJECT; X, Y: Integer);

Begin

IF target <> nil dam.Update;

END;

In addition to the three incidents described above, there is an event OnMouseDown, which is often used to drag and drop the response.

Although INMOUSEDOWN is not a special drag and drop event, the beginning of dragging in manual mode is implemented during the process of this event.

9.1.3 Drag and drop method

There are three ways to drag and drop:

● Begindrag: starting a drag in manual mode

● Enddrag:

End a drag

● Dragging: Determines if a control is being dragged

These three methods are used by source controls.

When DragMode is set to DMMANUAL, dragging the Begindrag method that must call the control can begin.

Begindrag has a Boolean parameter immediate. If the input parameter is True, dragging immediately, the cursor changes to DragCursor settings. If the input parameter is false until the user moves the cursor to change the cursor and start dragging. This allows the control to accept an OnClick event and do not start dragging.

The EndDrag method has stopped the drag status of an object. It has a Boolean parameter DROP. If the DROP is set to true, the object being dragged is put down in the current location (whether it is accepted by the target control); if the DROP is set to false, dragging is canceled in the ground.

The following sequence indicates that the drag is canceled 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;

Draging method determines whether a control is being dragged. In the following example, the window changes to different colors when the user drags different checkboxes.

Procedure TFORM1.MMACACTIVATE (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;

Delphi Drop Programming (2) 2000-08-03 · - · 仙 时间

转载请注明原文地址:https://www.9cbs.com/read-92967.html

New Post(0)