2017-02-13 76 views
1

這是德爾福(7)。如何根據屏幕上的X,Y座標返回顏色?

我一直在尋找屏幕的像素搜索器,但沒有太多的幫助。最多的時候,我發現了一些需要完整截圖並將其存儲在畫布中的內容,但我不確定是否真的有必要,因爲唯一的目的是檢查給定的協調。

我基本上只需要的東西,這將使這個工程:

procedure TForm1.Button1Click(Sender: TObject); 
begin 
if(Checkcolor(1222,450) == 000000) then 
showmessage('Black color present at coordinates'); 
end; 

回答

4

嘗試使用此代碼:

function ColorPixel(P: TPoint): TColor; 
var 
    DC: HDC; 
begin 
    DC:= GetDC(0); 
    Result:= GetPixel(DC,P.X,P.Y); 
    ReleaseDC(0,DC); 
end; 

一個例子程序,以顯示十六進制顏色:

var 
    P: TPoint; 
    R,G,B: integer; 
begin 
    GetCursorPos(P); 
    Color:= ColorPixel(P); 
    R := Color and $ff; 
    G := (Color and $ff00) shr 8; 
    B := (Color and $ff0000) shr 16; 
    ShowMessage(format('(%d,%d,%d)',[R,G,B])); 
end; 

如果您需要特定窗口的像素,您需要使用窗口句柄修改GetDC調用。

GETDC https://msdn.microsoft.com/en-us/library/windows/desktop/dd144871(v=vs.85).aspx GetPixel https://msdn.microsoft.com/en-us/library/windows/desktop/dd144909(v=vs.85).aspx

編輯: 在這個例子中,可以提取使用函數(視窗單元)GetRValueGetGValueGetBValue,代替位操作RGB分量。例如:

R:= GetRValue(Color); 
+1

您不需要自己動手一點 - 在單元Windows中有GetRValue/GetGValue/GetBValue。 –

+0

太棒了!謝謝一堆。 –

相關問題