如何根据屏幕上的 X、Y 坐标 return 颜色?

How do I return color based on X,Y coordinates from the screen?

这是 Delphi (7).

我一直在尝试为屏幕寻找像素搜索器,但没有太多帮助。至多我发现了一些可以截取整个屏幕截图并将其存储在 canvas 中的东西,但我不确定这是否真的有必要,因为唯一的目的是检查给定的协调。

我基本上只需要一些能让它起作用的东西:

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

尝试使用此代码:

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;

如果您需要特定 window 的像素,您需要使用 window 句柄修改 GetDC 调用。

GetDc https://msdn.microsoft.com/en-us/library/windows/desktop/dd144871(v=vs.85).aspx 获取像素 https://msdn.microsoft.com/en-us/library/windows/desktop/dd144909(v=vs.85).aspx

编辑: 在示例中,您可以使用函数(Windows 单元)GetRValueGetGValueGetBValue 代替位操作来提取 RGB 分量。例如:

R:= GetRValue(Color);