Delphi implementation let TListView receive file drag and drop

Source: Internet
Author: User
Tags resource win32 visual studio

It is often necessary to use the TListView list control when developing applications using Delphi, but it is not enough to use the original properties, methods, and events of TListView, and it is often required to extend the functionality of the list as it develops during the process of development.

For example, the list to add any column combination sorting, ListItem drag, Customdraw from drawing, add background map and other functions, this shows that Delphi TListView list of the control function is limited, does not make people feel satisfied. To make the list more functional, we have to turn to a third party control or write our own code to transform the TListView.

Recently, in the process of developing an application with Delphi, I need to implement the function of letting TListView list to be dragged and dropped by the resource Manager or File Manager file, and displaying the file name and path received in the TListView list. I couldn't find a third party control that could implement this feature, so I groped for my own code to implement it. Now the implementation of the function to be sorted out, convenient for everyone later in the work need to use similar functions as a reference.

First, programming ideas

The way to receive files in a Windows application is this: first, the application needs to use void DragAcceptFiles (HWND hwnd, BOOL faccept) The function declares that the handle of a window or control in the application can accept the Wm_dropfiles message sent by the resource Manager or File Manager, the handle of the window or control is specified by the HWND of the DragAcceptFiles () parameter, and the corresponding increase in the application to the WM_ Dropfiles the code to process the message, and when the application (or, precisely, the DragAcceptFiles () function is called to the window or control that corresponds to the handle specified in the parameter HWND) receives the Wm_dropfiles message, The application calls Dragqueryfile, Dragfinish, dragquerypoint These three functions to the received message processing, the detailed use of these functions and instructions see Delphi Win32.hlp or Microsoft MSDN in the Visual Studio development package. Use the Tapplication OnMessage event in Delphi to process the Wm_dropfiles message.

Second, the realization step

Start Delphi, create a new project Project1, drag a tlistview list from the Win32 Control Panel to the window Form1, set the Viewstyle property to VSReport, and then add two columns to the ListView1. filename and path are respectively. OK, the list is complete, and now it's time to start coding in Unit1.pas (don't forget to add the SHELLAPI unit), as follows:

Unit Unit1;
Interface
Uses
Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs, Shellapi,
Comctrls;
Type
TForm1 = Class (Tform)
Listview1:tlistview;
Procedure Formcreate (Sender:tobject);
Private
{Private declarations}
Public
{Public declarations}
Procedure Appmessage (var msg:tmsg; var handled:boolean);
End
Var
Form1:tform1;
Implementation
{$R *. DFM}
Procedure Tform1.formcreate (Sender:tobject);
Begin
file://settings need to process files Wm_dropfiles drag-and-drop messages
DragAcceptFiles (Listview1.handle, TRUE);
file://set the Appmessage procedure to capture all messages
Application.onmessage: = Appmessage;
End
Procedure Tform1.appmessage (var msg:tmsg; var handled:boolean);
Var
Nfiles, I:integer;
filename:string;
Listitem:tlistitem;
Begin
//
Attention! All messages will come through here!
Do not write too many or long hours of code in this process, or you will affect the performance of your program
//
Determine if the Wm_dropfiles message is sent to ListView1
if (Msg.message = Wm_dropfiles) and (Msg.hwnd = Listview1.handle) Then
Begin
Number of dropped files taken
Nfiles: = Dragqueryfile (Msg.wparam, $FFFFFFFF, nil, 0);
Loops the full file name of each dragged file
Try
For I: = 0 to NFiles-1 do
Begin
allocating buffers for file names allocate memory
SetLength (Filename, 80);
Fetch filename Read the file name
Dragqueryfile (Msg.wparam, I, Pchar (Filename), 80);
FileName: = pchar (filename);
file://the full file name decomposition process file name and path
ListItem: = LISTVIEW1.ITEMS.ADD;
Listitem.caption: = Extractfilename (FileName);
LISTITEM.SUBITEMS.ADD (Extractfilepath (FileName));
End
Finally
file://end this drag-and-drop operation
Dragfinish (Msg.wparam);
End
The file://identity has processed this message
Handled: = True;
End
End
End.

Iii. Compiling and running

Press F9 to compile, run the Project1 program, open Explorer or File Manager, and drag and drop some files into the Project1 program, when the mouse pointer moves to ListView1, the pointer becomes a drag shape, this time the mouse button, see? FileName and path two columns show the file names and paths that are dragged and dropped into the PROJECT1 program, so that the TListView accept the file drag-and-drop function successfully.

Note: This procedure in the Delphi 5,windows Professional Chinese version debugging run through.

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.