unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Grids, ImgList;

type
  TForm2 = class(TForm)
    Button1: TButton;
    img: TImage;
    Panel1: TPanel;
    StringGrid1: TStringGrid;
    Button2: TButton;
    Label1: TLabel;
    lst: TImageList;
    Label2: TLabel;
    Timer1: TTimer;
    CheckBox1: TCheckBox;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure imgMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
  private
    Fbmp: TBitmap;
    FbmpIdx: TBitmap;
    function IndexFromPoint(X1, Y1: Integer): integer;
    procedure UpdateGrid;

    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;
const
  MESH_WIDTH = 144;
  MESH_HEIGHT = 144;
  MESH_TOP = 55;
  MESH_LEFT = 12;
  ROW_HEIGH = 16;
implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
var h: THandle;
begin
  h := FindWindowEx(panel1.Handle,0,nil, 'Сапер');
  if h = 0 then
  begin
  WinExec('winmine',SW_SHOWNA);
  sleep(100);
  h := FindWindow(nil, 'Сапер');
  end;
  
  if  windows.GetParent(h) <> panel1.Handle then
  begin
    windows.SetParent(h, panel1.Handle);
    SetWindowPos(h,0,0,0,0,0,SWP_NOSIZE);
  end;
  h := GetDC(h);
  img.Canvas.Handle := h;
  img.Repaint;
end;

procedure TForm2.UpdateGrid;
var
  i: Integer;
  j: Integer;
begin
  i := 0; j := 0;
  for i := 0 to 8 do
    for j := 0 to 8 do
  begin
    StringGrid1.Cells[i,j] := IntToStr(
      IndexFromPoint((i*(ROW_HEIGH))+MESH_LEFT ,
        (j*(ROW_HEIGH))+MESH_TOP));
  end;
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
  UpdateGrid
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  Fbmp := TBitmap.Create;
  Fbmp.Width := ROW_HEIGH;
  Fbmp.Height := ROW_HEIGH;
  FbmpIdx := TBitmap.Create;
  FbmpIdx.Width := ROW_HEIGH;
  FbmpIdx.Height := ROW_HEIGH;
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
  Fbmp.Free;
  FbmpIdx.Free;
end;

function TForm2.IndexFromPoint(X1, Y1: Integer): integer;

  function CompareBMP(b1,b2: TBitmap): boolean;
  var
    I: Integer;
    j: Integer;
  begin
    { TODO : оптимизировать на сравнение памяти }
    Result := True;
    for I := 0 to 15 do
      for j := 0 to 15 do
        if b1.Canvas.Pixels[i,j] <> b2.Canvas.Pixels[i,j] then
        begin
          Result := False;
          Exit;
        end;
  end;
var
  n: Integer;

begin
  Fbmp.Canvas.CopyRect(Rect(0,0,16,16),img.Canvas, Rect(X1, Y1, X1 + ROW_HEIGH, Y1 + ROW_HEIGH));
  Result := -1;
  for n := 0 to lst.Count - 1 do
  begin
    if not lst.GetBitmap(n, FbmpIdx) then exit;
    if CompareBMP(FbmpIdx, Fbmp) then
    begin
      Result := n;
      Exit;
    end;
  end;
end;

procedure TForm2.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var i: Integer;
begin
  if CheckBox1.Checked then
  begin
    i := StrToIntDef(StringGrid1.Cells[ACol,ARow], -1);
    if i >= 0 then
      lst.GetBitmap(i, FbmpIdx);
      StringGrid1.Canvas.Draw(Rect.Left, Rect.Top, FbmpIdx);
  end;
end;

procedure TForm2.Timer1Timer(Sender: TObject);
begin
  UpdateGrid
end;

procedure TForm2.imgMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  label1.Caption := 'X = ' + IntToStr(X) + '; Y = ' + IntToStr(Y);
end;

end.
