构造自己的StringGrid

构造自己的StringGrid

代码
 
   
{ ******************************************************* }
{ }
{ Module Name: unitStringGrid.pas }
{ Author Ming.z }
{ Creation Date 2010-12-29 }
{ }
{ ******************************************************* }
unit unitStringGrid;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;

type
TForm1
= class (TForm)
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
{ Private declarations }
public
{ Public declarations }
procedure InitStringGridData;
procedure InitStringGridParam;
end ;

TSGTitleEnum
= (sgtID,sgtName,sgtAge,sgtSex,sgtAddr);
TSGTitleSubRange
= sgtID..sgtAddr;
TSGRecord
= record
name:
string ;
width:Word;
end ;

const // 5 col N row
SGTitle:
array [TSGTitleEnum] of TSGRecord
// SGTitle: array [TSGTitleSubRange] of TSGRecord // same as above
= ((name: ' ID ' ;width: 40 ),
(name:
' Name ' ;width: 70 ),
(name:
' Age ' ;width: 45 ),
(name:
' Sex ' ;width: 45 ),
(name:
' Address ' ;width: 80 ));
var
Form1: TForm1;

implementation
{ $R *.dfm }

procedure TForm1.InitStringGridData;
var
i,j: Integer;
begin
with StringGrid1 do
begin
for i : = 0 to ColCount - 1 do
for j : = 1 to RowCount - 1 do
begin
case i of
Ord(sgtID):
Cells[i,j] :
= IntToStr(j);
else
Cells[i,j] :
= Format( ' %d%d ' ,[i,j]);
end ;
end ;
end ;
end ;

procedure TForm1.InitStringGridParam;
var
i: Integer;
lSG: TSGTitleSubRange;
begin
// for lSG in [Low(lSG)..High(lSG)] do // same as below
for lSG in [Low(TSGTitleSubRange)..High(TSGTitleSubRange)] do
begin
i :
= Ord(lSG);
StringGrid1.ColWidths[i] :
= SGTitle[lSG].width;
StringGrid1.Cells[i,
0 ] : = SGTitle[lSG].name;
end ;
InitStringGridData;
end ;

procedure TForm1.FormCreate(Sender: TObject);
begin
InitStringGridParam;
end ;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
with TStringGrid(sender).Canvas do
begin
if ARow = 0 then // Draw Title
begin
brush.color:
= clTeal;
FillRect(rect);
Font.Style:
= [fsBold];
Font.Size:
= 10 ;
Font.Color:
= clYellow; // Grid font color
OffsetRect(rect,
1 , 1 );
DrawText(handle,PChar(TStringGrid(sender).cells[ACol,ARow])
,length(TStringGrid(sender).cells[ACol,ARow]),rect,DT_CENTER);
end
else
case ACol of
Ord(sgtID):
begin
// brush.color: = clBtnFace; // default
brush.color:
= clSkyBlue;
FillRect(rect);
Font.Style:
= [fsBold];
Font.Size:
= 9 ;
Font.Color:
= clRed; // Grid font color
OffsetRect(rect,
1 , 1 );
DrawText(handle,PChar(TStringGrid(sender).cells[ACol,ARow])
,length(TStringGrid(sender).cells[ACol,ARow]),rect,DT_CENTER);
end ;
Ord(sgtName):
begin
brush.color:
= clBtnFace; // default
// brush.color: = clSkyBlue;
FillRect(rect);
Font.Style:
= [fsBold];
Font.Size:
= 9 ;
Font.Color:
= clRed; // Grid font color
OffsetRect(rect,
1 , 1 );
DrawText(handle,PChar(TStringGrid(sender).cells[ACol,ARow])
,length(TStringGrid(sender).cells[ACol,ARow]),rect,DT_CENTER);
end ;
else // default
begin
brush.color:
= clBtnFace; // default
// brush.color: = clSkyBlue;
FillRect(rect);
Font.Style:
= [fsBold];
Font.Size:
= 9 ;
Font.Color:
= clBlue; // Grid font color
OffsetRect(rect,
1 , 1 );
DrawText(handle,PChar(TStringGrid(sender).cells[ACol,ARow])
,length(TStringGrid(sender).cells[ACol,ARow]),rect,DT_CENTER);
end
end ;
end ;
end ;

end .

 

转载于:https://www.cnblogs.com/Jekhn/archive/2010/12/29/1920348.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值