如何获取剪贴板的内容?(100分)

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

liujunyao

Unregistered / Unconfirmed
GUEST, unregistred user!
<br>如何获取剪贴板上的内容?<br>不是用paste.<br>
 
Clipboard.AsText
 
如果是bcb<br><br>#include &lt;clipbrd.hpp&gt;<br><br>Clipboard()-&gt;AsText
 
下面的代码是Delphi帮助中的例子,可用来获取Clipboard中各种类型的数据:<br>var<br>&nbsp; MyHandle: THandle;<br>&nbsp; TextPtr: PChar;<br>&nbsp; MyString: string;<br>begin<br>&nbsp; ClipBoard.Open; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //打开Clipboard<br>try<br>&nbsp; MyHandle := Clipboard.GetAsHandle(CF_TEXT); //可以用CF_BITMAP、CF_PICTURE等等<br>&nbsp; TextPtr := GlobalLock(MyHandle); &nbsp;//获得指向该数据的指针<br>&nbsp; MyString := StrPas(TextPtr); &nbsp; &nbsp; &nbsp;//必须复制数据到自己的变量中<br>&nbsp; GlobalUnlock(MyHandle);<br>finally<br>&nbsp; Clipboard.Close; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//关闭Clipboard<br>end;<br>end;
 
如果只处理文字,那么TEdit之类的编辑组件具有可以直接调用 <br>的方法如PastFromClipboard。例如: <br>procedure TForm1.Button1Click(Sender: TObject); <br>begin <br>&nbsp; RichEdit1.SelectAll; <br>&nbsp; RichEdit1.CopyToClipboard; <br>&nbsp; Edit1.Clear; <br>&nbsp; Edit1.PasteFromClipboard; <br>&nbsp; RichEdit1.SetFocus; <br>end; <br>如果还要处理图形,那么需要用到预定义的系统对象Clipboard。例如: <br>procedure TForm1.PasteButtonClick(Sender: TObject); <br>var <br>&nbsp; Bitmap: TBitmap; <br>&nbsp; begin <br>&nbsp; if Clipboard.HasFormat(CF_BITMAP) then { check to see if there is a pictur <br>e } <br>&nbsp; &nbsp;begin <br>&nbsp; &nbsp; &nbsp;Bitmap := TBitmap.Create; &nbsp;{Create a bitmap to hold the contents of the <br>&nbsp;Clipboard} <br>&nbsp; &nbsp; &nbsp;try <br>&nbsp; &nbsp; &nbsp; &nbsp;Bitmap.Assign(Clipboard); {get the bitmap off the clipboard using Ass <br>ign} <br>&nbsp; &nbsp; &nbsp; &nbsp;Image.Canvas.Draw(0, 0, Bitmap); {copy the bitmap to the Image} <br>&nbsp; &nbsp; &nbsp;finally <br>&nbsp; &nbsp; &nbsp; &nbsp;Bitmap.Free; <br>&nbsp; &nbsp; &nbsp;end; <br>&nbsp; &nbsp;end; <br>end; <br>&nbsp;
 
去看看tClipboard吧.
 
试着拦截这两个消息:<br>&nbsp; &nbsp; procedure WMDrawClipboard (var message : TMessage); message WM_DRAWCLIPBOARD;<br>&nbsp; &nbsp; procedure WMChangeCBCHain (var message : TMessage); message WM_CHANGECBCHAIN;<br>
 
如果实在Delphi中,非常简单!请看以下代码:<br>procedure TForm1.PasteButtonClick(Sender: TObject);<br><br>var<br>&nbsp; Bitmap: TBitmap;<br>begin<br>&nbsp;if Clipboard.HasFormat(CF_BITMAP) then { is there a bitmap on the Clipboard? )<br>&nbsp;begin<br>&nbsp; &nbsp;Image.Picture.Bitmap.Assign(Clipboard);<br>&nbsp;end;<br>end;<br><br>
 
多人接受答案了。
 
什么呀。
 
后退
顶部