在Delphi中,如何更改TDBGrid中网格线的颜色?

时间:2018-12-30 22:01:46

标签: delphi colors dbgrid

我正在Delphi应用程序中使用TDBGrid组件,当我更改行颜色时,网格线变得不清楚或几乎不可见。

那么,任何人都可以向我们展示如何更改网格线的颜色吗?

我的意思是:如何更改单元格边框的颜色(见下图)

细胞边界

1 个答案:

答案 0 :(得分:7)

您在寻找

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const [Ref] Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
Var
  R: TRect;
begin
  R:= Rect;
  with DBGrid1.Canvas do
    begin
      Brush.Color:= clRed;
      R.Offset(Column.Width, 0);
      FillRect(R);
      R:= System.Types.Rect(Rect.Left, Rect.Bottom - 1, Rect.Right, Rect.Bottom);
      FillRect(R);
    end;
end;

结果将是:

一种更好的方法(来自Tom Brunberg注释)是使用FrameRect()作为

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const [Ref] Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  with DBGrid1.Canvas do
    begin
      Brush.Color:= clRed;
      FrameRect(Rect);
    end;
end;

使用FrameRect()在矩形区域周围绘制1像素宽的边框,该区域不会用“画笔”图案填充矩形的内部。 要使用钢笔绘制边界,请使用Polygon方法

相关问题