Den her har vist alt hvad du har brug for:
unit DragNDrop;
{ DropForm.Pas - written by Nikias Klohr for the
"drag'n'drop with Delphi" - tuturial at
www.SNTD.de E-Mail: Nikias@stnd.de
use this unit wherever you like
would be nice if you write me a mail if you use it in a "good" application !
}
interface
uses Windows,Messages,ShellApi,SysUtils;
// add "procedure DropFile(var message: TWMDropFiles); message WM_DROPFILES;"
// to the Form. Then call any code you need to from this procedure
// Also add "DragAcceptFiles(handle,true);" at FormCreate; Uses ShellApi
type
TDropGotFileProc = function(FileName: String; Count: Integer): Boolean;
function DropPoint(DropMsg: TWMDropFiles): TPoint;
function DropFileCount(DropMsg: TWMDropFiles): Integer;
function DropGetFile(DropMsg: TWMDropFiles): String; overload;
// Gets the first file if there are more than one files added !
function DropGetFile(DropMsg: TWMDropFiles; Index: Integer): String; overload;
function DropGetFileExt(DropMsg: TWMDropFiles): String; overload;
// Gets the first fileextension if there are more than one files added !
function DropGetFileExt(DropMsg: TWMDropFiles; Index: Integer): String; overload;
function DropDifferentExt(DropMsg: TWMDropFiles): Boolean;
// Finds out if the dropped files all have the same extension
procedure DropGetFiles(DropMsg: TWMDropFiles; GotFileProc: TDropGotFileProc); overload;
// Calls GotFileProc for any file which was droped
// If the result of GotFileProc is false then DropGetFiles stops calling it !
procedure Dropped(DropMsg: TWMDropFiles);
// Call this when you're finished with the drop operation !!!
implementation
function DropPoint(DropMsg: TWMDropFiles): TPoint;
begin
DragQueryPoint(DropMsg.Drop, Result);
end;
function DropFileCount(DropMsg: TWMDropFiles): Integer;
begin
Result := DragQueryFile(DropMsg.Drop, $FFFFFFFF, nil, 0);
end;
function DropGetFile(DropMsg: TWMDropFiles): String; overload;
begin
Result := DropGetfile(DropMsg, 0);
end;
function DropGetFileExt(DropMsg: TWMDropFiles): String;
begin
Result := ExtractFileExt(DropGetFile(DropMsg));
end;
function DropGetFileExt(DropMsg: TWMDropFiles; Index: Integer): String;
begin
Result := ExtractFileExt(DropGetFile(DropMsg, Index));
end;
function DropDifferentExt(DropMsg: TWMDropFiles): Boolean;
var
I: Integer;
S: String;
begin
Result := False;
S := DropGetFileExt(DropMsg);
for i := 1 to DropFileCount(DropMsg) -1 do
if S <> DropGetFileExt(DropMsg, I) then
begin
Result := True;
Exit;
end;
end;
function DropGetFile(DropMsg: TWMDropFiles; Index: Integer): String; overload;
var
P: PChar;
begin
GetMem(P, 255);
DragQueryFile(DropMsg.Drop, Index, P, 255);
Result := P;
FreeMem(P, 255);
end;
procedure DropGetFiles(DropMsg: TWMDropFiles; GotFileProc: TDropGotFileProc); overload;
var
I: Integer;
begin
for I := 0 to DropFileCount(DropMsg) -1 do
if not GotFileProc(DropGetFile(DropMsg, I), I) then Exit;
end;
procedure Dropped(DropMsg: TWMDropFiles);
begin
DragFinish(DropMsg.Drop);
end;
end.