Thema: Delphi Drag / Drop Tutorial

Einzelnen Beitrag anzeigen

Hawkeye219

Registriert seit: 18. Feb 2006
Ort: Stolberg
2.227 Beiträge
 
Delphi 2010 Professional
 
#8

Re: Drag / Drop Tutorial

  Alt 1. Jan 2008, 16:21
Hallo,

die Erklärung zu den benötigten Funktionen findest du bei Microsoft: DragAcceptFiles und DragQueryFile.

Ich habe die Routine von MrKnogge mal in eine handliche Funktion umgewandelt, dann kann man sie relativ einfach in mehreren Applikationen verwenden.

Delphi-Quellcode:
// uses ShellAPI

function GetFilesDropped (aHandle: THandle; aList: TStrings): Boolean;
var
  FileCount : Integer;
  Index : Integer;
  Buffer : array [0..MAX_PATH] of Char;
begin
  FileCount := DragQueryFile(aHandle, Cardinal(-1), nil, 0);
  aList.BeginUpdate;
  try
    for Index := 0 to FileCount - 1 do
      begin
        DragQueryFile(aHandle, Index, @Buffer, SizeOf(Buffer));
        aList.Add(Buffer);
      end;
  finally
    aList.EndUpdate;
  end;
  Result := (FileCount > 0);
end;

// in der Applikation:
type
  TForm1 = class (TForm)
    [...]
    procedure FormCreate (Sender: TObject);
  private
    procedure WMDropFiles (var aMsg: TMessage); message WM_DROPFILES;
  end;

procedure Form1.FormCreate (Sender: TObject);
begin
  DragAcceptFiles (Handle, True);
end;

procedure Form1.WMDropFiles (var aMsg: TMessage);
begin
  if GetFilesDropped(aMsg.WParam, ListBox.Items) then
    ShowMessage ('files dropped');
end;
Gruß Hawkeye
  Mit Zitat antworten Zitat