應用程式常需根據剪貼板內容動態啟用/禁用「貼上」功能,或針對特定格式(如 Excel 資料、RTF 富文本、自訂格式)進行處理。本文介紹如何枚舉剪貼板上所有可用格式(含標準與自訂格式),並提供 C++ Builder 完整實現代碼。
OpenClipboard / CloseClipboard - 開啟/關閉剪貼板存取EnumClipboardFormats - 枚舉格式:首次傳 0 獲取第一個格式,後續傳上一格式 ID 獲取下一個,返回 0 表示結束GetClipboardFormatName - 獲取自訂格式的註冊名稱| 常數 | 數值 | 說明 |
|---|---|---|
| CF_TEXT | 1 | ANSI 純文本 |
| CF_BITMAP | 2 | 位圖句柄 |
| CF_METAFILEPICT | 3 | 繪圖元檔 |
| CF_SYLK | 4 | Symbolic Link 格式 |
| CF_DIF | 5 | Data Interchange Format |
| CF_TIFF | 6 | TIFF 圖片 |
| CF_OEMTEXT | 7 | OEM 字符集文本 |
| CF_DIB | 8 | 裝置無關位圖 |
| CF_PALETTE | 9 | 調色盤 |
| CF_PENDATA | 10 | 筆輸入資料 |
| CF_RIFF | 11 | RIFF 音頻 |
| CF_WAVE | 12 | WAVE 音頻 |
| CF_UNICODETEXT | 13 | Unicode 文本 (推薦) |
| CF_ENHMETAFILE | 14 | 增強型繪圖元檔 |
| CF_HDROP | 15 | 檔案拖放清單 |
| CF_LOCALE | 16 | 地區設定資訊 |
| CF_MAX | 17 | 預留最大值 |
#ifndef Unit1H
#define Unit1H
#include
#include
#include
#include
class TForm1 : public TForm {
__published:
TListBox *ListBox1;
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private:
String GetUnknownFormatName(UINT uFormatID);
public:
__fastcall TForm1(TComponent* Owner);
};
extern PACKAGE TForm1 *Form1;
#endif
#include
#pragma hdrstop
#include "Unit1.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
// 已知標準格式名稱映射表 (索引對應格式 ID)
const char* StdFormatNames[18] = {
"", "CF_TEXT", "CF_BITMAP", "CF_METAFILEPICT", "CF_SYLK",
"CF_DIF", "CF_TIFF", "CF_OEMTEXT", "CF_DIB", "CF_PALETTE",
"CF_PENDATA", "CF_RIFF", "CF_WAVE", "CF_UNICODETEXT",
"CF_ENHMETAFILE", "CF_HDROP", "CF_LOCALE", "CF_MAX"
};
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {}
void __fastcall TForm1::Button1Click(TObject *Sender) {
ListBox1->Items->Clear();
if (OpenClipboard(Handle)) {
try {
UINT uFormat = EnumClipboardFormats(0);
while (uFormat != 0) {
if (uFormat < 18)
ListBox1->Items->Add(StdFormatNames[uFormat]);
else
ListBox1->Items->Add(GetUnknownFormatName(uFormat));
uFormat = EnumClipboardFormats(uFormat);
}
}
__finally {
CloseClipboard();
}
} else {
ShowMessage("無法開啟剪貼板,可能被其他程式佔用");
}
}
String TForm1::GetUnknownFormatName(UINT uFormatID) {
char szBuf[256];
int len = GetClipboardFormatName(uFormatID, szBuf, sizeof(szBuf));
return (len > 0) ? String(szBuf) : "Unknown Format (" + IntToStr(uFormatID) + ")";
}
bool IsFormatAvailable(UINT uFormat) {
if (!OpenClipboard(0)) return false;
bool found = false;
UINT fmt = EnumClipboardFormats(0);
while (fmt) {
if (fmt == uFormat) { found = true; break; }
fmt = EnumClipboardFormats(fmt);
}
CloseClipboard();
return found;
}
// 使用範例:檢查是否有 Excel 資料 (CF_CSV 或自訂格式)
if (IsFormatAvailable(CF_UNICODETEXT) || IsFormatAvailable(RegisterClipboardFormat("XML Spreadsheet"))) {
// 啟用「貼上 Excel 資料」選單項
}
UINT uMyCustomFormat = RegisterClipboardFormat("MyApp_CustomData_v1");
// 之後可用此 ID 檢測或讀寫自訂格式資料
template
bool SafeClipboardOp(T operation) {
if (!OpenClipboard(0)) return false;
try {
return operation();
} __finally {
CloseClipboard();
}
}
CF_RTF 或 Rich Text Format 出現CF_CSV、XML Spreadsheet、HTML Format 等CF_BITMAP、CF_DIB、PNG 等CF_HDROPRegisterClipboardFormat 註冊