Advertisement
jayarathina

C++ Copy to Clipboard

May 5th, 2013
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. #include <shlwapi.h>
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <stdio.h>
  5.  
  6. using namespace std;
  7.  
  8. bool setClipboard2(wstring textToclipboard)
  9. {
  10.     if (OpenClipboard(NULL)){
  11.         EmptyClipboard();
  12.         HGLOBAL hClipboardData;
  13.         size_t size = (textToclipboard.length()+1) * sizeof(WCHAR);
  14.         hClipboardData = GlobalAlloc(NULL, size);
  15.         WCHAR* pchData = (WCHAR*)GlobalLock(hClipboardData);
  16.         memcpy(pchData, textToclipboard.c_str(), size);
  17.         SetClipboardData(CF_UNICODETEXT, hClipboardData);
  18.         GlobalUnlock(hClipboardData);
  19.         CloseClipboard();
  20.         return true;
  21.     }
  22.     return false;
  23. }
  24.  
  25. bool setClipboard(LPCWSTR lpszWide){
  26.     int nUtf8Size = ::WideCharToMultiByte(CP_UTF8, 0, lpszWide, -1, NULL, 0, NULL, NULL);
  27.     if (nUtf8Size < 1) return false;
  28.  
  29.     const int nDescLen = 105;
  30.     HGLOBAL hGlobal = ::GlobalAlloc(GMEM_MOVEABLE, nDescLen + nUtf8Size);
  31.     if (NULL != hGlobal)
  32.     {
  33.         bool bErr = false;
  34.         LPSTR lpszBuf = static_cast<LPSTR>(::GlobalLock(hGlobal));
  35.         LPSTR lpszUtf8 = lpszBuf + nDescLen;
  36.         if (::WideCharToMultiByte(CP_UTF8, 0, lpszWide, -1, lpszUtf8, nUtf8Size, NULL, NULL) <= 0)
  37.         {
  38.             bErr = true;
  39.         }
  40.         else
  41.         {
  42.             LPCSTR lpszStartFrag = strstr(lpszUtf8, "<!--StartFragment-->");
  43.             LPCSTR lpszEndFrag = strstr(lpszUtf8, "<!--EndFragment-->");
  44.             lpszStartFrag += strlen("<!--StartFragment-->") + 2;
  45.  
  46.             int i = _snprintf(
  47.             lpszBuf, nDescLen,
  48.             "Version:1.0\r\nStartHTML:%010d\r\nEndHTML:%010d\r\nStartFragment:%010d\r\nEndFragment:%010d\r\n",
  49.             nDescLen,
  50.             nDescLen + nUtf8Size - 1,       // offset to next char behind string
  51.             nDescLen + static_cast<int>(lpszStartFrag - lpszUtf8),
  52.             nDescLen + static_cast<int>(lpszEndFrag - lpszUtf8));
  53.         }
  54.         ::GlobalUnlock(hGlobal);
  55.         if (bErr)
  56.         {
  57.             ::GlobalFree(hGlobal);
  58.             hGlobal = NULL;
  59.         }
  60.  
  61.         // Get clipboard id for HTML format...
  62.         static int cfid = 0;
  63.         cfid = RegisterClipboardFormat("HTML Format");
  64.         // Open the clipboard...
  65.         if(OpenClipboard(0)) {
  66.             EmptyClipboard();
  67.             HGLOBAL hText = GlobalAlloc(GMEM_MOVEABLE |GMEM_DDESHARE, strlen(lpszBuf)+4);
  68.             char *ptr = (char *)GlobalLock(hText);
  69.             strcpy(ptr, lpszBuf);
  70.             GlobalUnlock(hText);
  71.             ::SetClipboardData(cfid, hText);
  72.             CloseClipboard();
  73.             GlobalFree(hText);
  74.         }
  75.     }
  76.  
  77.     return NULL != hGlobal;
  78. }
  79.  
  80. int main (int argc, char * argv[])
  81. {
  82.     wstring  s = L"<!--StartFragment--> <HTML> <head><meta http-equiv='Content-type' content='text/html;charset=UTF-8'></head> <body>";
  83.     s += L"தமிழ் திராவிட <a href='index.html'>மொழிக் குடும்பத்தின்</a> முதன்மையான மொழி Hello World";//Written in Tamil Language en.wikipedia.org/wiki/Tamil_language‎
  84.     s += L"</body></HTML><!--EndFragment--> ";
  85.     LPCWSTR a = TEXT( s.c_str() );
  86.     setClipboard(a);
  87.     getch();   
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement