Reputation: 5184
SOLVED: I'm trying to change the background color of the column header on an FMX StringGrid using the onDrawColumnHeader. I can change the column header color but I lose the Header Text and Header Grid Lines.
What is the proper way to change the background color of the Column Headings so I can still see the text and grid lines?
Here is the code I'm using:
procedure TfrmCustomers.GridDrawColumnHeader(Sender: TObject; const Canvas: TCanvas;
const Column: TColumn; const Bounds: TRectF);
begin
//Exit;
Canvas.Fill.Kind := TBrushKind.Solid;
Canvas.Fill.Color := TAlphaColors.LightBlue;
Canvas.FillRect(Bounds,1);
end;
object lytGrid: TLayout
Align = Client
Padding.Left = 2.000000000000000000
Padding.Top = 2.000000000000000000
Padding.Right = 2.000000000000000000
Padding.Bottom = 2.000000000000000000
Size.Width = 640.000000000000000000
Size.Height = 398.000000000000000000
Size.PlatformDefault = False
TabOrder = 2
object Grid: TStringGrid
Align = Client
CanFocus = True
ClipChildren = True
Size.Width = 636.000000000000000000
Size.Height = 394.000000000000000000
Size.PlatformDefault = False
StyleLookup = 'GridStyle1'
TabOrder = 2
RowCount = 55
OnDrawColumnHeader = GridDrawColumnHeader
Viewport.Width = 616.000000000000000000
Viewport.Height = 353.000000000000000000
end
end
Here is a screen shot of the light Blue column headings:
Upvotes: 0
Views: 1102
Reputation: 5184
I needed to incorporate the Canvas.Filltext into the process.
Here's the code I finally came up with. It has a nice little side effect of centered column headings. The tricky bit was figuring out how to get the Column Header text. I found a nice piece of code in this SO answer that made it kind of easy to figure out.
procedure TfrmCustomers.GridDrawColumnHeader(Sender: TObject; const Canvas: TCanvas;
const Column: TColumn; const Bounds: TRectF);
var
R : TRectF;
begin
R := Bounds;
Canvas.Fill.Kind := TBrushKind.Solid;
Canvas.Fill.Color := TAlphaColorRec.Dimgray;
Canvas.FillRect(R,1);
R.Inflate(0,0,-0.25,-0.25);
Canvas.Fill.Color := TAlphaColorRec.Whitesmoke;
Canvas.FillRect(R,1);
Canvas.Fill.Color := TAlphaColors.Black;
Canvas.Font.Style := [TFontStyle.fsBold];
Canvas.FillText(Bounds,Grid.ColumnByIndex(Column.Index).Header,False,1,[],TTextAlign.Center,TTextAlign.Center);
end;
The first FillRect paints the entire cell Dimgray. The second FillRect paints the cell WhiteSmoke with a small little twist. Before calling the FillRect, the RectF is reduced sligtly on the right side and on the bottom edge. This lets a tiny sliver of the Dimgray from the first RectF through which acts as a border.
Upvotes: 2