Created
October 19, 2016 17:12
-
-
Save gyakoo/04da0231c632898bae5d4c966c0c4d3e to your computer and use it in GitHub Desktop.
Simple snippet to recall how you copy/paste *text* to/from clipboard in c++ win32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ------- COPY | |
{ | |
char some[256]; // your text here, zero ended | |
OpenClipboard(GetDesktopWindow()); | |
EmptyClipboard(); | |
HGLOBAL hg=GlobalAlloc(GMEM_MOVEABLE,ARRAYSIZE(some)+1); | |
if (!hg) | |
{ | |
CloseClipboard(); | |
return; | |
} | |
memcpy(GlobalLock(hg),some, ARRAYSIZE(some)+1); | |
GlobalUnlock(hg); | |
SetClipboardData(CF_TEXT,hg); | |
CloseClipboard(); | |
GlobalFree(hg); | |
#endif | |
} | |
// ------ PASTE | |
{ | |
if ( !IsClipboardFormatAvailable(CF_TEXT)) | |
return; | |
if ( !OpenClipboard(GetDesktopWindow()) ) | |
return; | |
HGLOBAL hg=GetClipboardData(CF_TEXT); | |
if (hg) | |
{ | |
LPCSTR strData =(LPCSTR)GlobalLock(hg); | |
if ( strData ) | |
{ | |
// Your TXT in StrData | |
GlobalUnlock(hg); | |
} | |
} | |
CloseClipboard(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment