Solved

Drag Files to and from ShellListview1 & ShellListview2 & Explorer.

Posted on 2010-09-10
1
763 Views
Last Modified: 2012-05-10
Hi, is it possible to drag files from shelllistview1 to shelllistview2? & is it possible to drag files from shelllistview2 to shelllistview2? and is it possible to drag files from explorer to shelllistview1 or shelllistview2? at the same time? making it seem like it works like any other folder?

0
Comment
Question by:eNarc
1 Comment
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 33655556
This is an example of how to drag a file from explorer to a TShellListView

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ShellCtrls, ShellAPI;

type
  TForm1 = class(TForm)
    ShellListView1: TShellListView;
    ShellComboBox1: TShellComboBox;
    procedure FormCreate(Sender: TObject);
  private
    FOriginalWindowProc: TWndMethod;
    procedure CopyToFolder(const AFileName, AFileFolder: string);
    procedure ShellFileDrop(var Msg :TWMDROPFILES);
    procedure ShellWindowProc(var Msg :TMessage);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.ShellFileDrop(var Msg: TWMDROPFILES);
var
  Buffer: array[0..MAX_PATH] of Char;
  CopyFolder: string;
  Item: TListItem;
  NumFiles: LongInt;
  P: TPoint;
  ShellFolder: TShellFolder;
begin
  NumFiles := DragQueryFile(Msg.Drop, $FFFFFFFF, nil, 0); //get number of files selected
  if NumFiles = 1 then  //this example handles just one file
  begin
    DragQueryFile(Msg.Drop, 0, @Buffer, SizeOf(Buffer));  //get the dragged file
    GetCursorPos(P);
    P := ShellListView1.ScreenToClient(P);
    Item := ShellListView1.GetItemAt(P.X, P.Y);
    if Assigned(Item) then
    begin
      ShellFolder := ShellListView1.Folders[Item.index];
      if not ShellFolder.IsFolder then
        CopyFolder := ExtractFileDir(ShellFolder.PathName)
      else
        CopyFolder := ShellFolder.PathName;
      CopyToFolder(string(Buffer), CopyFolder);
    end;
  end;
end;

procedure TForm1.ShellWindowProc(var Msg: TMessage);
begin
  if Msg.Msg = WM_DROPFILES then
    ShellFileDrop(TWMDROPFILES(Msg))
  else
    FOriginalWindowProc(Msg);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FOriginalWindowProc := ShellListView1.WindowProc;
  ShellListView1.WindowProc := ShellWindowProc;
  DragAcceptFiles(ShellListView1.Handle, True);
end;

procedure TForm1.CopyToFolder(const AFileName, AFileFolder: string);
var
  CopyToFile: string;
begin
  CopyToFile := IncludeTrailingPathDelimiter(AFileFolder) + ExtractFileName(AFileName);
  if FileExists(CopyToFile) then
    if MessageDlg('File already exists. Overwrite?', mtConfirmation, [mbYes, mbNo], 0) = mrNo then
      Exit;

  Windows.CopyFile(PChar(AFileName), PChar(CopyToFile), False);
end;

end.

//Form
object Form1: TForm1
  Left = 192
  Top = 114
  Width = 749
  Height = 263
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object ShellListView1: TShellListView
    Left = 24
    Top = 56
    Width = 705
    Height = 150
    ObjectTypes = [otFolders, otNonFolders]
    Root = 'rfDesktop'
    ShellComboBox = ShellComboBox1
    Sorted = True
    ReadOnly = False
    HideSelection = False
    TabOrder = 0
    ViewStyle = vsReport
  end
  object ShellComboBox1: TShellComboBox
    Left = 24
    Top = 24
    Width = 705
    Height = 22
    Root = 'rfDesktop'
    ShellListView = ShellListView1
    UseShellImages = True
    DropDownCount = 8
    TabOrder = 1
  end
end
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now