1

我刚刚发现这段代码可以从剪贴板中获取文件,它工作正常,但我想让它成为一个布尔函数,这样我就知道它成功了。我需要测试什么以查看剪贴板上是否有文件并且一切正常?

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;
4

1 回答 1

4

尝试这样的事情:

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;
于 2013-06-09T19:54:32.357 回答