4 * @brief ClipBoard helper functions implementations.
11 inline CLIPFORMAT GetClipTcharTextFormat() { return (sizeof(tchar_t) == 1 ? CF_TEXT : CF_UNICODETEXT); }
14 * @brief Copies string to clipboard.
15 * @param [in] text Text to copy to clipboard.
16 * @param [in] currentWindowHandle Handle to current window.
17 * @return `true` if text copying succeeds, `false` otherwise.
20 bool PutToClipboard<HWND>(const String & text, HWND currentWindowHandle)
26 if (OpenClipboard(currentWindowHandle))
29 const size_t dataSiz = text.length() + 1;
30 HGLOBAL hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, dataSiz * sizeof(tchar_t));
33 if (tchar_t* pszData = static_cast<tchar_t*>(::GlobalLock(hData)))
35 tc::tcslcpy(pszData, dataSiz, text.c_str());
38 CLIPFORMAT fmt = GetClipTcharTextFormat();
39 bOK = SetClipboardData(fmt, hData) != nullptr;
47 * @brief Retrieves the string from clipboard.
48 * @param [out] text Text copied from clipboard.
49 * @param [in] currentWindowHandle Handle to current window.
50 * @return `true` if retrieving the clipboard text succeeds, `false` otherwise.
52 bool GetFromClipboard(String & text)
54 bool bSuccess = false;
55 if (OpenClipboard(nullptr))
57 CLIPFORMAT fmt = GetClipTcharTextFormat();
58 HGLOBAL hData = GetClipboardData(fmt);
61 tchar_t* pszData = (tchar_t*) GlobalLock(hData);
62 if (pszData != nullptr)
75 void PutFilesToClipboardInternal<HWND>(const String& strPaths, const String& strPathsSepSpc, HWND currentWindowHandle)
78 HGLOBAL hDrop = GlobalAlloc(GHND, sizeof(DROPFILES) + sizeof(tchar_t) * strPaths.length());
81 if (tchar_t* pDrop = static_cast<tchar_t*>(GlobalLock(hDrop)))
84 df.pFiles = sizeof(DROPFILES);
85 df.fWide = (sizeof(tchar_t) > 1);
86 memcpy(pDrop, &df, sizeof(DROPFILES));
87 memcpy((BYTE*)pDrop + sizeof(DROPFILES), (const tchar_t*)strPaths.c_str(), sizeof(tchar_t) * strPaths.length());
92 HGLOBAL hDropEffect = GlobalAlloc(GHND, sizeof(DWORD));
93 if (hDropEffect == nullptr)
98 if (DWORD* p = static_cast<DWORD*>(GlobalLock(hDropEffect)))
100 *p = DROPEFFECT_COPY;
101 GlobalUnlock(hDropEffect);
105 HGLOBAL hPathnames = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, sizeof(tchar_t) * (strPathsSepSpc.length() + 1));
106 if (hPathnames == nullptr)
109 GlobalFree(hDropEffect);
112 if (void* pPathnames = GlobalLock(hPathnames))
114 memcpy((BYTE*)pPathnames, (const tchar_t*)strPathsSepSpc.c_str(), sizeof(tchar_t) * strPathsSepSpc.length());
115 ((tchar_t*)pPathnames)[strPathsSepSpc.length()] = 0;
116 GlobalUnlock(hPathnames);
119 UINT CF_DROPEFFECT = RegisterClipboardFormat(CFSTR_PREFERREDDROPEFFECT);
120 if (::OpenClipboard(currentWindowHandle))
123 SetClipboardData(CF_HDROP, hDrop);
124 SetClipboardData(CF_DROPEFFECT, hDropEffect);
125 SetClipboardData(GetClipTcharTextFormat(), hPathnames);