I am only dragging 1 file if that will simplify any.
Errors?
Improvements?
<EDIT: I am dragging to another application, not receiving a dragged file.
<http://groups.google.com/group/borland.public.delphi.nativeapi/browse_thread/thread/3807c98996c6dc4a/906ac37701139ab8?q=How+to+automatically+drag+and+drops+a+file+onto+any+window>
or
procedure DoDropFiles(Wnd: HWND; Files: TStringList);
var
Size: Cardinal;
DropFiles: PDropFiles;
Run: PChar;
MemHandle: THandle;
I: Integer;
begin
// first determine size of string buffer we have to allocate
Size := 0;
for I := 0 to Files.Count - 1 do
begin
// number of characters per string (as ANSI) plus one #0 terminator
Inc(Size, Length(Files[I]) + 1);
end;
if Size > 0 then
begin
Inc(Size, 1 + SizeOf(TDropFiles));
// allocate globally accessible memory
MemHandle := GlobalAlloc(GHND or GMEM_SHARE, Size);
DropFiles := GlobalLock(MemHandle);
// fill the header
with DropFiles^ do
begin
end;
// and finally the file names
Run := Pointer(DropFiles);
Inc(Run, SizeOf(TDropFiles));
for I := 0 to Files.Count - 1 do
begin
StrPCopy(Run, Files[I]);
// Inc(Run, Length(Files[I]));
Inc(Run, Length(Files[I])+1)
end;
// put a final #0 character at the end
Run^ := #0;
// release the lock we have to the memory,...
GlobalUnlock(MemHandle);
// ...do the message...
// SendMessage(Wnd, WM_DROPFILES, MemHandle, 0);
PostMessage(Wnd, WM_DROPFILES, MemHandle, 0);
// ... and finally release the memory
GlobalFree(MemHandle);
end;
end;
procedure TMainForm.Button1Click(Sender: TObject);
var
List: TStringList;
begin
List := TStringList.Create;
try
List.Add('C:\Data\Test.txt');
DoDropFiles(Handle, List);
finally
List.Free;
end;
end;
Norm
Edited by: Norm Carlberg on Sep 25, 2010 6:40 AM