[1000分求解]怎样把剪贴板的所有内容保存到文件和从文件恢复到剪贴板?(200分)

  • 主题发起人 主题发起人 lhc4000
  • 开始时间 开始时间
L

lhc4000

Unregistered / Unconfirmed
GUEST, unregistred user!
请问各路大侠:怎样把剪贴板的所有内容保存到文件和从文件恢复到剪贴板?<br>比如,我用Ctrl+C把网页上的图片和文字一起复制到了剪贴板中,怎样把他们一起保存起来,下次需要时再写入剪贴板中?<br><br>另外的分,有合适答案时开帖酬谢!
 
可以把剪贴板的内容存入一个RTF格式的文件中,然后需要时再复制出来就行了。
 
首先你要写一个剪贴板监视器,然后判断进入剪贴板的是图片还是文字,然后分别保存起来,但是有个问题没办法判断你保存的是网页中的文字还是其他文字,恐怕就要由你自己来筛选了,有关剪贴板监视器的代码在大富翁可以搜到
 
以下是将剪贴板中的RTF格式保存到流中,以及从流中恢复到剪贴板中的代码,我已经用了几年了,可以满足你的要求。<br>{***************************************************************<br>&nbsp;*<br>&nbsp;* 单元名 &nbsp; &nbsp;: UtFunction<br>&nbsp;* 描述 &nbsp; &nbsp; &nbsp;: 通用的函数<br>&nbsp;* 创建者 &nbsp; &nbsp;: zwz_good<br>&nbsp;* 创建日期 &nbsp;: 2005-12-25<br>&nbsp;* 修改者 &nbsp; &nbsp;: zwz_good<br>&nbsp;* 修改日期 &nbsp;: 2006-4-25<br>&nbsp;****************************************************************}<br>{******************************************************************************<br>函 数 名:ClipboardRTFSaveToStream<br>说 &nbsp; &nbsp;明:将剪贴板中的RTF格式内容保存到流中<br>输入参数:mStream: TStream 待保存内容的流媒体<br>输出参数:无<br>返 回 值:成功为True,失败为False<br>全局变量:无<br>数 据 库:<br>调 &nbsp; &nbsp;用:无<br>*******************************************************************************}<br>function ClipboardRTFSaveToStream(var mStream: TStream): boolean;<br><br>{******************************************************************************<br>函 数 名:ClipboardRTFLoadFromStream<br>说 &nbsp; &nbsp;明:将流中的内容以RTF格式复制到剪贴板中<br>输入参数:mStream: TStream 待复制到剪贴板中的流媒体<br>输出参数:无<br>返 回 值:成功为True,失败为False<br>全局变量:无<br>数 据 库:<br>调 &nbsp; &nbsp;用:无<br>*******************************************************************************}<br>function ClipboardRTFLoadFromStream(var mStream: TStream): boolean;<br>const<br>&nbsp; RTFFormatName: array[1..17] of AnsiChar = ('R','i','c','h',' ','T','e','x','t',' ',<br>&nbsp; 'F','o','r','m','a','t', #0);<br>function ClipboardRTFSaveToStream(var mStream: TStream): boolean;<br>var<br>&nbsp; iFormat: cardinal;<br>&nbsp; buffer: array[0..127] of AnsiChar;<br>&nbsp; hGlobal: THANDLE;<br>&nbsp; pGlobal: PAnsiChar;<br>&nbsp; FormatSize: cardinal;<br>begin<br>// &nbsp;'Rich Text Format'<br>&nbsp; Result:= False;<br>&nbsp; mStream.Position:= 0;<br>&nbsp; //OpenClipboard(Application.Handle);<br>&nbsp; Clipboard.Open;<br>&nbsp; try<br>&nbsp; &nbsp; iFormat:= EnumClipboardFormats(0);<br>&nbsp; &nbsp; while iFormat &lt;&gt; 0 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if iFormat &gt; 49151 then &nbsp; //49152--65535之间的值才是自定义剪贴板格式,才可以用 GetClipboardFormatName取得名字<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; zeromemory(@buffer, sizeof(AnsiChar)*128);<br>&nbsp; &nbsp; &nbsp; &nbsp; GetClipboardFormatName(iFormat, @buffer, 128);<br>&nbsp; &nbsp; &nbsp; &nbsp; if StrComp(@RTFFormatName, @buffer) = 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hGlobal:= GetClipboardData(iFormat);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pGlobal:= GlobalLock(hGlobal);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FormatSize:= GlobalSize(hGlobal);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mStream.Write(FormatSize, SizeOf(Cardinal));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mStream.Write(pGlobal^, FormatSize);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GlobalUnLock(hGlobal);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result:= True;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break; //保存RTF完成后,退出循环.<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; iFormat:= EnumclipboardFormats(iFormat);<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; Clipboard.Close;<br>&nbsp; end;<br>end;<br><br>function ClipboardRTFLoadFromStream(var mStream: TStream): boolean;<br>var<br>&nbsp; hGlobal: THANDLE;<br>&nbsp; pGlobal: PAnsiChar;<br>&nbsp; iFormat: cardinal;<br>&nbsp; FormatSize: cardinal;<br>begin<br>&nbsp; Result:= False;<br>&nbsp; iFormat:= RegisterClipboardFormat(@RTFFormatName);<br>&nbsp; mStream.Read(FormatSize, SizeOf(cardinal));<br>&nbsp; hGlobal:= 0;<br>&nbsp; hGlobal:= GlobalAlloc(GHND + GMEM_SHARE, FormatSize);<br>&nbsp; if hGlobal &gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; pGlobal:= GlobalLock(hGlobal);<br>&nbsp; &nbsp; //复制数据<br>&nbsp; &nbsp; mStream.Read(pGlobal^, FormatSize);<br>&nbsp; &nbsp; GlobalUnlock(hGlobal);<br>&nbsp; &nbsp; //打开剪贴板将内存句柄交给剪贴板<br>&nbsp; &nbsp; Clipboard.Open;<br>&nbsp; &nbsp; EmptyClipboard();<br>&nbsp; &nbsp; SetClipboardData(iFormat, hGlobal);<br>&nbsp; &nbsp; Clipboard.Close;<br>&nbsp; &nbsp; Result:= True;<br>&nbsp; end;<br>end;
 
后退
顶部