zoukankan      html  css  js  c++  java
  • 如何将文件拷贝到剪贴板

    怎样判断当前剪贴板中的内容为文件,如何将指定文件(不是文件内容)拷贝到剪贴板中。请问各位大侠:怎样判断当前剪贴板中的内容为文件,如何将指定文件(不是文件内容)拷贝到剪贴板中。
    int GetClipboardFormatName(
      UINT format,            // clipboard format to retrieve
      LPTSTR lpszFormatName,  // address of buffer for name
      int cchMaxCount         // length of name string in characters
    );
    如果format=CF_HDROP就是文件了

    HANDLE SetClipboardData(
      UINT uFormat, // clipboard format
      HANDLE hMem   // data handle
    );

    menxin的方案是可行的.Delphi定义了两个格式:CF_PICTURE和CF_COMPONENT.
    用户可以定义自己的格式.不过因为Formats是WORD格式,所以系统中只能有
    16种格式.打开ClipBrd单元,可以查到.
      CF_PICTURE := RegisterClipboardFormat('Delphi Picture');
      CF_COMPONENT := RegisterClipboardFormat('Delphi Component');
    你添加:
      CF_MYFILE := RegisterClipboardFormat(' My File Format');

    剪贴板操作:
    打开;
    清空;
    SetFormatData(CF_TEXT)  --->文件名;
    SetFormatData(CF_MYFILE)  --->文件内容
    关闭;

    如果你只对文件名感兴趣,那只是一个文本格式.取出内容,用FileExists查询
    是否存在即可.
    registerClipboardFormat函数登记新的剪贴板格式
    格式的值在OXC000和0XFFF范围
    IsClipboardFormatAvailable函数判断剪贴板是否包含指定格式数据
    格式可用返回非零值
    windows是有定义:
    可以用cf_Hdrop;

    uses shlobj,activex,clipbrd;

    procedure TForm1.Button1Click(Sender: TObject);
    var
      FE:TFormatEtc;
      Medium: TStgMedium;
      FileName:String;
      dropfiles:PDropFiles;
      pFile:PChar;
    begin
      FileName:='c:\1.bmp';
      FE.cfFormat := CF_HDROP;
      FE.dwAspect := DVASPECT_CONTENT;
      FE.tymed := TYMED_HGLOBAL;
      Medium.hGlobal := GlobalAlloc(GMEM_SHARE or GMEM_ZEROINIT, SizeOf(TDropFiles)+length(FileName)+1);
      if Medium.hGlobal<>0 then begin
        Medium.tymed := TYMED_HGLOBAL;
        dropfiles := GlobalLock(Medium.hGlobal);
        try
          dropfiles^.pfiles := SizeOf(TDropFiles);
          dropfiles^.fwide := False;
          longint(pFile) := longint(dropfiles)+SizeOf(TDropFiles);
          StrPCopy(pFile,FileName);
          Inc(pFile, Length(FileName)+1);
          pFile^ := #0;
        finally
          GlobalUnlock(Medium.hGlobal);
        end;
        Clipboard.SetAsHandle(CF_HDROP,Medium.hGlobal);
      end;
    end;

    参照dragdrop做了一个,还不错.上面这个例子把filename这个文件放在了clipboard上.

  • 相关阅读:
    python学习笔记 async and await
    python学习笔记 异步asyncio
    python学习笔记 协程
    python学记笔记 2 异步IO
    python学习笔记 可变参数关键字参数**kw相关学习
    逆波兰表达式 栈表达式计算
    Codeforces 270E Flawed Flow 网络流问题
    Codeforces 219D Choosing Capital for Treeland 2次DP
    kuangbin 带你飞 概率期望
    函数式编程思想:以函数的方式思考,第3部分
  • 原文地址:https://www.cnblogs.com/MaxWoods/p/1978148.html
Copyright © 2011-2022 走看看