Solved

Drag Files to and from ShellListview1 & ShellListview2 & Explorer.

Posted on 2010-09-10
1
771 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya 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 Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

751 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