坚
坚持信念
Unregistered / Unconfirmed
GUEST, unregistred user!
unit Test;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, Grids, DB, ADODB, BaseGrid, AdvGrid;
type
TCellColor = class
Color: TColor;
Text: string;
end; // 单元格颜色类
TfrmTest = class(TForm)
StringGrid1: TStringGrid;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
{ Private declarations }
procedure InitGrid(AStringGrid: TStringGrid);
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;
var
frmTest: TfrmTest;
implementation
{$R *.dfm}
procedure TfrmTest.StringGrid1DrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
var
CellColor: TCellColor;
begin
CellColor := TCellColor(StringGrid1.Objects[ACol, ARow]);
if Assigned(CellColor) then begin
with TStringGrid(Sender).Canvas do begin
Brush.Color := CellColor.Color;
FillRect(Rect);
TextRect(Rect, Rect.Left + 2, Rect.Top + 2, CellColor.Text);
end;
end;
// DrawText(StringGrid1.Canvas.Handle,pchar(StringGrid1.Cells[Acol,Arow]),Length(StringGrid1.Cells[Acol,Arow]),Rect,DT_WORDBREAK or DT_LEFT);
end;
constructor TfrmTest.Create(AOwner: TComponent);
begin
inherited;
InitGrid(StringGrid1);
end;
destructor TfrmTest.Destroy;
var
I, J: Integer;
begin
for I := 0 to StringGrid1.RowCount do begin
for J := 0 to StringGrid1.ColCount do begin
TCellColor(StringGrid1.Objects[J, I]).Free;
end;
end;
inherited;
end;
procedure TfrmTest.InitGrid(AStringGrid: TStringGrid);
var
i,j:integer;
CellColor: TCellColor;
begin
i:=0;
j:=0;
while not ADOQuery1.Eof do
begin
AStringGrid.Cells[i,j]:=ADOQuery1.FieldByName('编号').AsString+ADOQuery1.FieldByName('房台名称').AsString;
CellColor := TCellColor.Create;
if ADOQuery1['状态']='空闲' then
begin
CellColor.Color := clred;
end;
if ADOQuery1['状态']='营业' then
begin
CellColor.Color := clblue;
end;
CellColor.Text := AStringGrid.Cells[i, j];
AStringGrid.Objects[i, j] := CellColor;
if i<4 then
inc(i)
else begin
i:=0;
inc(j);
end;
ADOQuery1.Next;
end;
StringGrid1.RowCount := j+1;
StringGrid1.ColCount := 5;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, Grids, DB, ADODB, BaseGrid, AdvGrid;
type
TCellColor = class
Color: TColor;
Text: string;
end; // 单元格颜色类
TfrmTest = class(TForm)
StringGrid1: TStringGrid;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
{ Private declarations }
procedure InitGrid(AStringGrid: TStringGrid);
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;
var
frmTest: TfrmTest;
implementation
{$R *.dfm}
procedure TfrmTest.StringGrid1DrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
var
CellColor: TCellColor;
begin
CellColor := TCellColor(StringGrid1.Objects[ACol, ARow]);
if Assigned(CellColor) then begin
with TStringGrid(Sender).Canvas do begin
Brush.Color := CellColor.Color;
FillRect(Rect);
TextRect(Rect, Rect.Left + 2, Rect.Top + 2, CellColor.Text);
end;
end;
// DrawText(StringGrid1.Canvas.Handle,pchar(StringGrid1.Cells[Acol,Arow]),Length(StringGrid1.Cells[Acol,Arow]),Rect,DT_WORDBREAK or DT_LEFT);
end;
constructor TfrmTest.Create(AOwner: TComponent);
begin
inherited;
InitGrid(StringGrid1);
end;
destructor TfrmTest.Destroy;
var
I, J: Integer;
begin
for I := 0 to StringGrid1.RowCount do begin
for J := 0 to StringGrid1.ColCount do begin
TCellColor(StringGrid1.Objects[J, I]).Free;
end;
end;
inherited;
end;
procedure TfrmTest.InitGrid(AStringGrid: TStringGrid);
var
i,j:integer;
CellColor: TCellColor;
begin
i:=0;
j:=0;
while not ADOQuery1.Eof do
begin
AStringGrid.Cells[i,j]:=ADOQuery1.FieldByName('编号').AsString+ADOQuery1.FieldByName('房台名称').AsString;
CellColor := TCellColor.Create;
if ADOQuery1['状态']='空闲' then
begin
CellColor.Color := clred;
end;
if ADOQuery1['状态']='营业' then
begin
CellColor.Color := clblue;
end;
CellColor.Text := AStringGrid.Cells[i, j];
AStringGrid.Objects[i, j] := CellColor;
if i<4 then
inc(i)
else begin
i:=0;
inc(j);
end;
ADOQuery1.Next;
end;
StringGrid1.RowCount := j+1;
StringGrid1.ColCount := 5;
end;
end.