How To Accept Drag And Drop Files Onto A ListView
| April 4, 2012 | Posted by Rivalina under Delphi, Sample Apps, Tips & Tricks, Win32 API |
You may in need of dragging and dropping files from explorer onto a listview (or another control) on a form. Here’s a simple way to deal with that.
First, we know that each Delphi control has message handler procedure known as Window Proc, for handling messages from Windows.
A ListView has this procedure of course. To implement drag and drop on a ListView, we must override the window proc of the ListView with our customized window proc, and implement code of our needs there.
The steps are:
- Save old ListView’s window proc
- Create a new window proc for the ListView and our codes there
- Filter messages from Windows. If our new window proc receives message that match our need, then process it else pass it to old saved window proc
- Enable the ListView to accept files
To have those all done, we need to to add ShellApi to uses list.
interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, ShellApi;
Then we define two procedures to handle messages, and a variable to save old window proc:
type
TForm3 = class(TForm)
ListView1: TListView;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
OldLvWndProc : TWndMethod;
procedure OnDropFiles(var msg: TWMDropFiles);
procedure LvWndProc(var Msg: TMessage);
public
{ Public declarations }
end;
Now the code for the new window proc to filter messages (we need only WM_DROPFILES):
procedure TForm3.LvWndProc(var Msg: TMessage); begin if msg.Msg = WM_DROPFILES then OnDropFiles(TWMDropFiles(msg)) else OldLvWndProc(msg); end;
And the code for handling files dropped on the ListView:
procedure TForm3.OnDropFiles(var msg: TWMDropFiles);
var
FCount, i: Integer;
FName: array [0..MAX_PATH] of char;
begin
ListView1.Items.BeginUpdate;
ListView1.Items.Clear;
try
{How many files droppped?}
FCount := DragQueryFile(msg.Drop, $FFFFFFFF, FName, MAX_PATH);
for i:= 0 to FCount-1 do
begin
{Add them to the ListView}
DragQueryFile(msg.Drop, i, FName, MAX_PATH);
with ListView1.Items.Add do
begin
Caption := STring(FName);
{Is current file a folder or a file?}
if DirectoryExists(string(FName)) then
SubItems.Add('Directory')
else
SubItems.Add('File');
end;
end;
finally
ListView1.Items.EndUpdate;
end;
end;
The last, we enable the ListView to accept files and replace window proc with the new one:
procedure TForm3.FormCreate(Sender: TObject); begin OldLvWndProc := ListView1.WindowProc; ListView1.WindowProc := LvWndProc; DragAcceptFiles(ListView1.Handle, True); end;
And here’s the whole code:
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ShellApi;
type
TForm3 = class(TForm)
ListView1: TListView;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
OldLvWndProc : TWndMethod;
procedure OnDropFiles(var msg: TWMDropFiles);
procedure LvWndProc(var Msg: TMessage);
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
{ TForm3 }
procedure TForm3.FormCreate(Sender: TObject);
begin
OldLvWndProc := ListView1.WindowProc;
ListView1.WindowProc := LvWndProc;
DragAcceptFiles(ListView1.Handle, True);
end;
procedure TForm3.LvWndProc(var Msg: TMessage);
begin
if msg.Msg = WM_DROPFILES then
OnDropFiles(TWMDropFiles(msg))
else
OldLvWndProc(msg);
end;
procedure TForm3.OnDropFiles(var msg: TWMDropFiles);
var
FCount, i: Integer;
FName: array [0..MAX_PATH] of char;
begin
ListView1.Items.BeginUpdate;
ListView1.Items.Clear;
try
{How many files droppped?}
FCount := DragQueryFile(msg.Drop, $FFFFFFFF, FName, MAX_PATH);
for i:= 0 to FCount-1 do
begin
{Add them to the ListView}
DragQueryFile(msg.Drop, i, FName, MAX_PATH);
with ListView1.Items.Add do
begin
Caption := STring(FName);
{Is current file a folder or a file?}
if DirectoryExists(string(FName)) then
SubItems.Add('Directory')
else
SubItems.Add('File');
end;
end;
finally
ListView1.Items.EndUpdate;
end;
end;
end.
Hope this post helps someone who in need. Leave comment if you have any question
Drag And Drop Files Onto A ListView (248.4 KiB)
Incoming search terms:
- contoh aplikasi delphi dengan cxgrid
- winapi drop file in control
- contoh script yg menggunakan shellapi
- clistview Accept files
- wallpaper asian girl
- ondropfiles
- how to handle drag drop link on lazarus component
- how to drag and drop onto a windows form
- drag and drop explorer listview
- delphi listview file drop
Comments
Powered by Facebook Comments









This is the default footer layout. You can easily add or remove columns in the footer.
klo bikin kebalikanya gmn bang,, dari list view di drag ke windows explorer
Hi, Your example is very usefull. I need just this example, but drop files onto a TcxTreeList (really to drop files onto a treenode). I made the component change, and the example works, but i can’t select a particular node from the tree to drop the file.
Could you tell me how to do this?
Thanks!
Ramiro