2010-06-20 36 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

相關問題