在c#中试了一下,有些问题,建议你参考这个c++builder写的代码
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <clipbrd.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
TList* ClipList = new TList;
struct ClipData
{
WORD Format;
HANDLE Data;
ClipData() { Format = 0; Data = NULL; }
~ClipData() { if (Data) GlobalFree(Data); }
};
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
for(int n=0; n<ClipList->Count; n++) {
delete (ClipData*)ClipList->Items[n];
}
ClipList->Clear(); delete ClipList;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SaveClipBtnClick(TObject *Sender)
{
HANDLE Data;
ClipData* pcd;
for(int n=0; n<ClipList->Count; n++) {
delete (ClipData*)ClipList->Items[n];
}
ClipList->Clear(); Clipboard()->Open();
for(int n=0; n<Clipboard()->FormatCount;
n++) {
if ((pcd=new ClipData) == NULL)
break;
pcd->Format = Clipboard()->Formats[n];
Data = (HANDLE)Clipboard()->GetAsHandle(
pcd->Format);
if (Data == NULL) {
delete pcd; continue;
}
pcd->Data = GlobalAlloc(GMEM_MOVEABLE|
GMEM_DDESHARE, GlobalSize(Data));
void* DataMem = GlobalLock(pcd->Data);
if (DataMem != NULL) {
memcpy(DataMem, GlobalLock(Data),
GlobalSize(Data));
GlobalUnlock((HANDLE)pcd->Data);
GlobalUnlock((HANDLE)Data);
}
ClipList->Add(pcd); continue;
}
Clipboard()->Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RestoreClipBtnClick(TObject *Sender)
{
HANDLE Data;
ClipData* pcd;
Clipboard()->Open();
Clipboard()->Clear();
for(int n=0; n<ClipList->Count; n++) {
ClipData* pcd = (ClipData*)ClipList
->Items[n];
SetClipboardData(pcd->Format, pcd->
Data);
pcd->Data = NULL; delete pcd;
}
ClipList->Clear(); Clipboard()->Close();
}