【代码】 【编译前,请将工程调整为使用多字节字符集】 #include <tchar.h> #include <string>
struct Information { int id; char name[50]; char content[200]; bool replied; int replies_num; };
void cut(HWND hWnd) { TCHAR str[] = TEXT("information"); HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, sizeof(str)); LPTSTR lptStr = (LPTSTR)GlobalLock(hMem); memcpy(lptStr, str, sizeof(str)); GlobalUnlock(hMem);
HBITMAP hBitmap = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));
Information info; info.id = 49; _tcscpy_s(info.name, TEXT("Tony")); _tcscpy_s(info.content, TEXT("对于不同的程序,这些格式的规定是不同的,因此这些格式只针对某一具体程序的不同实例才有意义。")); info.replied = true; info.replies_num = 12306;
HGLOBAL hMem2 = GlobalAlloc(GMEM_MOVEABLE, sizeof(info)); Information *pInfo = (Information *)GlobalLock(hMem2); memcpy(pInfo, &info, sizeof(info)); GlobalUnlock(hMem2); UINT myformat = RegisterClipboardFormat("Information Struct Format");
OpenClipboard(hWnd); EmptyClipboard(); SetClipboardData(CF_TEXT, hMem); SetClipboardData(CF_BITMAP, hBitmap); SetClipboardData(myformat, hMem2); CloseClipboard(); }
void paste(HWND hWnd) { if (!IsClipboardFormatAvailable(CF_TEXT)) { MessageBox(hWnd, TEXT("剪切板中没有文本内容!"), TEXT("剪切板"), MB_ICONWARNING); return; } OpenClipboard(hWnd); HGLOBAL hMem = GetClipboardData(CF_TEXT); if (hMem) { LPTSTR str = (LPTSTR)GlobalLock(hMem); if (str) { MessageBox(hWnd, str, TEXT("剪切板内容"), MB_ICONWARNING); GlobalUnlock(hMem); } }
UINT myformat = RegisterClipboardFormat("Information Struct Format"); // 只要格式名称相同,返回的UINT值也相同 if (IsClipboardFormatAvailable(myformat)) { hMem = GetClipboardData(myformat); if (hMem) { Information *info = (Information *)GlobalLock(hMem); if (info) { TCHAR tstr[500]; _stprintf_s(tstr, TEXT("编号: %d\r\n名称: %s\r\n内容: %s"), info->id, info->name, info->content); if (info->replied) { TCHAR subtstr[30]; _stprintf_s(subtstr, TEXT("\r\n已回复,回复数: %d"), info->replies_num); _tcscat_s(tstr, subtstr); } MessageBox(hWnd, tstr, TEXT("自定义剪切板格式的内容"), MB_ICONWARNING); GlobalUnlock(hMem); } } }
CloseClipboard(); }
|