unit ListBoxWithFileDrop;
//******************************************************************************
// TListBoxWithFileDrop is identical with TListBox with exception of two new
// One new property:
//    AcceptFileDrop: boolen;
// And one new event:
//    OnFileDrop((Sender: TObject; FileName: string; X: integer; Y: integer);
//
// Usage:
// As regular ListBox. To accept file drop from Windows Explorer:
//
// 1)  set AcceptFileDrop value to 'True' e.g.:
//     ListBoxWithFileDrop1.AcceptFileDrop:=True;
// NOTE:
//    AcceptFileDrop property has to be set in run-time. Design time setting
//    will not take effect unil reinforced by run-time action. It can be done
//    for example in hosting form OnCreate event handler:
//
//       procedure TForm1.FormCreate(Sender: TObject);
//       begin
//          ListBoxWithFileDrop1.AcceptFileDrop:= ListBoxWithFileDrop1.AcceptFileDrop;
//       end;
//
// 2)  Write code in 'OnFileDrop' event handler e.g.:
//       procedure TForm1.ListBoxWithFileDrop1FileDrop(Sender: TObject;
//          FileName: String; X, Y: Integer);
//       begin
//          ListBoxWithFileDrop1.Items.Add(FileName);
//       end;
//
//
******************************************************************************
interface

uses
  Windows, Dialogs, Messages, SysUtils, Classes, Controls, StdCtrls, ShellAPi;

type TNotifyFileDropEvent = procedure (Sender: TObject; FileName: string; X: integer; Y: integer) of object;
type
  TListBoxWithFileDrop = class(TCustomListBox)
  private
    m_acceptFile:   boolean;
    FOnFileDrop:    TNotifyFileDropEvent;

    //private functions used by 'AcceptFileDrop'
    function GetAcceptFiles: boolean;
    procedure SetAcceptFiles(Flag: boolean);
  protected
    
procedure WMDROPFILESMESSAGE (var msg:TWMDropFiles); message WM_DROPFILES;
  public
  published
    // New Event
    property OnFileDrop: TNotifyFileDropEvent read FOnFileDrop write FOnFileDrop;
    // New Property
    property AcceptFileDrop: boolean read GetAcceptFiles write SetAcceptFiles;
    // All other properties exposed as in 'regular' listbox
    property Style;
    property AutoComplete;
    property Align;
    property Anchors;
    property BevelEdges;
    property BevelInner;
    property BevelKind default bkNone;
    property BevelOuter;
    property BiDiMode;
    property BorderStyle;
    property Color;
    property Columns;
    property Constraints;
    property Ctl3D;
    property DragCursor;
    property DragKind;
    property DragMode;
    property Enabled;
    property ExtendedSelect;
    property Font;
    property ImeMode;
    property ImeName;
    property IntegralHeight;
    property ItemHeight;
    property Items;
    property MultiSelect;
    property ParentBiDiMode;
    property ParentColor;
    property ParentCtl3D;
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property ScrollWidth;
    property ShowHint;
    property Sorted;
    property TabOrder;
    property TabStop;
    property TabWidth;
    property Visible;
    property OnClick;
    property OnContextPopup;
    property OnData;
    property OnDataFind;
    property OnDataObject;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnDrawItem;
    property OnEndDock;
    property OnEndDrag;
    property OnEnter;
    property OnExit;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property OnMeasureItem;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
    property OnStartDock;
    property OnStartDrag;
  end;

procedure Register;

implementation



//------------------------------------------------------------------------------
procedure TListBoxWithFileDrop.WMDROPFILESMESSAGE (var msg:TWMDropFiles);
//------------------------------------------------------------------------------
var
   pt:         tPoint;
   Filecount:  integer;
   nCount:     integer;
   FileName:   array[0..MAX_PATH] of char;
begin
  inherited;

  try
     DragQueryPoint(msg.Drop, pt);
     Filecount:=DragQueryFile(msg.Drop, $FFFFFFFF, nil, 0);
     for nCount:=1 to FileCount do
        if DragQueryFile(msg.Drop, nCount-1, FileName, MAX_PATH)>0 then
           if Assigned(FOnFileDrop) then FOnFileDrop(Self, fileName, pt.X, pt.Y);
  finally
     DragFinish(msg.Drop);
     Msg.Result := 0;
  end;
end;


//------------------------------------------------------------------------------
function TListBoxWithFileDrop.GetAcceptFiles: boolean;
//------------------------------------------------------------------------------
begin
  Result:= m_AcceptFiles;
end;


//------------------------------------------------------------------------------
procedure TListBoxWithFileDrop.SetAcceptFiles(Flag: boolean);
//------------------------------------------------------------------------------
begin
  DragAcceptFiles(self.Handle, flag);
  m_AcceptFiles:=flag;
end;



procedure Register;
begin
  RegisterComponents('Standard', [TListBoxWithFileDrop]);
end;

end.