Jump to content
  • Advertisement

Archived

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

How to send string to the clipboard

This topic is 5192 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!