Skip to content

Instantly share code, notes, and snippets.

@codec-abc
Created December 11, 2020 23:21
Show Gist options
  • Select an option

  • Save codec-abc/b9c1bc3f8a1c602b499a5856b6a0d1e0 to your computer and use it in GitHub Desktop.

Select an option

Save codec-abc/b9c1bc3f8a1c602b499a5856b6a0d1e0 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <windows.h>
#include <string>
#include <iomanip>
#include <sstream>
int width = 2;
int height = 2;
int32_t bitmap2x2[4] = { 0xffff0000, 0xff00ff00, 0xff0000ff, 0x00000000 };
void WriteBitmapFromHandle(HBITMAP source_hbitmap, UINT uFormat, int width, int height) {
// We would like to just call ::SetClipboardData on the source_hbitmap,
// but that bitmap might not be of a sort we can write to the clipboard.
// For this reason, we create a new bitmap, copy the bits over, and then
// write that to the clipboard.
HDC dc = ::GetDC(nullptr);
HDC compatible_dc = ::CreateCompatibleDC(nullptr);
HDC source_dc = ::CreateCompatibleDC(nullptr);
// This is the HBITMAP we will eventually write to the clipboard
HBITMAP hbitmap = ::CreateCompatibleBitmap(dc, width, height);
if (!hbitmap) {
// Failed to create the bitmap
::DeleteDC(compatible_dc);
::DeleteDC(source_dc);
::ReleaseDC(nullptr, dc);
return;
}
HBITMAP old_hbitmap = (HBITMAP)SelectObject(compatible_dc, hbitmap);
HBITMAP old_source = (HBITMAP)SelectObject(source_dc, source_hbitmap);
// Now we need to blend it into an HBITMAP we can place on the clipboard
BLENDFUNCTION bf = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
::GdiAlphaBlend(compatible_dc,
0,
0,
width,
height,
source_dc,
0,
0,
width,
height,
bf);
// Clean up all the handles we just opened
::SelectObject(compatible_dc, old_hbitmap);
::SelectObject(source_dc, old_source);
::DeleteObject(old_hbitmap);
::DeleteObject(old_source);
::DeleteDC(compatible_dc);
::DeleteDC(source_dc);
::ReleaseDC(nullptr, dc);
EmptyClipboard();
HANDLE data = SetClipboardData(uFormat, hbitmap);
if (data) {
std::cout << "data put on the clipboard" << std::endl;
}
::DeleteObject(source_hbitmap);
}
int main()
{
if (OpenClipboard(NULL))
{
EmptyClipboard();
HDC hdc = CreateCompatibleDC(NULL);
BITMAPV5HEADER header;
header.bV5Size = sizeof(BITMAPV5HEADER);
header.bV5Width = width;
header.bV5Height = -height;
header.bV5Planes = 1;
header.bV5BitCount = 32;
header.bV5Compression = BI_BITFIELDS;
header.bV5SizeImage = 4 * width * height;
header.bV5AlphaMask = 0xff000000;
header.bV5RedMask = 0x00ff0000;
header.bV5GreenMask = 0x0000ff00;
header.bV5BlueMask = 0x000000ff;
header.bV5CSType = LCS_WINDOWS_COLOR_SPACE;
header.bV5Intent = LCS_GM_GRAPHICS;
header.bV5ClrUsed = 0;
header.bV5ClrImportant = 0;
header.bV5ProfileData = 0;
void* bits = NULL;
HBITMAP dib = ::CreateDIBSection(hdc, reinterpret_cast<BITMAPINFO*>(&header),
DIB_RGB_COLORS, &bits, NULL, 0);
memcpy(bits, &bitmap2x2[0], 4 * sizeof(int32_t));
WriteBitmapFromHandle(dib, CF_BITMAP, width, height);
//WriteBitmapFromHandle(dib, CF_DIBV5, width, height);
CloseClipboard();
}
return 0;
}
@codec-abc
Copy link
Copy Markdown
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment