Reputation: 330
Delphi TStringGrid is displayed incorrectly (with unwanted column spacing) in Embracedero Delphi 10.4. I tried everything in settings - disabled margins, disable Ctl3D, font settings, ... - I also tried creating a totally new StringGrid, but there is still a problem with column spacing.
Code to reproduce:
procedure TForm5.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if ARow = 0 then
begin
StringGrid1.Canvas.Brush.Color := $808080;
StringGrid1.Canvas.FillRect(Rect)
end;
end;
Upvotes: 1
Views: 862
Reputation: 12322
I dropped a TStringGrid on the form and assigned an OnDrawCell event with the code you showed but this doesn't reproduce your issue exactly: I get only one pixel not drawn between cells.
To fix it, I enlarged the rectangle width and height by one pixel:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
R : TRect;
begin
R := Rect;
Inc(R.Right);
Inc(R.Bottom);
if ARow = 0 then
StringGrid1.Canvas.Brush.Color := $808080
else
StringGrid1.Canvas.Brush.Color := $A0A0A0;
StringGrid1.Canvas.FillRect(R)
end;
As you can see, I added a check to paint rows greater that 0 to see the same behavior in the row direction.
Upvotes: 0
Reputation: 21045
It is a known issue that the cell's Rect.Left
in the OnDrawCell
event of a TStringGrid
is offset by 4 pixels. Probably (but not documented) to make it more easy to output text data, which requires a small offset from the border of the cell.
Note that the TDrawGrid
does not have this offset.
For background painting of a cell you can use the CellRect()
function
procedure TForm5.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
with Sender as TStringGrid do
begin
if ARow = 0 then
begin
Canvas.Brush.Color := $C0C0C0;
Canvas.FillRect(CellRect(ACol, ARow));
end;
// text output using `Rect` follows
// ...
end;
end;
Upvotes: 3