Solved

Drag Files to and from ShellListview1 & ShellListview2 & Explorer.

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

756 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