处理一个windows消息就行了,对不起,我忘了...

解决方案 »

  1.   

    //componet pasunit DropFiles;
    //E-Mail: [email protected]
      Messages, Windows, SysUtils, Controls, Forms, Classes, Menus, Graphics,
      StdCtrls, ShellApi, ComCtrls;type
      TDropFilesEvent = procedure(Sender: TObject; X, Y: Integer; FileNames: TStrings) of object;type
      TDropFilesMemo = class(TCustomMemo)
      private
        FCanDropFiles: Boolean;
        FOnDropFiles: TDropFilesEvent;
        procedure WMDropFiles(var Msg: TMessage); message WM_DROPFILES;
        procedure SetCanDropFiles(const Value: Boolean);
      published
        property Align;
        property Alignment;
        property Anchors;
        property BevelEdges;
        property BevelInner;
        property BevelKind default bkNone;
        property BevelOuter;
        property BiDiMode;
        property BorderStyle;
        property Color;
        property Constraints;
        property Ctl3D;
        property DragCursor;
        property DragKind;
        property DragMode;
        property Enabled;
        property Font;
        property HideSelection;
        property ImeMode;
        property ImeName;
        property Lines;
        property MaxLength;
        property OEMConvert;
        property ParentBiDiMode;
        property ParentColor;
        property ParentCtl3D;
        property ParentFont;
        property ParentShowHint;
        property PopupMenu;
        property ReadOnly;
        property ScrollBars;
        property ShowHint;
        property TabOrder;
        property TabStop;
        property Visible;
        property WantReturns;
        property WantTabs;
        property WordWrap;
        property OnChange;
        property OnClick;
        property OnContextPopup;
        property OnDblClick;
        property OnDragDrop;
        property OnDragOver;
        property OnEndDock;
        property OnEndDrag;
        property OnEnter;
        property OnExit;
        property OnKeyDown;
        property OnKeyPress;
        property OnKeyUp;
        property OnMouseDown;
        property OnMouseMove;
        property OnMouseUp;
        property OnStartDock;
        property OnStartDrag;
        property CanDropFiles: Boolean read FCanDropFiles write SetCanDropFiles;
        property OnDropFiles: TDropFilesEvent read FOnDropFiles write FOnDropFiles;
      end;  TDropFilesEdit = class(TCustomEdit)
      private
        FCanDropFiles: Boolean;
        FOnDropFiles: TDropFilesEvent;
        procedure WMDropFiles(var Msg: TMessage); message WM_DROPFILES;
        procedure SetCanDropFiles(const Value: Boolean);
      published
        property Anchors;
        property AutoSelect;
        property AutoSize;
        property BevelEdges;
        property BevelInner;
        property BevelKind default bkNone;
        property BevelOuter;
        property BiDiMode;
        property BorderStyle;
        property CharCase;
        property Color;
        property Constraints;
        property Ctl3D;
        property DragCursor;
        property DragKind;
        property DragMode;
        property Enabled;
        property Font;
        property HideSelection;
        property ImeMode;
        property ImeName;
        property MaxLength;
        property OEMConvert;
        property ParentBiDiMode;
        property ParentColor;
        property ParentCtl3D;
        property ParentFont;
        property ParentShowHint;
        property PasswordChar;
        property PopupMenu;
        property ReadOnly;
        property ShowHint;
        property TabOrder;
        property TabStop;
        property Text;
        property Visible;
        property OnChange;
        property OnClick;
        property OnContextPopup;
        property OnDblClick;
        property OnDragDrop;
        property OnDragOver;
        property OnEndDock;
        property OnEndDrag;
        property OnEnter;
        property OnExit;
        property OnKeyDown;
        property OnKeyPress;
        property OnKeyUp;
        property OnMouseDown;
        property OnMouseMove;
        property OnMouseUp;
        property OnStartDock;
        property OnStartDrag;
        property CanDropFiles: Boolean read FCanDropFiles write SetCanDropFiles;
        property OnDropFiles: TDropFilesEvent read FOnDropFiles write FOnDropFiles;
      end;  TDropFilesRichEdit = class(TCustomRichEdit)
      private
        FCanDropFiles: Boolean;
        FOnDropFiles: TDropFilesEvent;
        procedure WMDropFiles(var Msg: TMessage); message WM_DROPFILES;
        procedure SetCanDropFiles(const Value: Boolean);
      published
        property Align;
        property Alignment;
        property Anchors;
        property BevelEdges;
        property BevelInner;
        property BevelOuter;
        property BevelKind default bkNone;
        property BevelWidth;
        property BiDiMode;
        property BorderStyle;
        property BorderWidth;
        property Color;
        property Ctl3D;
        property DragCursor;
        property DragKind;
        property DragMode;
        property Enabled;
        property Font;
        property HideSelection;
        property HideScrollBars;
        property ImeMode;
        property ImeName;
        property Constraints;
        property Lines;
        property MaxLength;
        property ParentBiDiMode;
        property ParentColor;
        property ParentCtl3D;
        property ParentFont;
        property ParentShowHint;
        property PlainText;
        property PopupMenu;
        property ReadOnly;
        property ScrollBars;
        property ShowHint;
        property TabOrder;
        property TabStop default True;
        property Visible;
        property WantTabs;
        property WantReturns;
        property WordWrap;
        property OnChange;
        property OnContextPopup;
        property OnDragDrop;
        property OnDragOver;
        property OnEndDock;
        property OnEndDrag;
        property OnEnter;
        property OnExit;
        property OnKeyDown;
        property OnKeyPress;
        property OnKeyUp;
        property OnMouseDown;
        property OnMouseMove;
        property OnMouseUp;
        property OnMouseWheel;
        property OnMouseWheelDown;
        property OnMouseWheelUp;
        property OnProtectChange;
        property OnResizeRequest;
        property OnSaveClipboard;
        property OnSelectionChange;
        property OnStartDock;
        property OnStartDrag;
        property CanDropFiles: Boolean read FCanDropFiles write SetCanDropFiles;
        property OnDropFiles: TDropFilesEvent read FOnDropFiles write FOnDropFiles;
      end;  TDropFilesListBox = class(TCustomListBox)
        procedure WMDropFiles(var Msg: TMessage); message WM_DROPFILES;
        procedure SetCanDropFiles(const Value: Boolean);
      private
        FCanDropFiles: Boolean;
        FOnDropFiles: TDropFilesEvent;
      published
        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;
        property CanDropFiles: Boolean read FCanDropFiles write SetCanDropFiles;
        property OnDropFiles: TDropFilesEvent read FOnDropFiles write FOnDropFiles;
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('Zsoft', [TDropFilesMemo, TDropFilesEdit, TDropFilesRichEdit]);
    end;{ TDropFilesMemo }procedure TDropFilesMemo.SetCanDropFiles(const Value: Boolean);
    begin
      FCanDropFiles := Value;
      DragAcceptFiles(Handle, FCanDropFiles);
    end;procedure TDropFilesMemo.WMDropFiles(var Msg: TMessage);
    var
      I, FilesCount: Integer;
      FileName: array[0..MAX_PATH] of Char;
      Point: TPoint;
      FilesNames: TStringList;
    begin
      if Assigned(FOnDropFiles) then begin
        FilesNames := TStringList.Create;
        try
          FilesCount := DragQueryFile(THandle(Msg.wParam), Cardinal(-1), nil, 0);
          DragQueryPoint(THandle(Msg.wParam), Point);
          for I := 0 to FilesCount - 1 do begin
            DragQueryFile(THandle(Msg.wParam), I, FileName,
              SizeOf(FileName));
            FilesNames.Add(FileName);
          end;
          FOnDropFiles(Self, Point.X, Point.Y, FilesNames);
        finally
          FilesNames.Free;
        end;
      end;
      Msg.Result := 0;
      DragFinish(THandle(Msg.wParam));
    end;{ TDropFilesEdit }procedure TDropFilesEdit.SetCanDropFiles(const Value: Boolean);
    begin
      FCanDropFiles := Value;
      DragAcceptFiles(Handle, FCanDropFiles);
    end;procedure TDropFilesEdit.WMDropFiles(var Msg: TMessage);
    var
      I, FilesCount: Integer;
      FileName: array[0..MAX_PATH] of Char;
      Point: TPoint;
      FilesNames: TStringList;
    begin
      if Assigned(FOnDropFiles) then begin
        FilesNames := TStringList.Create;
        try
          FilesCount := DragQueryFile(THandle(Msg.wParam), Cardinal(-1), nil, 0);
          DragQueryPoint(THandle(Msg.wParam), Point);
          for I := 0 to FilesCount - 1 do begin
            DragQueryFile(THandle(Msg.wParam), I, FileName,
              SizeOf(FileName));
            FilesNames.Add(FileName);
          end;
          FOnDropFiles(Self, Point.X, Point.Y, FilesNames);
        finally
          FilesNames.Free;
        end;
      end;
      Msg.Result := 0;
      DragFinish(THandle(Msg.wParam));
    end;{ TDropFilesRichEdit }procedure TDropFilesRichEdit.SetCanDropFiles(const Value: Boolean);
    begin
      FCanDropFiles := Value;
      DragAcceptFiles(Handle, FCanDropFiles);
    end;procedure TDropFilesRichEdit.WMDropFiles(var Msg: TMessage);
    var
      I, FilesCount: Integer;
      FileName: array[0..MAX_PATH] of Char;
      Point: TPoint;
      FilesNames: TStringList;
    begin
      if Assigned(FOnDropFiles) then begin
        FilesNames := TStringList.Create;
        try
          FilesCount := DragQueryFile(THandle(Msg.wParam), Cardinal(-1), nil, 0);
          DragQueryPoint(THandle(Msg.wParam), Point);
          for I := 0 to FilesCount - 1 do begin
            DragQueryFile(THandle(Msg.wParam), I, FileName,
              SizeOf(FileName));
            FilesNames.Add(FileName);
          end;
          FOnDropFiles(Self, Point.X, Point.Y, FilesNames);
        finally
          FilesNames.Free;
        end;
      end;
      Msg.Result := 0;
      DragFinish(THandle(Msg.wParam));
    end;{ TDropFilesListBox }procedure TDropFilesListBox.SetCanDropFiles(const Value: Boolean);
    begin
      FCanDropFiles := Value;
      DragAcceptFiles(Handle, FCanDropFiles);
    end;procedure TDropFilesListBox.WMDropFiles(var Msg: TMessage);
    var
      I, FilesCount: Integer;
      FileName: array[0..MAX_PATH] of Char;
      Point: TPoint;
      FilesNames: TStringList;
    begin
      if Assigned(FOnDropFiles) then begin
        FilesNames := TStringList.Create;
        try
          FilesCount := DragQueryFile(THandle(Msg.wParam), Cardinal(-1), nil, 0);
          DragQueryPoint(THandle(Msg.wParam), Point);
          for I := 0 to FilesCount - 1 do begin
            DragQueryFile(THandle(Msg.wParam), I, FileName,
              SizeOf(FileName));
            FilesNames.Add(FileName);
          end;
          FOnDropFiles(Self, Point.X, Point.Y, FilesNames);
        finally
          FilesNames.Free;
        end;
      end;
      Msg.Result := 0;
      DragFinish(THandle(Msg.wParam));
    end;end.
    //demo pas
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, DropFiles;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
      private
        { Private declarations }
        FDropFilesListBox: TDropFilesListBox;
        procedure DropFilesListBoxDropFiles(Sender: TObject; X, Y: Integer; FileNames: TStrings);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.DropFilesListBoxDropFiles(Sender: TObject; X, Y: Integer;
      FileNames: TStrings);
    begin
      TListBox(Sender).Items.Assign(FileNames);
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      FDropFilesListBox := TDropFilesListBox.Create(Self);
      FDropFilesListBox.Parent := Self;
      FDropFilesListBox.Width := 200;
      FDropFilesListBox.Height := 100;
      FDropFilesListBox.CanDropFiles := True;
      FDropFilesListBox.OnDropFiles := DropFilesListBoxDropFiles;
    end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
      FDropFilesListBox.Free;
    end;end.
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Controls, Forms, Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        ListBox1: TListBox;
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        procedure WMDropFiles(var Message: TMessage); message WM_DropFiles;
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    uses ShellApi;procedure TForm1.WMDropFiles(var Message: TMessage);
    var
      sFileName: array [0..255] of char;
      iIndex, iCount: integer;
    begin
      iIndex := -1;
      iCount := DragQueryFile(Message.WParam, iIndex, sFileName, 255);
      for iIndex := 0 to iCount - 1 do
      begin
        DragQueryFile(Message.WParam, iIndex, sFileName, 255);
        ListBox1.Items.Add(sFileName);
      end;
      DragFinish(Message.WParam);
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      DragAcceptFiles(Handle, true);
    end;end.