13bool copy_text_to_clipboard(
const char* clipboard_text)
15 if ( !OpenClipboard(NULL) )
17 std::cout <<
"Cannot open the Clipboard" << std::endl;
21 if( !EmptyClipboard() )
23 std::cout <<
"Cannot empty the Clipboard" << std::endl;
30 HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE,
31 (strlen(clipboard_text)+ 1) *
sizeof(TCHAR));
40 LPTSTR lptstrCopy = (LPTSTR)GlobalLock(hglbCopy);
41 memcpy(lptstrCopy, clipboard_text,
42 strlen(clipboard_text) *
sizeof(TCHAR));
43 lptstrCopy[strlen(clipboard_text)] = (TCHAR) 0;
44 GlobalUnlock(hglbCopy);
47 if ( ::SetClipboardData( CF_TEXT, hglbCopy) == NULL )
49 std::cout <<
"Unable to set Clipboard data" << std::endl;
57bool copy_rgb_image_to_clipboard(
int w,
int h,
const unsigned char* image_buffer)
59 if ( !OpenClipboard(NULL) )
61 std::cout <<
"Cannot open the Clipboard" << std::endl;
65 if( !EmptyClipboard() )
67 std::cout <<
"Cannot empty the Clipboard" << std::endl;
74 unsigned int src_line_length = w*3;
75 unsigned int dst_line_length = src_line_length;
76 if ((dst_line_length&3) != 0)
77 dst_line_length = (dst_line_length/4+1)*4;
78 unsigned int size =
sizeof(BITMAPINFO)+dst_line_length*h;
79 HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, size);
87 PBITMAPINFO pbmi = (PBITMAPINFO)GlobalLock(hglbCopy);
88 PBITMAPINFOHEADER pbhi = &pbmi->bmiHeader;
89 pbhi->biSize =
sizeof(BITMAPINFOHEADER);
93 pbhi->biBitCount = 24;
94 pbhi->biCompression = BI_RGB;
95 pbhi->biSizeImage = 0;
96 pbhi->biXPelsPerMeter = 1000000;
97 pbhi->biYPelsPerMeter = 1000000;
99 pbhi->biClrImportant = 0;
100 unsigned char* dst = (
unsigned char*)&pbmi->bmiColors[0];
101 const unsigned char* src = image_buffer;
102 for (
int i=0; i<h; ++i) {
103 unsigned char* dst_1 = dst;
104 const unsigned char* src_1 = src;
105 for (
int j = 0; j<w; ++j) {
111 dst += dst_line_length;
112 src += src_line_length;
114 GlobalUnlock(hglbCopy);
116 if ( ::SetClipboardData( CF_DIB, hglbCopy) == NULL )
118 std::cout <<
"Unable to set Clipboard data" << std::endl;
126bool get_text_from_clipboard(std::string& text,
bool clear_clipboard)
128 if (!IsClipboardFormatAvailable(CF_TEXT))
131 if (OpenClipboard(
nullptr)) {
133 HANDLE hData = GetClipboardData(CF_TEXT);
134 if (hData !=
nullptr) {
136 char* pszText =
static_cast<char*
>(GlobalLock(hData));
137 if (pszText !=
nullptr) {
139 text = std::string(pszText);
153bool get_rgb_image_from_clipboard(
int& w,
int& h, std::vector<char>& data,
bool clear_clipboard)
155 if (!IsClipboardFormatAvailable(CF_BITMAP))
159 if (OpenClipboard(
nullptr)) {
161 HANDLE hData = GetClipboardData(CF_BITMAP);
162 if (hData !=
nullptr) {
163 HBITMAP hBitmap = (HBITMAP)hData;
166 if (GetObject(hBitmap,
sizeof(BITMAP), (LPSTR)&bmp)) {
168 std::fill((uint8_t*)&bmi, (uint8_t*)(&bmi + 1), 0);
169 bmi.bmiHeader.biSize =
sizeof(BITMAPINFOHEADER);
170 w = bmi.bmiHeader.biWidth = bmp.bmWidth;
171 h = bmi.bmiHeader.biHeight = bmp.bmHeight;
172 bmi.bmiHeader.biPlanes = 1;
173 bmi.bmiHeader.biBitCount = 24;
174 bmi.bmiHeader.biCompression = BI_RGB;
175 data.resize(3 * bmp.bmHeight * bmp.bmWidth);
176 if (GetDIBits(GetDC(0), hBitmap, 0, (WORD)bmp.bmHeight, data.data(), &bmi, DIB_RGB_COLORS)) {
192bool copy_text_to_clipboard(
const char* text) {
197bool copy_rgb_image_to_clipboard(
int w,
int h,
const unsigned char* image_buffer) {
201bool get_text_from_clipboard(std::string& text,
bool clear_clipboard)
208bool get_rgb_image_from_clipboard(
int& w,
int& h, std::vector<char>& data,
bool clear_clipboard)