How do I check whether I've successfully fetched a list of file names from the clipboard?

℡╲_俬逩灬. 提交于 2019-12-11 13:32:14

问题


I have just found this code to get files from the clipboard and it works fine, but I would like to make it a Boolean function so that I know it succeeded. What do I need to test to see if it has the file(s) on the clipboard and that all is OK?

USES Clipbrd, shellapi;

// procedure GetFileNameFromClipboard(oSL : TStringlist);
function GetFileNameFromClipboard(oSL : TStringlist) : Boolean;
var
      f: THandle;
      buffer: array [0..MAX_PATH] of Char;
      i, c: Integer;
begin
      Result:=False;
      if NOT Clipboard.HasFormat(CF_HDROP) then exit;
      Clipboard.Open;
      f := Clipboard.GetAsHandle(CF_HDROP);
      if f <> 0 then 
      begin
         c := DragQueryFile(f, $FFFFFFFF, nil, 0);
         for i:=0 to c-1 do 
         begin
             buffer[0] := #0;
             DragQueryFile(f, i, buffer, SizeOf(buffer));
             oSL.Add(buffer);
         end;
      end;
      Clipboard.Close;
   Result:=???????
end;

回答1:


Try something like this:

function GetFileNameFromClipboard(oSL : TStrings) : Boolean;
var
  f: THandle;
  buffer: array [0..MAX_PATH] of Char;
  S: string;
  i, c: UINT;
begin
  Result := False;
  Clipboard.Open;
  try
    f := Clipboard.GetAsHandle(CF_HDROP);
    if f = 0 then Exit;
    c := DragQueryFile(f, $FFFFFFFF, nil, 0);
    if c = 0 then Exit;
    for i := 0 to c-1 do 
    begin
      c := DragQueryFile(f, i, buffer, Length(buffer));
      if c <> 0 then begin
        SetString(s, buffer, c);
        oSL.Add(s);
        Result := True;
      end;
    end;
  finally
    Clipboard.Close;
  end;
end;


来源:https://stackoverflow.com/questions/17011609/how-do-i-check-whether-ive-successfully-fetched-a-list-of-file-names-from-the-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!