Skip to content

Instantly share code, notes, and snippets.

@espresso3389
Created March 9, 2016 17:10
Show Gist options
  • Save espresso3389/0addc8dc7ab828e59102 to your computer and use it in GitHub Desktop.
Save espresso3389/0addc8dc7ab828e59102 to your computer and use it in GitHub Desktop.
Easy color picker implementation
#include <stdio.h>
#include <windows.h>
#include <ShellScalingAPI.h>
#pragma comment(lib,"gdi32.lib")
#pragma comment(lib,"user32.lib")
#pragma comment(lib,"shcore.lib")
int main()
{
SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
auto hdc = GetDC(0);
for (;;)
{
Sleep(300);
POINT pt;
GetCursorPos(&pt);
const int w = 1;
const int h = 1;
BYTE* pBits;
BITMAPINFOHEADER bih;
ZeroMemory(&bih, sizeof(bih));
bih.biSize = sizeof(bih);
bih.biWidth = w;
bih.biHeight = -h;
bih.biPlanes = 1;
bih.biBitCount = 24;
bih.biCompression = BI_RGB;
auto bmp = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS, (void**)&pBits, NULL, 0);
auto hdcMem = CreateCompatibleDC(hdc);
auto oldBmp = SelectObject(hdcMem, bmp);
BitBlt(hdcMem, 0, 0, w, h, hdc, pt.x, pt.y, SRCCOPY);
SelectObject(hdcMem, oldBmp);
DeleteDC(hdcMem);
printf("(%d,%d): #%02X%02X%02X\n",
pt.x, pt.y, pBits[2], pBits[1], pBits[0]);
}
ReleaseDC(0, hdc);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment