【问题标题】:How do I return color based on X,Y coordinates from the screen? [duplicate]如何根据屏幕上的 X、Y 坐标返回颜色? [复制]
【发布时间】:2017-02-13 13:52:09
【问题描述】:

这是为 Delphi (7) 准备的。

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

我基本上只需要一些可以使它起作用的东西:

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

【问题讨论】:

    标签: delphi position screen mouse pixel


    【解决方案1】:

    试试这个代码:

    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 获取像素 https://msdn.microsoft.com/en-us/library/windows/desktop/dd144909(v=vs.85).aspx

    编辑: 在示例中,您可以使用函数(Windows 单元)GetRValue、GetGValue、GetBValue,而不是位运算来提取 RGB 分量。例如:

    R:= GetRValue(Color);
    

    【讨论】:

    • 你不需要自己动手——单元窗口中有GetRValue/GetGValue/GetBValue。
    猜你喜欢
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    相关资源
    最近更新 更多