构造自己的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.