On the first hand, i want to have the text of the cells (except first row and column) centered. To do it, i select the column in the object inspector, and set its "Alignment" to tacenter, as proposed here by @wp here:
https://forum.lazarus.freepascal.org/index.php?topic=34285.0It works perfect.
However, i also want to add color to each independant column (except first column and row), while keeping the center alignment of the text. I use this piece of code on "OnDrawCell" event:
procedure TForm6.SunGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
aRect: TRect; aState: TGridDrawState);
var
ts: TTextStyle;
begin
if (ACol = 3) and (ARow >= 1) then
with TStringGrid(Sender) do
begin
Canvas.Brush.Color := $7FFFD4; //Testing color
Canvas.FillRect(aRect);
Canvas.TextOut(aRect.Left+2,aRect.Top+2,Cells[ACol, ARow]);
Canvas.Font.Color := clBlack;
ts := Canvas.TextStyle;
ts.Alignment := taCenter;
Canvas.TextStyle :=ts;
end
else if (ACol = 4) and (ARow >= 1) then
with TStringGrid(Sender) do
begin
Canvas.Brush.Color := clSkyBlue; //Testing color
Canvas.FillRect(aRect);
Canvas.TextOut(aRect.Left+2,aRect.Top+2,Cells[ACol, ARow]);
Canvas.Font.Color := clBlack;
ts := TStringGrid(Sender).Canvas.TextStyle;
ts.Alignment := taCenter;
TStringGrid(Sender).Canvas.TextStyle :=ts;
end;
end;
The color adjusment works perfectly, but not the aligment, since the text is left aligned in spite it was configured as centered in the object inspector, and that i included specific call to change it in the above code. BTW, you can see that i did in two different ways in each column in the code above, just to try to see if this could solve it.... nop!
The second issue is that when i select a row, the standard colored rows changes to a grey color, however, those cell on the modified columns keep the new color. How could i do to color all the selected cell in the same row show a blue color (instead of the grey one)?
I added a capture of the form on runtime to see the effects of these code.
Thanks!!