C++ 複製內容到剪貼簿 [寬字元處理方法]
如果需要擷取剪貼簿內容請參考這一篇:
http://charlottehong.blogspot.com/2017/10/c_24.html
http://charlottehong.blogspot.com/2017/10/c_24.html
一樣直接上完成的函式庫
#include <windows.h>
#include <atlstr.h>
// 設定剪貼簿
void SetClipboardStr(const CStringW& StrW) {
// 參考:https://goo.gl/XYppCD
// 轉寬char
const CStringA& strA = static_cast<const CStringA>(StrW);
const char* clipText = static_cast<LPCSTR>(strA);
wchar_t* wString = new wchar_t[strlen(clipText)+1];
MultiByteToWideChar(CP_ACP, 0, clipText, -1, wString, strlen(clipText));
wString[strlen(clipText)] = '\0';
LPWSTR cwdBuffer = wString;
// Get the current working directory:
//if( (cwdBuffer = _wgetcwd( NULL, 0 )) == NULL ) return 1;
DWORD len = wcslen(cwdBuffer);
HGLOBAL hdst;
LPWSTR dst;
// Allocate string for cwd
hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
dst = (LPWSTR)GlobalLock(hdst);
memcpy(dst, cwdBuffer, len * sizeof(WCHAR));
dst[len] = 0;
GlobalUnlock(hdst);
// Set clipboard data
if (!OpenClipboard(NULL)) return;
EmptyClipboard();
if (!SetClipboardData(CF_UNICODETEXT, hdst)) return;
CloseClipboard();
//free(cwdBuffer);
delete[] wString;
}