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 Then Begin 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.FileListendDrag (Sender, Target: TOBJECT; X, Y: Integer); Begin IF target <> nil dam.Update; END;