欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Delphi10.3的DBGrid美化(03)

程序员文章站 2022-03-03 08:25:11
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); var i: Integer; ......
procedure tform1.dbgrid1drawcolumncell(sender: tobject; const rect: trect;
  datacol: integer; column: tcolumn; state: tgriddrawstate);
var
  i: integer;
begin
  if gdselected in state then
    exit;
  // 定义表头的字体和背景颜色:
  for i := 0 to (sender as tdbgrid).columns.count - 1 do
  begin
    (sender as tdbgrid).columns[i].title.font.name := '宋体 '; // 字体
    (sender as tdbgrid).columns[i].title.font.size := 9; // 字体大小
    (sender as tdbgrid).columns[i].title.font.color := $000000ff; // 字体颜色 ( 红色 )
    (sender as tdbgrid).columns[i].title.color := $0000ff00; // 背景色 (绿色 )
  end;
  // 隔行改变网格背景色:
  if (sender as tdbgrid).datasource.dataset.recno mod 2 = 0 then
    (sender as tdbgrid).canvas.brush.color := clinfobk // 定义背景颜色
  else
    (sender as tdbgrid).canvas.brush.color := rgb(191, 255, 223); // 定义背景颜色
  // 定义网格线的颜色:
  tdbgrid(sender).defaultdrawcolumncell(rect, datacol, column, state);
  with (sender as tdbgrid).canvas do // 画 cell 的边框
  begin
    pen.color := $00ff0000; // 定义画笔颜色 (蓝色 )
    moveto(rect.left, rect.bottom); // 画笔定位
    lineto(rect.right, rect.bottom); // 画蓝色的横线
    pen.color := $0000ff00; // 定义画笔颜色 (绿色 )
    moveto(rect.right, rect.top); // 画笔定位
    lineto(rect.right, rect.bottom); // 画绿色的竖线
  end;
end;

Delphi10.3的DBGrid美化(03)