2010. április 14., szerda

How to get the file name (and directory) of a file in the clipboard


Problem/Question/Abstract:

I need to have the filename and directory of the file which is in the clipboard. For example: In the Explorer I select a file, press ctrl+c and in my application I would like copy or do anything with it.

Answer:

procedure TMainForm.Button1Click(Sender: TObject);
var
  hDrop: THandle;
  cnt, i: Integer;
  filename: array[0..MAX_PATH - 1] of Char;
begin
  if ClipBoard.HasFormat(CF_HDROP) then
  begin
    hDrop := ClipBoard.GetAsHandle(CF_HDROP);
    cnt := DragQueryFile(hDrop, $FFFFFFFF, nil, 0);
    Memo.Clear;
    Memo.Lines.Add(Format('Found %d files:', [cnt]));
    for i := 0 to cnt - 1 do
    begin
      DragQueryFile(hDrop, i, @filename, MAX_PATH);
      Memo.lines.Add(Format('%2d: %s', [i + 1, filename]));
    end;
  end
  else
    MessageDlg(' No filename in clipboard!', mtInformation, [mbOk], 0);
end;

Nincsenek megjegyzések:

Megjegyzés küldése