Solved

Drag Files to and from ShellListview1 & ShellListview2 & Explorer.

Posted on 2010-09-10
1
769 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 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