TSMH’s blog

多趣味人間のメモ

<StringGrid>セルの色変更

オブジェクト<StringGrid>各セルの色変更のやり方について

サンプルソース

procedure TFMain.StringGrid1DrawColumnCell(Sender: TObject;
  const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF;
  const Row: Integer; const Value: TValue; const State: TGridDrawStates);
//
procedure ToRed;
begin
  // 背景色を設定
  Canvas.Fill.Color := TAlphaColors.Red;
  Canvas.FillRect(Bounds, 0, 0, , 1);
  // テキストを描画(テキストの色や位置を調整することもできます)
  Canvas.Fill.Color := TAlphaColors.White; // テキストの色
  Canvas.FillText(Bounds, Value.ToString, False, 1,
, TTextAlign.Leading,
TTextAlign.Leading);
end;
//
begin
  if (Column.Index = 0) or (Row = 0) then
  begin
      ToRed;
  end;
end;