I am a new programmer in c++ and I can't understand how to use the clipboard to copy and paste like in any other program with text. Example please?
I am using Code::Blocks 16.01 MinGW32 g++ windows 10.
I am a new programmer in c++ and I can't understand how to use the clipboard to copy and paste like in any other program with text. Example please?
I am using Code::Blocks 16.01 MinGW32 g++ windows 10.
SetClipboardData should handle it.
glob = GlobalAlloc(GMEM_FIXED,32);
memcpy(glob,"it works",9);
OpenClipboard(hWnd);
EmptyClipboard();
SetClipboardData(CF_TEXT,glob);
CloseClipboard();
EDIT
This will get data out of clipboard and return that data in string
.
std::string GetClipboardText()
{
OpenClipboard(nullptr);
HANDLE hData = GetClipboardData(CF_TEXT);
char * pszText = static_cast<char*>( GlobalLock(hData) );
std::string text( pszText );
GlobalUnlock( hData );
CloseClipboard();
return text;
}
For a cross-platform solution, you may use a library like ClipboardXX.
Usage example:
#include "clipboard.hpp"
#include <string>
int main()
{
clipboardxx::clipboard clipboard;
// copy
clipboard << "text you wanna copy";
// paste
std::string paste_text;
clipboard >> paste_text;
}
This may be preferrable for a Windows only software as well due to modern C++ being nicer to work with than the plain old Windows API.
Another useful library might be clip but I didn't test it myself.
Usage example:
#include "clip.h"
#include <iostream>
int main()
{
clip::set_text("Hello World");
std::string value;
clip::get_text(value);
std::cout << value << "\n";
}