Recent

Author Topic: Change color of DrawGrid Cell  (Read 5408 times)

PeterATC

  • New Member
  • *
  • Posts: 12
Change color of DrawGrid Cell
« on: July 08, 2013, 02:52:33 am »
Hi I am trying to get the color of cells on a draw grid to change color. Below is my code but it does not change the color.

procedure TfrmMain.DrawGridDrawCell;
var
 i, j : integer;
begin
     for i := 0 to frmMain.drwGrid.ColCount-1 do
       for j := 0 to frmMain.drwGrid.RowCount-1 do
        begin
            frmMain.drwGrid.CellRect(i,j);
            frmmain.drwGrid.FocusColor:= ColorOfBricks[i,j];

        end;
end;   

"ColorofBricks"  contains a two dimensional array of TColor values.

Any help much appreciated.

Blaazen

  • Hero Member
  • *****
  • Posts: 2846
  • POKE 54296,15
    • Eye-Candy Controls
Re: Change color of DrawGrid Cell
« Reply #1 on: July 08, 2013, 03:04:41 am »
Try OnPrepareCanvas:
Code: [Select]
procedure TForm1.DrawGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer;
  aState: TGridDrawState);
begin
  if aRow = 1
    then DrawGrid1.Canvas.Brush.Color := clRed
    else DrawGrid1.Canvas.Brush.Color := clGreen;
end;
This will color all cells to green except cells in the row = 1.
You don't have to use loops. Event is triggered for each cell.
Lazarus 2.1.0 r63594 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

PeterATC

  • New Member
  • *
  • Posts: 12
Re: Change color of DrawGrid Cell
« Reply #2 on: July 10, 2013, 04:02:02 am »
HI that works but I am trying to create a grid with three colors in random bricks. So the color of each cell needs to be read from the colors in the two dimentional array "ColorOfBricks". Is there way to cycle thru each cell and apply the color to the cell.

taazz

  • Hero Member
  • *****
  • Posts: 5365
Re: Change color of DrawGrid Cell
« Reply #3 on: July 10, 2013, 04:10:27 am »
no, cells are not objects per see just positions in the grid to be drawn upon, why don't you use the acol,arow parameters to get the color from the ColorOfBricks array? eg.

Code: [Select]
procedure TForm1.DrawGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer;
  aState: TGridDrawState);
begin
    DrawGrid1.Canvas.Brush.Color := ColorOfBricks[aRow,aCol];
end;
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

PeterATC

  • New Member
  • *
  • Posts: 12
Re: Change color of DrawGrid Cell
« Reply #4 on: July 11, 2013, 01:54:05 pm »
Thank taazz. It works fine. never thought of using those paramaters.

 

TinyPortal © 2005-2018