Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Drag Files to and from ShellListview1 & ShellListview2 & Explorer.

Posted on 2010-09-10
1
Medium Priority
?
777 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:
Ephraim Wangoya earned 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

927 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