Drag and drop operation in Delphi
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 post-procedure how to process.
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 thencolor: = Claqua;
if Checkbox2.dragging Then
Color: = CLYELLOW;
if Checkbox3.dragging Then
Color: = CLLIME;
END;
Delphi Drop Programming (2) 2000-08-03 · - · 仙 时间
Drag Development in Delphi (2)
9.2 General steps in developing drag and drop
Drag and drop as a convenient operation object with Windows, which can be easily developed in Delphi. The development step can be divided into four stages according to the process of drag and drop operations, namely:
● Start drag operation
● Receive drag items
● Let go down the drag item
● Terminate drag operation
In the process of introduction, we will combine a TabSet (label set) drag and drop operation instance. Interface design is shown. When the user drags a label to another label when the user drags a label, the label will move to the location and cause a reset of the label set.
9.2.1 Start dragging operation
When the DragMode is set to DMAUTOMATIC, the user drags automatically begins when the mouse is pressed; when set to DMMANUAL, decide whether the drag is started by handling the mouse event. If you want to start dragging to call the Begindrag method.
In TabSet drag and drop, we start a tag drag with the following MouseDown event handle.
First, it is judged whether or not the prescription is left button, and then determines whether the item 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.begindrag (false); END; END; 9.2.2 Receiving Drag Project Whether a control can receive the drag item is determined by the onDragover event of the control. In TabSet drag, it is mainly to determine the location of the mouse. Procedure TFORM1.TABSET1DRAGOVER (Sender, Source: TOBJECT; X, Y: Integer; State: TDRAGSTATE; VAR Accept: Boolean; VAR DropPOS: Integer; Begin IF source = tabset1 Then Begin Droppos: = TabSet1.ItemtPOS (Point (x, y); Accept: = (Droppos> -1) and (droppos <> tabset1.tabindex) and (Droppos END; Else Accept: = false; END; 9.2.3 Locating the drag item When the ACCEPT returned by the OnDragover event process is true and the item is put down, the onDragDrop event handles the response after the drag is dropped. In the TabSet drag and drop example, it is the location of the tag. Procedure TFORM1.TABSET1DRAGDROP (Sender, Source: TOBJECT; X, Y: Integer); VAR Oldpos: integer; Newpos: integer; Begin IF source = tabset1 thenbegin Oldpos: = tabset1.tabindex; Newpos: = Tabset1.itematpos (Point (x, y); IF (NewPOS> -1) and (newpos <> oldpos) THEN Tabset1.tabs.move (Oldpos, NewPOS); END; END; 9.2.4 End Drain Operation There are two ways to end the drag operation: or the user releases the mouse button or the program is forcibly discontinued with the EndDrag method. There are two consequences of ending the drag operation: put down the acceptance or put down is ignored. The source control is received by the Drag Operation End The source control is received and dragged end event OneendDrag. 9.3 Drag and drop Application example: Drag and drop support for file manager In Chapter 6, the final development of the file manager applied instance, although it has been implemented, but it is very short of operation compared to Windows file manager. The biggest defect is that it does not support the drag and drop movement of the file and drag and drop. At this chapter, we can make up for this defect. The file drag and drop means that when the user drags a file to a certain directory under the directory tree, the file will automatically move to the directory; the file drag and drop copy refers to the user to drag a file to When a drive tag is put down, the file will be automatically copied to the current directory of the drive. As a list of files of the source control, the drive tag can be located in different sub-windows as the directory tree of the source control. The current directory of the driver is the latest operation result of any sub-window, regardless of this sub-window and drag source, whether it is related to the target. In order to achieve the above functions, there are two problems that must first resolve: 1. How do I record the current directory of each drive? To this end we have defined a global variable: Var CurentDirlist: array [0 ... 25] of string [70]; In the onchange event of Directoryoutline: Procedure Tfmform.directoryoutLineChange (Sender: TOBJECT); Begin CreateCaption; FileList.clear; Filelist.directory: = DIRECTORYOUTLINE.DIRECTORY; Filelist.Update; CurrentDirlist [DriveTabSet.Tabindex]: = DIRECTORYOUTLINE.DIRECTORY; FileManager.directoryPanel.caption: = DirectoryoutLine.directory; END; Since DriveTabSet responds to the OnClick event before responding to the OnDragDrop event, and inspired DirectoryoutLine's onchange event before responding to the event, it can guarantee that the CurrentDirlist number item used in the onDragdrop event at any time is not an empty string. 2. How to ensure the irrelevantness of moving, copying and sub-windows? One key issue here is that we use the IS operator when it is determined by the source control: IF source is tfilelist the ... If we use the following statement: IF source = filelist kil ... Then move, the copy operation will be limited to this sub-window range. When solving the above-mentioned work, just follow the general development steps of drag and drop, press steps to complete. 1.FileList starts dragging Procedure Tfmform.FileListMousedown (Sender: Tobject; Button: tmousebutton; Shift: TshiftState; x, y: integer; Begin if Button = Mbleft then With sender as tfilelistbox do Begin ITEMATPOS (Point (x, y), true)> = 0 THEN Begindrag (False); END; END; ItemAtPos is used to check if there is a file. The Begindrag method transmits parameters FALSE, allowing FileList to separately handle mouse events and do not start dragging. In fact, this is a large number of existence. 2. DirectoryoutLine, DriveTabSet decides whether it can be dragged. Procedure Tfmform.directoryoutLinedragover (Sender, Source: Tobject; x, Y: Integer; State: TDRAGSTATE; VAR ACCEPT: BOOLEAN); Begin IF Source Is TfilelistBox Then Accept: = true; END; Procedure TFMMM.DriveTabSetDragover (Sender, Source: Tobject; x, Y: Integer; State: TDRAGSTATE; VAR ACCEPT: BOOLEAN); VAR Proppos: integer; Begin IF Source Is TfilelistBox Then With drivetabset do Begin PROPPOS: = ItemAtPOS (Point (x, y); Accept: = (Proppos> -1) and (Proppos END; END; DirectoryoutLine is unconditionally acceptable, while DRIVETABSET needs to check if it is a legitimate label. 3. Drag and put down the response DIRECTORYOUTLINE dragging down to implement file movements. Call the ConfirmChange event processing in the program, the target path is obtained by DirctoryoutLine.Items [GetItem (X, Y)]. Fullpath. Procedure Tfmform.directoryoutLinedragdrop (Sender, Source: Tobject; x, Y: integer); Begin IF Source Is TfilelistBox Then With DirectoryoutLine Do Begin Confirmchange ('Move', FileList.FileName, Items [GetItem (x, y)]. FullPath); END; END; DriveTabset dragging down the file copy function. The current location is converted to the corresponding drive letter, and the target path is obtained by currentdirlist [driveTabset.tabindex]. Procedure TFMMM.DriveTabSetDragdrop (Sender, Source: Tobject; x, y: integer); VAR Apoint: tpoint; Begin Apoint.x: = x; apoint.y: = y; DriveTabset.tabindex: = drivetabset.Itemtpos (Apoint); IF Source Is TfilelistBox Then With drivetabset do Begin IF currentdirlist [TabINDEX] <> 'THEN Confirmchange ('Copy', ThefileName, CurrentDirlist [Tabindex]); END; END; 4.FileList response Drag end, update file list Procedure Tfmform.FileListendddrag (Sender, Target: Tobject; x, y: integer; begin IF target <> nil dam.Update; END;