Recent

Author Topic: copying files/ folders to clipboard  (Read 5692 times)

mmab

  • Jr. Member
  • **
  • Posts: 89
copying files/ folders to clipboard
« on: October 19, 2012, 10:14:42 pm »
hi, is there a cross platform way to copy files or folders using clipbrd unit to clipbrd?

i found the flollowing delphi code but it does not work under lazarus
Code: [Select]
uses
  ShlObj, ClipBrd;

procedure CopyFilesToClipboard(FileList: string);
var
  DropFiles: PDropFiles;
  hGlobal: THandle;
  iLen: Integer;
begin
  iLen := Length(FileList) + 2;
  FileList := FileList + #0#0;
  hGlobal := GlobalAlloc(GMEM_SHARE or GMEM_MOVEABLE or GMEM_ZEROINIT,
    SizeOf(TDropFiles) + iLen);
  if (hGlobal = 0) then raise Exception.Create('Could not allocate memory.');
  begin
    DropFiles := GlobalLock(hGlobal);
    DropFiles^.pFiles := SizeOf(TDropFiles);
    Move(FileList[1], (PChar(DropFiles) + SizeOf(TDropFiles))^, iLen);
    GlobalUnlock(hGlobal);
    Clipboard.SetAsHandle(CF_HDROP, hGlobal);
  end;
end;

// Example, Beispiel:

procedure TForm1.Button1Click(Sender: TObject);
begin
  CopyFilesToClipboard('C:\Bootlog.Txt'#0'C:\AutoExec.Bat');
end;

{
  Separate the files with a #0.
  Dateien mit einem #0 trennen.
}
« Last Edit: October 19, 2012, 11:04:31 pm by mmab »

agorka

  • New Member
  • *
  • Posts: 25
Re: copying files/ folders to clipboard
« Reply #1 on: July 30, 2013, 08:22:50 pm »
Hello! I have been experiencing the same problem, trying to copy file from my application and paste at in windows explorer.

I found this workaround for that code, but it seems to be windows-only:

Code: [Select]
procedure CopyFilesToClipboard(FileList: string);
var
  DropFiles: PDropFiles;
  hGlobal: THandle;
  iLen: integer;
begin
  iLen := Length(FileList) + 2;
  FileList := FileList + #0#0;
  hGlobal := GlobalAlloc(GMEM_SHARE or GMEM_MOVEABLE or GMEM_ZEROINIT,
    SizeOf(TDropFiles) + iLen);
  if (hGlobal = 0) then
    raise Exception.Create('Could not allocate memory.');
  begin
    DropFiles := GlobalLock(hGlobal);
    DropFiles^.pFiles := SizeOf(TDropFiles);
    Move(FileList[1], (PChar(DropFiles) + SizeOf(TDropFiles))^, iLen);
    GlobalUnlock(hGlobal);
    OpenClipboard(Form1.Handle);
    EmptyClipboard;
    SetClipboardData(CF_HDROP,hGlobal);
    CloseClipboard;
   end;
end;

 

TinyPortal © 2005-2018