Author Topic: How can I get the color from under my cursor with c++?  (Read 150 times)

Offline Malachi

  • 1
  • Newbie
  • *
  • Posts: 0
  • Karma: +0/-0
    • View Profile
How can I get the color from under my cursor with c++?
« on: November 17, 2016, 07:04:54 PM »
Hello, I have a question that puzzles me, how can i get the color from under my cursor with c++? Thank you please kindly answer me !!! Cordially.
« Last Edit: April 30, 2017, 09:56:11 AM by Sofyene »



Offline Zachariah

  • 1
  • Newbie
  • *
  • Posts: 0
  • Karma: +0/-0
    • View Profile
Re: The color from under my cursor
« Reply #1 on: April 30, 2017, 09:57:39 AM »
Off the top of my head, the straightforward way:

#include <stdio.h>
#include <Windows.h>

int main(void) {
    POINT p;
    COLORREF color;
    HDC hDC;
    BOOL b;

    // Get the device context for the screen
    hDC = GetDC(NULL);
    if (hDC == NULL)
        return 3;

    // Get the current cursor position
    b = GetCursorPos(&p);
    if (!b)
        return 2;

    // Retrieve the color at that position
    color = GetPixel(hDC, p.x, p.y);
    if (color == CLR_INVALID)
        return 1;

    // Release the device context again
    ReleaseDC(GetDesktopWindow(), hDC);

    printf("%i %i %i", GetRValue(color), GetGValue(color), GetBValue(color));
    return 0;
}
ETA: Appears to work, at least for me.

ETA2: Added some error checking

ETA3: Commented code, compiled executable and a Visual Studio Solution can be found in my SVN repository.



 

Sitemap 1 2 3 4 5 6 7 8 9