http://blog.csdn.net/youthon/article/details/7327776

 

delphi备份恢复剪切板                   

        分类:            Delphi编程

看了季世平老兄的C++代码后翻译过来的

 

  1. unit clipbak; 
  2.  
  3. interface 
  4. uses 
  5.   SysUtils, Classes, Clipbrd, Windows, Contnrs; 
  6.  
  7. type 
  8.   TClipData = class 
  9.     Format: WORD; 
  10.     Data: THANDLE; 
  11.   end; 
  12.  
  13.   TClipBackuper = class 
  14.   private 
  15.     ClipList: TList; 
  16.   public 
  17.     constructor Create; 
  18.     destructor Destroy; override; 
  19.     procedure BackupClip(); 
  20.     procedure RestoreClip(); 
  21.   end; 
  22.  
  23.  
  24. implementation 
  25.  
  26.  
  27. constructor TClipBackuper.Create; 
  28. begin 
  29.   Cliplist := TObjectList.Create; 
  30. end; 
  31.  
  32. destructor TClipBackuper.Destroy; 
  33. begin 
  34.   ClipList.Free; 
  35. end;    
  36.  
  37. procedure TClipBackuper.BackupClip; 
  38. var 
  39.   i: Integer; 
  40.   Data: THANDLE; 
  41.   pcd: TClipData; 
  42.   DataMem: Pointer; 
  43. begin 
  44.   ClipList.Clear(); 
  45.   Clipboard.Open(); 
  46.   try 
  47.     for i := 0 to Clipboard().FormatCount - 1 do 
  48.     begin 
  49.       pcd := TClipData.Create; 
  50.  
  51.       pcd.Format := Clipboard().Formats[i]; 
  52. Data := THANDLE(Clipboard().GetAsHandle( pcd.Format)); 
  53.       if (Data = 0) then 
  54.       begin 
  55.         pcd.free; 
  56.         continue; 
  57.       end; 
  58.        
  59.       pcd.Data := GlobalAlloc(GMEM_MOVEABLE or GMEM_DDESHARE, GlobalSize(Data)); 
  60.       DataMem := GlobalLock(pcd.Data); 
  61.       if (DataMem <> nil) then 
  62.       begin 
  63.         CopyMemory(DataMem, GlobalLock(Data), GlobalSize(Data)); 
  64.         GlobalUnlock(THANDLE(pcd.Data)); 
  65.         GlobalUnlock(THANDLE(Data)); 
  66.       end; 
  67.       ClipList.Add(pcd); 
  68.     end; 
  69.   finally 
  70.     Clipboard().Close(); 
  71.   end;    
  72. end; 
  73.  
  74. procedure TClipBackuper.RestoreClip; 
  75. var 
  76.   pcd: TClipData; 
  77.   i: Integer; 
  78. begin 
  79.   Clipboard.Open(); 
  80.   try 
  81.     Clipboard.Clear(); 
  82.     for i := 0 to ClipList.Count - 1 do 
  83.     begin 
  84.       pcd := TClipData(ClipList[i]); 
  85.       SetClipboardData(pcd.Format, pcd.Data); 
  86.     end; 
  87.     ClipList.Clear(); 
  88.   finally 
  89.     Clipboard().Close(); 
  90.   end;    
  91. end; 
  92.  
  93. end.