function FindBitmapBox(wHandle: HWND; xs,ys,xe,ye:integer; bitmap: Integer; var x, y: Integer; flag, tol, mask: Integer): Boolean;
var
bmp1, bmp2: TBitmap;
x1, y1, x2, y2, tmpColor, FirstColor: Integer;
FirstPoint: TPoint;
tmpDC: HDC;
tmpRECT: TRect;
p, p2, p3: PRGBArray;
label first, done, testy;
begin
GetWindowRECT(wHandle, tmpRECT);
//showmessage(inttostr(tmprect.top));
tmpDC := getwindowdc(wHandle);
bmp2 := TBitmap.Create;
bmp2.Handle := bitmap;
bmp1 := TBitmap.Create;
bmp1.PixelFormat := pf24bit;
bmp1.Width := tmpRECT.Right - tmpRECT.Left;
bmp1.Height := tmpRECT.Bottom - tmpRECT.Top;
BitBlt(bmp1.Canvas.Handle, 0, 0, xe - xs, ye - ys, tmpdc, xs, ys, SRCCOPY);
//BitBlt(form1.image2.Canvas.Handle, 0, 0, xe - xs, ye - ys, bmp1.canvas.handle, 0, 0, SRCCOPY);
bmp1.Height := ye - ys;
bmp1.Width := xe - xs;
releasedc(whandle,tmpDC);
Result := True;
FirstPoint.X := 0; FirstPoint.Y := 0;
if mask >= 0 then begin
for y1 := 0 to bmp2.Height - 1 do
for x1 := 0 to bmp2.Width - 1 do
if GetPixel(bmp2.Canvas.Handle, x1, y1) <> mask then begin
FirstColor := GetPixel(bmp2.Canvas.Handle, x1, y1);
FirstPoint.X := x1; FirstPoint.Y := y1;
goto first;
end;
x := round(bmp2.Width / 2);
y := round(bmp2.Height / 2);
goto done;
end;
FirstColor := GetPixel(bmp2.Canvas.Handle, 0, 0);
first:
//showmessage(inttostr(FirstPoint.x));
for y1 := 0 to bmp1.Height - 1 do begin
p := bmp1.ScanLine[y1];
for x1 := 0 to bmp1.Width - 1 do begin
tmpColor := RGB(p[x1].b, p[x1].g, p[x1].r);
if SimilarColors(tmpColor, FirstColor, tol) then begin
for y2 := 0 to bmp2.Height - 1 do begin
if y1 + y2 - FirstPoint.y > bmp1.height - 1 then goto testy;
if y1 + y2 - FirstPoint.y < 0 then goto testy;
p2 := bmp2.ScanLine[y2];
p3 := bmp1.ScanLine[y1 + y2 - FirstPoint.y];
for x2 := 0 to bmp2.Width - 1 do begin
x := x1 + x2 - FirstPoint.X;
tmpColor := RGB(p2[x2].b, p2[x2].g, p2[x2].r);
if x > bmp1.Width - 1 then goto testy;
if x < 0 then goto testy;
if mask < 0 then begin
if not SimilarColors(tmpColor, RGB(p3[x].b, p3[x].g, p3[x].r), Tol) then goto testy;
end else if tmpColor <> mask then if not SimilarColors(RGB(p3[x].b, p3[x].g, p3[x].r), tmpColor, Tol) then goto testy;
end;
end;
//showmessage(inttostr(x1-firstpoint.X)+','+inttostr(y1-firstpoint.Y));
x := xs + x1 - FirstPoint.x; y := ys + y1 - FirstPoint.Y;
if flag in [4, 5, 6] then y := y + ys + round(bmp2.Height / 2);
if flag in [7, 8, 9] then y := y + ys + bmp2.Height;
if flag in [2, 5, 8] then x := x + xs + round(bmp2.Width / 2);
if flag in [3, 6, 9] then x := x + xs + bmp2.Width;
goto done;
end;
testy:
end;
end;
Result := False;
done:
bmp1.Free;
bmp1 := nil;
bmp2.ReleaseHandle;
bmp2.Free;
bmp2 := nil;
end;