【功能】
查看当前剪切板中有哪些格式的剪切板内容(显示出格式编号,名称以及内容的字节数),输入要保存的剪切板格式编号和文件名即可导出到文件。
【代码】
// 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文件,内容如下: