Ce code est censé envoyer une chaîne au presse-papiers. Cependant, je l'ai eu à travailler une fois. Maintenant, il ne vient pas correctement quand je CTRL + V. Mais lorsque j'utilise cet extrait pour identifier le texte du presse-papiers, il montre ce qu'il devrait être.API Windows C/C++ envoyant du texte au presse-papier
#include <windows.h>
#include <iostream>
BOOL SetClipboardText(LPCTSTR pszText)
{
BOOL ok = FALSE;
if(OpenClipboard(NULL)) {
// the text should be placed in "global" memory
HGLOBAL hMem = GlobalAlloc(GMEM_SHARE | GMEM_MOVEABLE,
(lstrlen(pszText)+1)*sizeof(pszText[0]));
LPTSTR ptxt = (LPTSTR)GlobalLock(hMem);
lstrcpy(ptxt, pszText);
GlobalUnlock(hMem);
// set data in clipboard; we are no longer responsible for hMem
ok = (BOOL)SetClipboardData(CF_TEXT, hMem);
CloseClipboard(); // relinquish it for other windows
}
return ok;
}
int main()
{
LPCTSTR test = "DOG";
SetClipboardText(test);
return 0;
}
//get clipboard text
#include <windows.h>
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
HANDLE clip;
if (OpenClipboard(NULL))
clip = GetClipboardData(CF_TEXT);
printf("%s",clip);
//cout << (char*)clip; // HANDLE==void*, so cast it
cin.get();}