How to set HTML Unicode text to clipboard in VC++?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am a newbie to C++. I want to get the content of the clipboard, which might contain Unicode chars, append a div tag with some content formatted in HTML and set that back to clipboard.
I have achieved successfully in getting the content and appending it. But could not set it back to the clipboard asan HTML text. I have achieved settingas simple text. Here is my code:#include <shlwapi.h>
#include <iostream>
#include <conio.h>
#include <stdio.h>

using namespace std;

wstring getClipboard(){
if (OpenClipboard(NULL)){
HANDLE clip = GetClipboardData(CF_UNICODETEXT);
WCHAR * c;
c = (WCHAR *)clip;
CloseClipboard();
return (WCHAR *)clip;
}
return L"";
}

bool setClipboard(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;
}

int main (int argc, char * argv[])
{
wstring s = getClipboard();
s += std::wstring(L"some extra text <b>hello</b>");
setClipboard(s);
getch();
return 0;
}

I did try using the code described here (http://support.microsoft.com/kb/274308). But I couldnt make it work. What I tried could be way off track or completely wrong. This is what I tried:
bool CopyHTML2(wchar_t *html ){

// Create temporary buffer for HTML header...
wchar_t *buf = new wchar_t [400 + wcslen(html)];
if(!buf) return false;

static int cfid = 0;
if(!cfid) cfid = RegisterClipboardFormat("HTML Format");


// Create a template string for the HTML header...
wcscpy(buf,
L"Version:0.9rn"
L"StartHTML:00000000rn"
L"EndHTML:00000000rn"
L"StartFragment:00000000rn"
L"EndFragment:00000000rn"
L"<html><body>rn"
L"<!--StartFragment -->rn");

// Append the HTML...
wcscpy (buf, html);
wcscpy (buf, L"rn");
// Finish up the HTML format...
wcscpy (buf,
L"<!--EndFragment-->rn"
L"</body>rn"
L"</html>");

wchar_t *ptr = wcsstr(buf, L"StartHTML");
wsprintf(ptr+10, "%08u", wcsstr(buf, L"<html>") - buf);
*(ptr+10+8) = r;

ptr = wcsstr(buf, L"EndHTML");
wsprintf(ptr+8, "%08u", wcslen(buf));
*(ptr+8+8) = r;

ptr = wcsstr(buf, L"StartFragment");
wsprintf(ptr+14, "%08u", wcsstr(buf, L"<!--StartFrag") - buf);
*(ptr+14+8) = r;

ptr = wcsstr(buf, L"EndFragment");
wsprintf(ptr+12, "%08u", wcsstr(buf, L"<!--EndFrag") - buf);
*(ptr+12+8) = r;


// Open the clipboard...
if(OpenClipboard(0)) {
EmptyClipboard();
HGLOBAL hText = GlobalAlloc(GMEM_MOVEABLE |GMEM_DDESHARE, wcslen(buf)+4);
wchar_t *ptr = (wchar_t *)GlobalLock(hText);
wcscpy(ptr, buf);
GlobalUnlock(hText);
SetClipboardData(cfid, hText);
CloseClipboard();
GlobalFree(hText);
}

// Clean up...
delete [] buf;
return true;
}


But I get error at wsprintf. Please guide me on how to proceed. I am using Visual Studio Express 2012 on Windows 8. Thanks.

View the full article
 
Back
Top