【笨嘴拙舌WINDOWS】实践检验之剪切板查看器【Delphi】

该程序能够监视Windows剪切板的内容(文字和图片)

其思路是

先调用SetClipBoardViewer(Self.Handle),让Windows剪切板内容发生改变之后,通知本程序;

然后截获WM_DRAWCLIPBOARD消息来查看剪切板里面的内容;

最后关闭程序时调用changeclipboardChain(handle,hwndNextViewer)注销监视剪切板

再处理WM_CHANGECBCHAIN消息来真正撤销

主要代码如下:

    

procedure TForm1.FormCreate(Sender: TObject);
begin
  hwndNextViewer:= SetClipBoardViewer(Self.Handle);
end;


procedure TForm1.DrawclipBoard(var message: Tmessage);
begin
  lbl1.Caption := '' ;
  if (hwndNextViewer<>0) then
    SendMessage(hwndNextViewer,message.Msg,message.WParam,message.LParam);
  PaintClip;
end;



procedure TForm1.FormDestroy(Sender: TObject);
begin
  changeclipboardChain(handle,hwndNextViewer);
end;


procedure TForm1.ChangecbChain(var message: Tmessage);
begin
  if (message.WParam=hwndNextViewer) then
     hwndNextViewer := message.LParam
  else if(hwndNextViewer<>0) then
    SendMessage(hwndNextViewer,message.Msg,message.WParam,message.LParam);
end;


procedure TForm1.PaintClip;
Var
  htext,hBitmap:HGLOBAL;
  str:String;
  pch:Pointer;
  len:Integer;
begin
  openclipboard(Self.Handle);
  htext := GetClipboardData(CF_TEXT);
  hBitmap := GetClipboardData(CF_BITMAP);
  if htext<>0 then
  begin
    pch:=globallock(htext);
    len:= globalSize(htext);
    SetString(str,Pchar(pch),len);
    lbl1.Caption := lbl1.Caption  + str;
    globalunlock(htext);
  end;
  if hBitmap<>0 then
  begin
    img1.Picture.Bitmap.Handle :=  hBitmap;
  end;
  CloseClipBoard;
end;

 

点击这里下载程序源码

 

posted @ 2013-09-16 15:21  Pavkoo  阅读(369)  评论(0编辑  收藏  举报