Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <shlwapi.h>
- #include <iostream>
- #include <conio.h>
- #include <stdio.h>
- using namespace std;
- bool setClipboard2(wstring textToclipboard)
- {
- if (OpenClipboard(NULL)){
- EmptyClipboard();
- HGLOBAL hClipboardData;
- size_t size = (textToclipboard.length()+1) * sizeof(WCHAR);
- hClipboardData = GlobalAlloc(NULL, size);
- WCHAR* pchData = (WCHAR*)GlobalLock(hClipboardData);
- memcpy(pchData, textToclipboard.c_str(), size);
- SetClipboardData(CF_UNICODETEXT, hClipboardData);
- GlobalUnlock(hClipboardData);
- CloseClipboard();
- return true;
- }
- return false;
- }
- bool setClipboard(LPCWSTR lpszWide){
- int nUtf8Size = ::WideCharToMultiByte(CP_UTF8, 0, lpszWide, -1, NULL, 0, NULL, NULL);
- if (nUtf8Size < 1) return false;
- const int nDescLen = 105;
- HGLOBAL hGlobal = ::GlobalAlloc(GMEM_MOVEABLE, nDescLen + nUtf8Size);
- if (NULL != hGlobal)
- {
- bool bErr = false;
- LPSTR lpszBuf = static_cast<LPSTR>(::GlobalLock(hGlobal));
- LPSTR lpszUtf8 = lpszBuf + nDescLen;
- if (::WideCharToMultiByte(CP_UTF8, 0, lpszWide, -1, lpszUtf8, nUtf8Size, NULL, NULL) <= 0)
- {
- bErr = true;
- }
- else
- {
- LPCSTR lpszStartFrag = strstr(lpszUtf8, "<!--StartFragment-->");
- LPCSTR lpszEndFrag = strstr(lpszUtf8, "<!--EndFragment-->");
- lpszStartFrag += strlen("<!--StartFragment-->") + 2;
- int i = _snprintf(
- lpszBuf, nDescLen,
- "Version:1.0\r\nStartHTML:%010d\r\nEndHTML:%010d\r\nStartFragment:%010d\r\nEndFragment:%010d\r\n",
- nDescLen,
- nDescLen + nUtf8Size - 1, // offset to next char behind string
- nDescLen + static_cast<int>(lpszStartFrag - lpszUtf8),
- nDescLen + static_cast<int>(lpszEndFrag - lpszUtf8));
- }
- ::GlobalUnlock(hGlobal);
- if (bErr)
- {
- ::GlobalFree(hGlobal);
- hGlobal = NULL;
- }
- // Get clipboard id for HTML format...
- static int cfid = 0;
- cfid = RegisterClipboardFormat("HTML Format");
- // Open the clipboard...
- if(OpenClipboard(0)) {
- EmptyClipboard();
- HGLOBAL hText = GlobalAlloc(GMEM_MOVEABLE |GMEM_DDESHARE, strlen(lpszBuf)+4);
- char *ptr = (char *)GlobalLock(hText);
- strcpy(ptr, lpszBuf);
- GlobalUnlock(hText);
- ::SetClipboardData(cfid, hText);
- CloseClipboard();
- GlobalFree(hText);
- }
- }
- return NULL != hGlobal;
- }
- int main (int argc, char * argv[])
- {
- wstring s = L"<!--StartFragment--> <HTML> <head><meta http-equiv='Content-type' content='text/html;charset=UTF-8'></head> <body>";
- s += L"தமிழ் திராவிட <a href='index.html'>மொழிக் குடும்பத்தின்</a> முதன்மையான மொழி Hello World";//Written in Tamil Language en.wikipedia.org/wiki/Tamil_language
- s += L"</body></HTML><!--EndFragment--> ";
- LPCWSTR a = TEXT( s.c_str() );
- setClipboard(a);
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement