Archived

This topic is now archived and is closed to further replies.

Prozak

How to send string to the clipboard

Recommended Posts

Prozak    898
Hi all, how do I send a string to the clipboard? This cannot be terrifically dificult... Season's Greetings, Happy New Year, Jester Says: "Visit Positronic Dreams"
[Hugo Ferreira][Positronic Dreams][Colibri 3D Engine][Entropy HL2 MOD]
[Yann L.][Enginuity] [Penny Arcade] [MSDN][VS RoadMap][Humus][BSPs][UGP][NeHe]
The most irrefutable evidence that there is intelligent life in the Universe is that they haven't contacted us!
[edited by - pentium3id on January 29, 2004 1:22:21 AM]

Share this post


Link to post
Share on other sites
Zeblar Nagrim    150
Easy-pleasy,

  

char* g_pText;
HGLOBAL g_hGlobal;
HWND g_hWnd;

void CopyText()
{
//

// Prepare

g_hGlobal = GlobalAlloc(GHND | GMEM_SHARE, (lstrlen (g_pText) + 1) * sizeof (char));
g_hGlobal = GlobalLock(g_hGlobal);
lstrcpy(g_hGlobal, g_pText);
GlobalUnlock(g_hGlobal);

//

// Send

OpenClipboard(g_hWnd);
EmptyClipboard();
SetClipboardData(CF_TCHAR, g_hGlobal);
CloseClipboard();
}



HNY,



Zeblar Nagrim, Lord of Chaos

Share this post


Link to post
Share on other sites
Evil Steve    2017
Another, error checked method:

bool CopyTextToClipboard(const string& strBuff, HWND hWndOwner)
{
HGLOBAL hMem;
void* pData;

// Open Clipboard //

if(!OpenClipboard(hWndOwner))
return false;
EmptyClipboard();

// Allocate memory //

hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,strBuff.length()+1);
if(!hMem)
{
CloseClipboard();
return false;
}

// Fill Memory //

pData = GlobalLock(hMem);
if(!pData)
{
GlobalFree(hMem);
CloseClipboard();
return false;
}
memcpy(pData,strBuff.c_str(),strBuff.length()+1);

// Unlock memory, set clipboard data, and cleanup //

GlobalUnlock(hMem);
SetClipboardData(CF_TEXT,hMem);
CloseClipboard();
return true;
}

Share this post


Link to post
Share on other sites