2010-06-20 47 views

回答

12

了我的头顶部,直接的方式:

#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:似乎工作,至少对我来说。

ETA2:增加了一些错误检查

ETA3:注释代码,编译成可执行和Visual Studio解决方案可以在my SVN repository找到。

+0

非常感谢!我想我在“Win32控制台应用程序”中构建这个? – 2010-06-20 11:01:45

+0

@Jeremy:是的。我无法弄清楚如何从命令行构建它,但作为来自Visual Studio的控制台应用程序,它工作正常。 – Joey 2010-06-20 11:06:15

+0

谢谢!还有一件事,我试图从另一个应用程序调用这个幕后,所以有没有办法隐藏黑色命令行窗口显示? – 2010-06-20 11:58:56