|
 |
栏目导栏 |
|
| |
|
|
|
|
 |
资料搜索 |
|
| |
|
|
|
|
 |
热门文章 |
|
| |
|
|
|
|
 |
最新文章 |
|
| |
|
|
|
| |
| |
|
|
|
|
在 Delphi 语言的数据库编程中,DBGrid 是显示数据的主要手段之一。但是 DBGrid 缺省的外观未免显得单调和缺乏创意。其实,我们完全可以在我们的程序中通过编程来达到美化 DBGrid 外观的目的。通过编程,我们可以改变 DBGrid 的表头、网格、网格线的前景色和背景色,以及相关的字体的大小和风格。HETLinux联盟 以下的示例程序演示了对 DBGrid 各属性的设置,使 Delphi 显示的表格就像网页中的表格一样漂亮美观。HETLinux联盟 示例程序的运行:在 Form1 上放置 DBGrid1、Query1、DataSource1 三个数据库组件,设置相关的属性,使 DBGrid1 能显示表中的数据。然后,在 DBGrid1 的 onDrawColumnCell 事件中键入以下代码,然后运行程序,就可以看到神奇的结果了。本代码在 Windows98、Delphi5.0 环境下调试通过。HETLinux联盟 procedure TMainForm.DBGrid1DrawColumnCell(Sender: TObject;HETLinux联盟 const Rect: TRect; DataCol: Integer; Column: TColumn;HETLinux联盟 State: TGridDrawState);HETLinux联盟 var i :integer;HETLinux联盟 beginHETLinux联盟 if gdSelected in State then Exit;HETLinux联盟 HETLinux联盟 //定义表头的字体和背景颜色:HETLinux联盟 for i :=0 to (Sender as TDBGrid).Columns.Count-1 doHETLinux联盟 beginHETLinux联盟 (Sender as TDBGrid).Columns[i].Title.Font.Name :='宋体'; //字体HETLinux联盟 (Sender as TDBGrid).Columns[i].Title.Font.Size :=9; //字体大小HETLinux联盟 (Sender as TDBGrid).Columns[i].Title.Font.Color :=$000000ff; //字体颜色(红色)HETLinux联盟 (Sender as TDBGrid).Columns[i].Title.Color :=$0000ff00; //背景色(绿色)HETLinux联盟 end;HETLinux联盟 HETLinux联盟 //隔行改变网格背景色:HETLinux联盟 if Query1.RecNo mod 2 = 0 thenHETLinux联盟 (Sender as TDBGrid).Canvas.Brush.Color := clInfoBk //定义背景颜色HETLinux联盟 elseHETLinux联盟 (Sender as TDBGrid).Canvas.Brush.Color := RGB(191, 255, 223); //定义背景颜色HETLinux联盟 HETLinux联盟 //定义网格线的颜色:HETLinux联盟 DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);HETLinux联盟 with (Sender as TDBGrid).Canvas do //画 cell 的边框HETLinux联盟 beginHETLinux联盟 Pen.Color := $00ff0000; //定义画笔颜色(蓝色)HETLinux联盟 MoveTo(Rect.Left, Rect.Bottom); //画笔定位HETLinux联盟 LineTo(Rect.Right, Rect.Bottom); //画蓝色的横线HETLinux联盟 Pen.Color := $0000ff00; //定义画笔颜色(绿色)HETLinux联盟 MoveTo(Rect.Right, Rect.Top); //画笔定位HETLinux联盟 LineTo(Rect.Right, Rect.Bottom); //画绿色的竖线HETLinux联盟 end;HETLinux联盟 end;HETLinux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论 |
|
|
|
|
|