问题
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