0

I have the following code:

procedure TForm1.Button1Click(Sender: TObject);
var
  XYCoord: integer;
  window: hwnd;
  c: TCanvas;
  result : TColor;
  color : string;
begin
  c := TCanvas.Create;
  window := FindWindow('Atlantica Online', NIL);
  try
    c.Handle := GetWindowDC(window);
    Result   := GetPixel(c.Handle, 50, 10);
  finally
    c.Free;
  end;
  color := GetColorASRGBString(result, true);
  if color = '#FF0000' then
  begin
    SendMessage(window, WM_LBUTTONDOWN, MK_LBUTTON, makelong(50, 10));
    SendMessage(window, WM_LBUTTONUP, MK_LBUTTON, makelong(50, 10));
  end;
end;

function TForm1.GetColorASRGBString(
  const ColorToConvert : TColor;
  const IncludePrefixChar: Boolean): String;
var
  r,g,b         : Byte;
  CurrentColor  : TColor;
  HexColorWithSpaces : String;
const
  HexFormatStr  : String = '%2x';
begin
  CurrentColor  := ColorToConvert;

  CurrentColor  := ColorToRGB(CurrentColor);
  r := GetRValue(CurrentColor);
  g := GetGValue(CurrentColor);
  b := GetBValue(CurrentColor);

  HexColorWithSpaces := IfThen(IncludePrefixChar, '#','')
    + Format(HexFormatStr, [r])
    + Format(HexFormatStr, [g])
    + Format(HexFormatStr, [b]);
  Result := AnsiReplaceStr(HexColorWithSpaces, ' ', '0');
end;

If a give pixel on a given window is red (255,0,0) it should click there...

Works very fine for every normal application...

But if an application uses DirectX it fails (returns black (#000000))...

Are there any solutions for this without need to hook the directx?

Thanks in advance

Michael Grenzer
  • 483
  • 1
  • 9
  • 20
  • my inbox shows me 2 comments.. why i can't see them? :( – Michael Grenzer Sep 20 '12 at 20:48
  • That was me saying you, that you'll need to use DirectX function since GDI function can't access a DirectX drawing buffers because they works in a different level, but I've deleted them later... – TLama Sep 20 '12 at 20:52
  • Means, i would really have to hook the DirectX functions of the application where i want to read the pixels, copy the buffer and then search this one? – Michael Grenzer Sep 20 '12 at 21:02

1 Answers1

0

I think what you are looking for is IDirect3DSurface9::GetDC Assign that DC to your canvas and you should be good. A bit of googling will give you lots of information about IDirect3DSurface9::GetDC.

iamjoosy
  • 3,189
  • 18
  • 26