看過別人的方法,將各種數據分類讀取到數據,占用剪貼板完成後再分類保存。但這種方法存在問題。因為C#的剪貼板讀取和保存數據類型是有限的,如果對於一些用戶自定義書記的剪貼板或者其他程序的自定義數據。操作後是不能如實恢復剪貼板的。
別人的例子程序。
object xx = null;
string ctype = "";
try
{
if (Clipboard.ContainsText())
{
ctype = "Text";
xx = Clipboard.GetText();
}
else if (Clipboard.ContainsFileDropList())
{
ctype = "FileDrop";
xx = Clipboard.GetFileDropList();
}
。。。。。。。。。。。。。。。
想要達到的效果。
data=clipboard.get....
占用剪貼板。。。。。
clipboard.set.....(data);
前後data數據不會改變。請問該如何實現
在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();
}