Recent

Author Topic: colored string grid  (Read 13131 times)

grogh

  • Guest
colored string grid
« on: May 17, 2004, 02:38:20 pm »
Hello...
How implement color string grid with Lazarus?

Thanks

jesusr

  • Sr. Member
  • ****
  • Posts: 499
colored string grid
« Reply #1 on: May 18, 2004, 04:25:15 am »
usually you could use the OnDrawCell, for example to draw even rows in blue and odd rows in red, use this code:
Code: [Select]

procedure TForm1.GridDrawCell(sender as TObject; Col,Row: Integer; aRect: TRect; aState: TGridDrawState)
begin
  if not (gdfixed in aState) then begin
    if Row mod 2 = 0 then begin
      Grid.Canvas.Brush.Color := clBlue;
    end else begin
      Grid.Canvas.Brush.Color := clRed;
    end;
    Grid.canvas.fillrect(arect);
    Grid.canvas.textrect(arect, 0, 0, grid.cells[col,row])
  end else begin
    // draw fixed grid in a standard way
    Grid.defaultdrawcell(col,row,arect,astate);  
  end;
end;

since a few days ago, a new event was introduced to do this easily: OnPrepareCanvas

so the last example would be:
Code: [Select]

procedure form1.GridPrepareCanvas(sender as TObject; Col,Row: Integer; aState: TGridDrawState )
begin
  if not (gdfixed in aState) then
    if Col mod 2 = 0 then begin
      Grid.Canvas.Brush.Color := clBlue;
    end else begin
      Grid.Canvas.Brush.Color := clRed;
    end;
end;


No need to draw anything. I didn't actually check the above examples, they are simply illustrative.

wile64

  • New Member
  • *
  • Posts: 19
    • http://wile64.neuf.fr/
colored string grid
« Reply #2 on: October 25, 2006, 09:28:04 pm »
hello,
excused me for my bad English, I am user of Lazarus since one years and I would like colored a dbgrid according to criteria's, the example which you function gives well but my system (winXP) is to use has 90%, why?

quileab

  • New member
  • *
  • Posts: 9
colored string grid
« Reply #3 on: November 03, 2006, 12:16:26 pm »
This is my onDrawCell, works fine in my case. Could be used for any StringGrid using (sender as ...)

procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
  aRect: TRect; aState: TGridDrawState);
const
Colores:array[0..3] of TColor=($ffefef, $efffef, $efefff, $efffff);
ColSele:array[0..3] of TColor=($444444, $444444, $444444, $444444);
begin
  if not (gdFixed in aState) then // si no es el tituloŽ
  if not (gdSelected in aState) then
    begin
    (Sender as TStringGrid).Canvas.Brush.Color:=Colores[Row mod 4];
    end
  else
    begin
    (Sender as TStringGrid).Canvas.Brush.Color:=ColSele[Row mod 4];
    (Sender as TStringGrid).Canvas.Font.Color:=$ffffff;
    //(Sender as TStringGrid).Canvas.Font.Style:=[fsBold];
    end;

  //(Sender as TStringGrid).DefaultDrawColumnCell(Rect,DataCol,Column,State);
  (Sender as TStringGrid).defaultdrawcell(col,row,arect,astate);

end;

I hope you can adapt it. Excuse my bad English. Enjoy!

 

TinyPortal © 2005-2018