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

delphi备份恢复剪切板

        分类:            Delphi编程2012-03-07 10:31578人阅读评论(0)举报

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

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