//ZColorStringGrid Sample
// тестовый набросок

unit unit_main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ZColorStringGrid, Buttons, StdCtrls, ImgList, Grids, ComCtrls;

type
  TfrmMain = class(TForm)
    ZSG: TZColorStringGrid;
    Label1: TLabel;
    Label2: TLabel;
    btnTop: TBitBtn;
    btnCenter: TBitBtn;
    btnBottom: TBitBtn;
    btnLeft: TBitBtn;
    btnCenterH: TBitBtn;
    btnRight: TBitBtn;
    btnFont: TBitBtn;
    FntDialog: TFontDialog;
    BitBtn2: TBitBtn;
    ImageList1: TImageList;
    procedure ZSGSelectCell(Sender: TObject; ACol,
      ARow: Integer; var CanSelect: Boolean);
    procedure btnTopClick(Sender: TObject);
    procedure btnCenterClick(Sender: TObject);
    procedure btnBottomClick(Sender: TObject);
    procedure btnLeftClick(Sender: TObject);
    procedure btnCenterHClick(Sender: TObject);
    procedure btnRightClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnFontClick(Sender: TObject);
    procedure FormCanResize(Sender: TObject; var NewWidth,
      NewHeight: Integer; var Resize: Boolean);
    procedure BitBtn2Click(Sender: TObject);
    procedure ZSGDrawMergeCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState; var CellCanvas: TCanvas);
    procedure ZSGBeforeTextDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
  private
    { Private declarations }
  public
    procedure kkk;
    procedure CalcSelection;
    procedure setvert(t: TVerticalAlignment);
    procedure setHor(t: TAlignment);
    { Public declarations }
  end;

var
  frmMain: TfrmMain;
  zzz: TBitmap;

implementation

{$R *.DFM}

procedure TfrmMain.ZSGSelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
  btnTop.Font.Style :=[];
  btnCenter.Font.Style :=[];
  btnBottom.Font.Style :=[];
  btnLeft.Font.Style :=[];
  btnRight.Font.Style :=[];
  btnCenterH.Font.Style :=[];
  if zsg.CellStyle[ACol, ARow].VerticalAlignment = vaTop then btnTop.Font.Style :=[fsBold] else
  if zsg.CellStyle[ACol, ARow].VerticalAlignment = vaCenter then btnCenter.Font.Style :=[fsBold] else
  if zsg.CellStyle[ACol, ARow].VerticalAlignment = vaBottom then btnBottom.Font.Style :=[fsBold];
  if zsg.CellStyle[ACol, ARow].HorizontalAlignment = taLeftJustify then btnLeft.Font.Style := [fsBold] else
  if zsg.CellStyle[ACol, ARow].HorizontalAlignment = taCenter then btnCenterH.Font.Style := [fsBold] else
  if zsg.CellStyle[ACol, ARow].HorizontalAlignment = taRightJustify then btnRight.Font.Style := [fsBold];
end;

procedure TfrmMain.kkk;
var
  t: boolean;

begin
  t:= true;
  zsg.OnSelectCell(zsg,zsg.Col,zsg.Row,t);
end;

procedure TfrmMain.CalcSelection;
var
  t: integer;

begin                        
  t := zsg.MergeCells.InMergeRange(zsg.Col, zsg.Row);
  if t>=0 then
    zsg.Selection := TGridRect(zsg.MergeCells.Items[t]);
end;

//установить вертикальное выравнивание для
//выделенной области/одиночной ячейки
procedure TFrmMain.setvert(t: TVerticalAlignment);
var
  i,j: integer;

begin
  CalcSelection;
  for i:=zsg.Selection.left to zsg.Selection.Right do
  for j:=zsg.Selection.top to zsg.Selection.Bottom do
  zsg.CellStyle[i, j].VerticalAlignment := t;
  kkk;
end;

//установить горизонтальное выравнивание для
//выделенной области/одиночной ячейки
procedure TfrmMain.setHor(t: TAlignment);
var
  i,j: integer;

begin
  CalcSelection;
  for i:=zsg.Selection.left to zsg.Selection.Right do
  for j:=zsg.Selection.top to zsg.Selection.Bottom do
  zsg.CellStyle[i, j].HorizontalAlignment := t;
  kkk;        
end;

procedure TfrmMain.btnTopClick(Sender: TObject);
begin
  setvert(vaTop);
end;

procedure TfrmMain.btnCenterClick(Sender: TObject);
begin
  setvert(vaCenter);
end;

procedure TfrmMain.btnBottomClick(Sender: TObject);
begin
  setvert(vaBottom);
end;

procedure TfrmMain.btnLeftClick(Sender: TObject);
begin
  setHor(taLeftJustify);
end;

procedure TfrmMain.btnCenterHClick(Sender: TObject);
begin
  setHor(taCenter);
end;

procedure TfrmMain.btnRightClick(Sender: TObject);
begin
  setHor(taRightJustify);
end;

procedure TfrmMain.FormCreate(Sender: TObject);
var
  i: integer;

begin           
  //заполняем грид 
  zsg.CellStyle[1,1].Font.color := clWhite;
  i := zsg.MergeCells.AddRectXY(1,0,4,0);
  i := zsg.MergeCells.AddRectXY(5,0,5,1);
     i := zsg.MergeCells.AddRectXY(2,3,5,5);
  i := zsg.MergeCells.AddRectXY(1,1,2,1);
  i := zsg.MergeCells.AddRectXY(3,1,4,1);
  i := zsg.MergeCells.AddRectXY(0,0,0,1);
  i := zsg.MergeCells.AddRectXY(10,0,10,1);
    i := zsg.MergeCells.AddRectXY(3,6,5,8);
    i := zsg.MergeCells.AddRectXY(6,3,10,8);
  zsg.MergeCells.AddRectXY(zsg.ColCount - 4, 2, zsg.ColCount - 1, 15);
  zsg.MergeCells.AddRectXY(0, 3, 1, 10);

  zsg.MergeCells.AddRectXY(1, 18, 4, 20);

  zsg.CellStyle[6,3].BGColor := ClWhite;
  zsg.CellStyle[2,3].BGColor := ClGreen;
  zsg.Cells[2,3] := ' asda sdkja hdkjha kjsdhkahdkaj dkah sdahsdahskd Фвф';

  zsg.Cells[0,0] := '№ П/П';
  zsg.Cells[1,0] := 'надпись 1';

  i := zsg.MergeCells.AddRectXY(3,38,8,39);
  i := zsg.MergeCells.AddRectXY(10,37,13,38);

  i := zsg.MergeCells.AddRectXY(10,35,13,36);

  i := zsg.MergeCells.AddRectXY(3,36,8,37);
  i := zsg.MergeCells.AddRectXY(5,9,5,34);


  for i:=1 to 39 do
  begin
    zsg.CellStyle[0,i].Font.size  := i + 7;
    zsg.cells[0,i] := inttostr(i)+'.';
    if i <> 1 then
    begin
      zsg.CellStyle[1,i].BGColor := clRed + i*5 + 50;
      zsg.CellStyle[1,i].Font.Color := clWhite;
    end;
  end;
  zsg.Cells[5,0] := 'Что-то там общее';
  zsg.CellStyleRow[5, false] := zsg.CellStyle[1,1];
  zsg.CellStyle[5,3].BGColor := clwhite;
  zsg.CellStyle[5,3].Font.size := 16;
  zsg.CellStyleCol[5, false] := zsg.CellStyle[5,3];
end;

//изменить шрифт ячейки
procedure TfrmMain.btnFontClick(Sender: TObject);
begin
  CalcSelection;
  FntDialog.Font.Assign(ZSG.CellStyle[ZSG.Col, ZSG.Row].Font);
  if FntDialog.Execute then
    ZSG.CellStyle[ZSG.selection.left, zsg.Selection.top].Font := FntDialog.Font;
end;

//изменение размера формы
procedure TfrmMain.FormCanResize(Sender: TObject; var NewWidth,
  NewHeight: Integer; var Resize: Boolean);
begin
  if (newHeight<854)or(newWidth<733) then Resize := false;
  ZSG.Height := height - ZSG.top - 40;
  ZSG.Width := width - ZSG.left - 20;
end;

//раскрашиваем ячейки
procedure TfrmMain.BitBtn2Click(Sender: TObject);
var
  i, j, k: integer;

begin
  ZSG.editormode := true;
  k:=0;
  for j:=1 to 19 do
  for i:=2 to 39 do
  begin
    inc(k);
    zsg.CellStyle[10+j,i].BGColor := clGreen + ClBlue - 10000{clBlue} + k*20{i*j} + 20;
  end;
end;

//в объединенных ячейках рисуем картинку
procedure TfrmMain.ZSGDrawMergeCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState; var CellCanvas: TCanvas);
begin
  with CellCanvas do
    ImageList1.Draw(CellCanvas, rect.left + 5, rect.top +(rect.Bottom-rect.top) div 2 - 8, 0, true);
end;

procedure TfrmMain.ZSGBeforeTextDrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  if (Arow = (sender as TZCOlorStringGrid).row) and (ACol = (sender as TZCOlorStringGrid).col) then
  with (sender as TZCOlorStringGrid) do
  begin
    canvas.pen.Color := clRed;
    canvas.MoveTo(rect.Left, rect.Top);
    canvas.LineTo(rect.Right, rect.Bottom);
  end;
end;

end.

// CalcDrawInfo

