Nineth Chapter-delphi Drag-and-drop programming (4)

Source: Internet
Author: User
Tags file copy integer

9.3 Drag-and-drop application instances: File Manager Drag-and-drop support

In the sixth chapter of the last development of the File Manager application instance, although the function has begun to take shape, but in the operation of the Windows File Manager compared with a large enough. One of the biggest drawbacks is that it does not support drag-and-drop movement and drag-and-drop copies of files. At the end of this chapter, we can make up for the flaw.

File drag-and-drop movement refers to when a user drags a file to a directory under the directory tree and drops it. The file is automatically moved to the directory, and the file drag-and-drop copy refers to the file being automatically copied to the current directory of the drive when the user drags a file onto a drive label and drops it. The File list box as the source control and the directory tree and drive label that are the target control can be in different child windows. The current directory of the drive is the latest action result for any child window, regardless of whether the child window is related to the drag source or the drag target.

To achieve these functions, there are two issues that must be addressed first:

1. How do I record the current directory for each drive?

For this we have defined a global variable:

Var

CURENTDIRLIST:ARRAY[0...25] of string[70];

In the Directoryoutline onchange event:

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

Because Drivetabset responds to the OnClick event before responding to the Ondragdrop event and fires the Directoryoutline onchange event by the event, This ensures that the Currentdirlist array entry used at any time in the Ondragdrop event is not an empty string.

2. How to ensure that the mobile, copy and child window of the independent sex?

A key issue here is that when we judge the source control, we use the IS operator for type checking:

If Source is tfilelist then

...

If we use the following statement:

If Source = FileList Then

...

The move and copy operations are limited to the book window.

When the above problem is solved, our work is simply to follow the general development steps of drag and drop, step-by-step to complete.

1.FileList Start drag operation

Procedure Tfmform.filelistmousedown (Sender:tobject; Button:tmousebutton;

Shift:tshiftstate; X, Y:integer);

Begin

If Button = Mbleft Then

With Sender as Tfilelistbox do

Begin

If Itematpos (Point (X, Y), True) >= 0 Then

BeginDrag (False);

End

End

Itematpos is used to check whether a file exists at this moment. The BeginDrag method passes the argument false, allowing the filelist to handle mouse events individually instead of starting dragging. In fact, there is a large number of such situations.

2.DirectoryOutline, Drivetabset decide whether to accept drag in place drop.

Procedure Tfmform.directoryoutlinedragover (Sender, Source:tobject; X

Y:integer; State:tdragstate; var accept:boolean);

Begin

If Source is Tfilelistbox then

Accept: = True;

End

Procedure Tfmform.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 < tabs.count);

End

End

Directoryoutline is unconditional acceptance, and drivetabset need to check whether the label is legal.

3. Drag down the response

Directoryoutline Drag drop is used to implement file mobility. The Confirmchange event handler is invoked in the program, and the target path is Dirctoryoutline.items[getitem (X,y)]. FullPath to get.

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 Drag drop is used to implement file copy functionality. In the program, the current position is converted to the corresponding drive letter, and the target path is obtained by Currentdirlist[drivetabset.tabindex.

Procedure Tfmform.drivetabsetdragdrop (Sender, Source:tobject; X,y:integer);

Var

Apoint:tpoint;

Begin

Apoint.x: = X; Apoint.y: = Y;

Drivetabset.tabindex: = Drivetabset.itematpos (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 then filelist.update;

End

So far, our File Manager functionality is strong enough. But there are a lot of questions that readers can go into.

A step-by-step approach, such as:

1. The establishment of File and application association;

2. Display more file information in the File list box;

3. The files in the File list box are sorted by suffix and so on.

The file Manager is a truly comprehensive routine, and delving into it will give you a further model of the essence of Delphi programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.