查看當前剪切板中有哪些格式的剪切板內容(顯示出格式編號,名稱以及內容的字節數),輸入要保存的剪切板格式編號和文件名即可導出到文件。
【代碼】
// ClipboardExporter.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <Windows.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
UINT format = 0;
TCHAR name[50];
char filename[MAX_PATH];
HGLOBAL hMem;
size_t size;
OpenClipboard(NULL);
while (format = EnumClipboardFormats(format))
{
GetClipboardFormatName(format, name, sizeof(name) / sizeof(TCHAR));
cout << '[' << format << ']';
wcout << name;
hMem = GetClipboardData(format);
size = GlobalSize(hMem);
cout << ", " << size << " bytes";
cout << endl;
}
cout << endl << "Which one would you like to save as a file?" << endl;
cin >> format;
if (IsClipboardFormatAvailable(format))
{
cout << "Input the file name:" << endl;
cin >> filename;
ofstream file(filename, ios::binary);
hMem = GetClipboardData(format);
size = GlobalSize(hMem);
char *data = (char *)GlobalLock(hMem);
file.write(data, size);
GlobalUnlock(hMem);
file.close();
cout << "File saved, " << size << " bytes written." << endl;
}
else
cout << "File not saved." << endl;
CloseClipboard();
system("pause");
return 0;
}
【運行效果】

從中可以看到剪切板中有RTF格式,其編號為49311。於是輸入49311,並輸入文件名,擴展名為rtf。保存後,可用Word或者寫字板打開查看剪切板中的RTF格式的完整內容。
打開保存的rtf文件,內容如下:
