unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    DrawGrid1: TDrawGrid;
    Image1: TImage;
    Image2: TImage;
    OpenPictureDialog1: TOpenPictureDialog;
    Label1: TLabel;
    Label2: TLabel;
    Button1: TButton;
    Button2: TButton;
    Label3: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure DrawGrid1GetEditText(Sender: TObject; ACol, ARow: Integer;
      var Value: string);
    procedure DrawGrid1SetEditText(Sender: TObject; ACol, ARow: Integer;
      const Value: string);
    procedure Button2Click(Sender: TObject);
    procedure DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Button1Click(Sender: TObject);
    procedure DrawGrid1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  end;
  obraz=record
          wsp:TPoint;
          obr:TBitmap;
        end;
  tekst=record
          wsp:TPoint;
          napis:shortstring;
        end;
var
  Form1: TForm1;
  edycja:boolean;
  bwiersz,bkolumna,index,ind: integer;
  w:string;
  tab: array[0..100]of obraz;
  tab1:array[0..100]of tekst;
implementation

{$R *.dfm}

procedure TForm1.DrawGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if not edycja then
  begin
  //na podstawie wspólrzednych myszy - w pikselach okresla nr wiersza i kolumny
  DrawGrid1.MouseToCell(x,y,bkolumna,bwiersz);
  Label1.Caption:='wiersz='+InttoStr(bwiersz);
  Label2.Caption:='kolumna='+Inttostr(bkolumna);
  tab[index].wsp.x:=bwiersz;
  tab[index].wsp.y:=bkolumna;
  tab[index].obr:=TBitmap.Create;
  tab[index].obr.Assign(Image2.Picture.Bitmap);
  inc(index);
  //*****
 end
 else
 begin
  inc(ind);
  tab1[ind].wsp.x:=bwiersz;
  tab1[ind].wsp.y:=bkolumna;
  tab1[ind].napis:=w;
  edycja:=false;
 end;

end;

//wczytanie obrazu do komponentu Image
procedure TForm1.Button1Click(Sender: TObject);
begin
 if  Openpicturedialog1.Execute then Image1.Picture.LoadFromFile(OpenPicturedialog1.FileName);
end;

procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var prost:TRect;
    k:integer;
begin
  //rysuje we wszystkich komórkach elipsę
  //Drawgrid1.Canvas.Ellipse(Rect);

  for k:=0 to ind do
  begin
    prost:=Drawgrid1.CellRect(tab1[k].wsp.y,tab1[k].wsp.x);//,Acol);//bwiersz,bkolumna);
    Drawgrid1.Canvas.TextRect(prost,prost.Left+2,prost.Top+2,tab1[k].napis);
   //Drawgrid1.Canvas.Draw(prost.Left,prost.top,tab[k].obr);
   end;

  for k:=0 to index do
  begin
    prost:=Drawgrid1.CellRect(tab[k].wsp.y,tab[k].wsp.x);//,Acol);//bwiersz,bkolumna);
    Drawgrid1.Canvas.Draw(prost.Left,prost.top,tab[k].obr);
   end;

 //***************
 //rysuje we wszystkich komorkach to samo
 // Drawgrid1.Canvas.Draw(rect.Left,rect.top,Image2.picture.Bitmap);
 //***************
 if gdFocused in State then Drawgrid1.Canvas.DrawFocusRect(Rect);
 end;

//skalowane wczytanego obrazu do rozmiaru komórki
procedure TForm1.Button2Click(Sender: TObject);
var pr: TRect;
begin
  //sprowadzanie Image1 do skali Image2, by zmiescic sie w siatce
  Image2.Picture.Bitmap.Height:=Drawgrid1.DefaultRowHeight;
  Image2.Picture.Bitmap.Width:=Drawgrid1.DefaultColWidth;
  pr:=Rect(1,1,Drawgrid1.DefaultColWidth,Drawgrid1.DefaultRowHeight);
  Image2.Canvas.StretchDraw(pr,Image1.Picture.Bitmap);//(0,0,100,100),Image1.Picture.Bitmap);
end;

procedure TForm1.DrawGrid1SetEditText(Sender: TObject; ACol, ARow: Integer;
  const Value: string);
begin
  w:=Value;
  edycja:=true;
  //label3.Caption:=label3.Caption+w+#13;//tab1[ind].napis+#13;
end;

procedure TForm1.DrawGrid1GetEditText(Sender: TObject; ACol, ARow: Integer;
  var Value: string);
begin
  //Value:=w;
  //label3.Caption:=label3.Caption+w+#13;//tab1[ind].napis+#13;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
tab1[0].wsp.x:=1;
tab1[0].wsp.y:=1;
tab1[0].napis:='ala';

tab1[1].wsp.x:=2;
tab1[1].wsp.y:=2;
tab1[1].napis:='oala';
ind:=1;
end;

end.
