加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 如何使用Windows剪贴板中复制的文件加载流

发布时间:2020-12-15 09:35:12 所属栏目:大数据 来源:网络整理
导读:我已将文件复制到 Windows剪贴板中(只需单击右键,复制). 我想加载一个TStream后代与当前存储在剪贴板中的文件. uses Classes,Clipbrd;MyStream := TMemoryStream.Create;try //here i would like to load the clipboard file into MyStreamfinally MyStream.
我已将文件复制到 Windows剪贴板中(只需单击右键,复制).
我想加载一个TStream后代与当前存储在剪贴板中的文件.

uses
  Classes,Clipbrd;

MyStream := TMemoryStream.Create;
try
  //here i would like to load the clipboard file into MyStream
finally
  MyStream.Free;
end;

解决方法

将文件从硬盘驱动器复制到剪贴板时,它只是以 CF_HDROP格式复制文件的完整路径和文件名.您可以使用 DragQueryFile()函数来读取文件名,例如:

uses
  Classes,Clipbrd,ShellAPI;

var
  hDrop: THandle
  MyStream: TMemoryStream;
  Files: TStringList;
  NumFiles,FileIdx: DWORD;
  FileName: array[0..MAX_PATH] of Char;
  I: Integer;
begin
  Files := TStringList.Create;
  try
    Clipboard.Open;
    try
      if Clipboard.HasFormat(CF_HDROP) then
      begin
        // DO NOT free this handle,the clipboard owns it!
        hDrop := Clipboard.GetAsHandle(CF_HDROP);
        NumFiles := DragQueryFile(hDrop,$FFFFFFFF,nil,0);
        if NumFiles <> 0 then
        begin
          for FileIdx := 0 to NumFiles-1 do
          begin
            if DragQueryFile(hDrop,FileIdx,FileName,MAX_PATH) <> 0 then
              Files.Add(FileName);
          end;
        end;
      end;
    finally
      Clipboard.Close;
    end;
    for I := 0 to Files.Count-1 do
    begin
      MyStream := TMemoryStream.Create;
      try
        MyStream.LoadFromFile(Files[I]);
        MyStream.Position := 0;
        // use MyStream as needed...
      finally
        MyStream.Free;
      end;
    end;
  finally
    Files.Free;
  end;
end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读