procedure SetFilesToClipboard(FileList: string); var DropFiles: PDropFiles; hGlobal: THandle; iLen: Integer; begin iLen := Length(FileList); hGlobal := GlobalAlloc(GMEM_SHARE or GMEM_MOVEABLE or GMEM_ZEROINIT, SizeOf(TDropFiles) + iLen); if hGlobal <> 0 then begin DropFiles := GlobalLock(hGlobal); DropFiles^.pFiles := SizeOf(TDropFiles); Move(FileList[1], (PChar(DropFiles) + SizeOf(TDropFiles))^, iLen); GlobalUnlock(hGlobal); Clipboard.Open; try Clipboard.Clear; Clipboard.SetAsHandle(CF_HDROP, hGlobal); finally Clipboard.Close; end; end; end;
type PDropFiles = ^TDropFiles; TDropFiles = record pFiles: DWORD; { offset of file list } pt: TPoint; { drop point (client coords) } fNC: BOOL; { is it on NonClient area }{ and pt is in screen coords } fWide: BOOL; { WIDE character switch } end;
HandleDropEffect := RegisterClipboardFormat('Preferred DropEffect'); hGblDropEffect := GlobalAlloc(GMEM_ZEROINIT or GMEM_MOVEABLE or GMEM_SHARE, sizeof(DWORD)); pdwDropEffect := GlobalLock(hGblDropEffect); if(bCopy) then pdwDropEffect^ := DROPEFFECT_COPY else pdwDropEffect^ := DROPEFFECT_MOVE; GlobalUnlock(hGblDropEffect);
if OpenClipboard(0) then begin EmptyClipboard(); SetClipboardData(HandleDropEffect,hGblDropEffect); SetClipboardData( CF_HDROP, hGblFileList ); CloseClipboard(); end else showmessage('无法打开,可能被别的程序占用'); end;
procedure TForm1.Button1Click(Sender: TObject); begin CutOrCopyFiles('c:\test\1.txt'+#0+'c:\test\2.txt'+#0,28,false); end;