unit Main;

interface
{$I AnyLib.Inc}
uses
  Windows, Messages, SysUtils, {$IFDEF Delphi6} Variants,{$ELSE}FileCtrl,{$ENDIF}
  Classes, Graphics, Controls, Forms,
  Dialogs, BaseForm, AnyPrint, AnyGrids, ComCtrls, Buttons, ExtCtrls,
  StdCtrls, ImgList;

type
  TfrmMain = class(TfrmBase)
    Button1: TButton;
    ImageList1: TImageList;
    procedure AnyGridAfterSelectCell(Sender: TObject; Row, Col: Integer);
    procedure AnyGridUnSelectCell(Sender: TObject; Row, Col: Integer);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure AnyGridDrawCell(Sender: TObject; Row, Col: Integer;
      Canvas: TCanvas; R: TRect; var Handled: Boolean);
  private
    SCurrency:WideString;
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}
uses ShellApi;
procedure TfrmMain.AnyGridAfterSelectCell(Sender: TObject; Row,
  Col: Integer);
var
  Text:WideString;
  i:integer;
begin
  if (Col>=5) and (Row>=3) and (Row<=5) then
  begin
    Text:='';
    for i:=5 to AnyGrid.Cols-1 do
    begin
      Text:=Text+AnyGrid.Cells(Row,i).Text;
    end;
    Delete(Text,1,Length(SCurrency));
    AnyGrid.Cells(Row,5).Cols:=9;
    AnyGrid.Cells(Row,5).MaxLength:=8;
    AnyGrid.Cells(Row,5).ValueType:=vtWord;
    AnyGrid.Cells(Row,5).Text:=Text;
    for i:=6 to AnyGrid.Cols-1 do
    begin
      AnyGrid.Cells(Row,i).Text:='';
    end;
    AnyGrid.Row:=Row;
    AnyGrid.Col:=5;
  end;
end;

procedure TfrmMain.AnyGridUnSelectCell(Sender: TObject; Row, Col: Integer);
var
  Text:WideString;
  i,Len:integer;
begin
  if (Col>=5) and (Row>=3) and (Row<=5) then
  begin
    Text:='';
    for i:=5 to AnyGrid.Cols-1 do
      Text:=Text+AnyGrid.Cells(Row,i).Text;
    if (Text<>'') and (Copy(Text,1,Length(SCurrency))=SCurrency) then
      Delete(Text,1,Length(SCurrency));
    Len:=Length(Text);
    AnyGrid.Cells(Row,5).ValueType:=vtString;
    for i:=AnyGrid.Cols-1 downto 5 do
    begin
      AnyGrid.Cells(Row,i).Text:=Copy(Text,Length(Text),1);
      Delete(Text,Length(Text),1);
    end;
    if Len>=1 then
      AnyGrid.Cells(Row,AnyGrid.Cols-1-Len).Text:=SCurrency;
    AnyGrid.Cells(Row,5).Cols:=1;
  end;
end;

procedure TfrmMain.Button1Click(Sender: TObject);
begin
  inherited;
  ShowMessage('I am a delphi button');
end;

procedure TfrmMain.FormCreate(Sender: TObject);
var
  R:TRect;
  s:array[0..9] of char;
begin
  inherited;
  LayoutSection:='Main';
  R:=AnyGrid.CellByName('Control').Rect;
  AnyGrid.SetUserControlPos(Button1,R.Left+3,R.Top+3);
  AnyGrid.FixUserControl(Button1,True);
  GetLocaleInfo(LOCALE_SYSTEM_DEFAULT,LOCALE_SCURRENCY,s,10);
  SCurrency:=s;
end;

procedure TfrmMain.AnyGridDrawCell(Sender: TObject; Row, Col: Integer;
  Canvas: TCanvas; R: TRect; var Handled: Boolean);
var
  W,L1,L2:integer;
begin
  inherited;
  if (Row=12) and (Col=4) then
  begin
    W:=R.Right-R.Left;
    ImageList1.Draw(Canvas,R.Left+(W-ImageList1.Width) div 2,R.Top+4,1);
    Canvas.Font.Assign(Font);
    Canvas.Brush.Style:=bsClear;
    Canvas.Font.Color:=clBlack;
    Canvas.Font.Style:=[fsBold];
    L2:=Canvas.TextWidth('Draw');
    Canvas.Font.Style:=[];
    L1:=Canvas.TextWidth('Owner');
    Canvas.TextOut(R.Left+(W-L1-L2) div 2,R.Top+4+ImageList1.Height+4,'Owner');
    Canvas.Font.Style:=[fsBold];
    Canvas.TextOut(R.Left+(W-L1-L2) div 2+L1, R.Top+4+ImageList1.Height+4,'Draw');
    Handled:=True;
  end;
end;

end.
