Delphi StringGrid以不需要的列间距显示

Delphi StringGrid以不需要的列间距显示,delphi,delphi-10.4-sydney,stringgrid,Delphi,Delphi 10.4 Sydney,Stringgrid,Delphi TStringGrid在Enclusedero Delphi 10.4中显示不正确(具有不需要的列间距)。我尝试了所有设置-禁用边距、禁用Ctl3D、字体设置等-我还尝试创建了一个全新的StringGrid,但列间距仍然存在问题 要复制的代码: procedure TForm5.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); begin

Delphi TStringGrid在Enclusedero Delphi 10.4中显示不正确(具有不需要的列间距)。我尝试了所有设置-禁用边距、禁用Ctl3D、字体设置等-我还尝试创建了一个全新的StringGrid,但列间距仍然存在问题

要复制的代码:

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;

已知的一个问题是,在
TStringGrid
OnDrawCell
事件中,单元格的
Rect.Left
偏移了4个像素。可能(但没有文档记录)是为了使输出文本数据更容易,这需要与单元格边框有一个小的偏移量

请注意,
TDrawGrid
没有此偏移

对于单元格的背景绘制,可以使用
CellRect()
函数

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;

已知的一个问题是,在
TStringGrid
OnDrawCell
事件中,单元格的
Rect.Left
偏移了4个像素。可能(但没有文档记录)是为了使输出文本数据更容易,这需要与单元格边框有一个小的偏移量

请注意,
TDrawGrid
没有此偏移

对于单元格的背景绘制,可以使用
CellRect()
函数

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;

我在表单上删除了一个TStringGrid,并用您显示的代码分配了一个OnDrawCell事件,但这并没有准确地再现您的问题:我只得到一个像素,没有在单元格之间绘制

为了修复它,我将矩形的宽度和高度放大了一个像素:

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;

如您所见,我添加了一个复选框来绘制大于0的行,以查看行方向上的相同行为。

我在表单上放置了一个TStringGrid,并使用您显示的代码分配了一个OnDrawCell事件,但这并没有准确地再现您的问题:我只在单元格之间绘制了一个像素

为了修复它,我将矩形的宽度和高度放大了一个像素:

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;

如您所见,我添加了一个复选框,用于绘制大于0的行,以查看行方向上的相同行为。

您可以使用网格的方法,而不是手动偏移矩形。@Remy按照建议进行了修改。您可以使用网格的方法,而不是手动偏移矩形。@Remy按照建议进行了修改。