请问怎样使RXDBGrid实现隔行改变颜色(50分)

  • 主题发起人 主题发起人 smile_jl
  • 开始时间 开始时间
S

smile_jl

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样使RXDBGrid实现隔行改变颜色
或者DBGrid也可以
谢谢先
 
定义一个全局变量:
LastColor:TColor;

DBGrid的DefaultDrawing设为false

procedure TMainForm.MailListDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
if gdSelected in State then LastColor := ClBlue
else if DataCol = 0 then
begin
if Column.Field.AsInteger = 101 then LastColor := clYellow
else if Column.Field.AsInteger = 107 then LastColor := clWhite
else LastColor := clGreen;
end;

MailList.Canvas.Brush.Color := LastColor;

MailList.canvas.textrect(rect, rect.left + 2, rect.top + 2, Column.Field.AsString);
end;
 
{*******************************************************}
{ }
{ Delphi VCL 控件库 }
{ TxRxDBGrid }
{ }
{ Copyright (c) 1997, 2000 }
{ write by XZC 2001.1.13 }
{*******************************************************}

//{$I VG.INC }
{$D-,L- }

unit xRxDBCt;

interface

uses
Windows, SysUtils, Classes,//Messages, Menus, Controls, DBCtrls, RXLookup,
Graphics, DB, RXDBCtrl;//Placemnt,

const
// scButtonClick = scAlt + VK_RIGHT;
clRowColor1 = clInfoBk;
clRowColor2 = TColor($02FFFFFF);

type
{ TxDBGrid }
TxRxDBGrid = class(TRxDBGrid)
private
FRowColorsUse: Boolean;
FRowColors: array[0..1] of TColor;
procedure SetRowColorsUse(Value: Boolean);
procedure SetRowColor(Index: Integer; Value: TColor);
protected
procedure GetCellProps(Field: TField; AFont: TFont; var Background: TColor;
Highlight: Boolean); override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure Scroll(Distance: Integer); override;
public
constructor Create(AOwner: TComponent); override;
published
property RowColorsUse: Boolean read FRowColorsUse write SetRowColorsUse default False;
property RowColor1: TColor index 0 read FRowColors[0] write SetRowColor default clRowColor1;
property RowColor2: TColor index 1 read FRowColors[1] write SetRowColor default clRowColor2;
end;

procedure Register;

implementation
//uses AppUtils, Buttons;

//const
// SmallButtonSize = 6;

{ TxDBGrid }
constructor TxRxDBGrid.Create(AOwner: TComponent);
begin
inherited;
FRowColors[0] := clRowColor1;
FRowColors[1] := clRowColor2;
end;

procedure TxRxDBGrid.GetCellProps(Field: TField; AFont: TFont; var Background: TColor;
Highlight: Boolean);
begin
if FRowColorsUse and not Highlight then
Background := FRowColors[DataLink.ActiveRecord mod 2];
inherited;
end;

procedure TxRxDBGrid.KeyDown(var Key: Word; Shift: TShiftState);
begin
inherited;
if ((Key = VK_NEXT) and not DataLink.DataSet.EOF)
or ((Key = VK_PRIOR) and not DataLink.DataSet.BOF) then
Invalidate;
end;

procedure TxRxDBGrid.Scroll(Distance: Integer);
begin
Invalidate;
inherited;
end;

procedure TxRxDBGrid.SetRowColorsUse(Value: Boolean);
begin
if FRowColorsUse <> Value then
begin
FRowColorsUse := Value;
Invalidate;
end;
end;

procedure TxRxDBGrid.SetRowColor(Index: Integer; Value: TColor);
begin
if FRowColors[Index] <> Value then
begin
FRowColors[Index] := Value;
if FRowColorsUse then Invalidate;
end;
end;

procedure Register;
begin
RegisterComponents('Data Controls', [TxRxDBGrid]);
end;

end.
 
gong666:
请问DataCol是什么,我试了一下,并不是隔行换色,能不能解释一下你的思路
WiseAnt:
在这段程序里没能看出SetRowColor的实现机理,能不能进一步给出解释

谢谢两位了
 
把paintoptions属性中的alternatingRowColor设置一个颜色(跟color属性中的颜色不同)
这样就可以使TwwDBGRID隔行显示不同颜色了
推荐设置alternatingRowcolor=clSkyBlue
 
TwwDBGRID在哪可以下载?
 
DBGrid :

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if ADOQuery1.RecNo mod 2 = 1 then
dbgrid1.Canvas.Brush.Color := clRed ;
else
dbgrid1.Canvas.Brush.Color := clWhite;
dbgrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); //画DBGrid
end;
 

Similar threads

回复
0
查看
978
不得闲
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
后退
顶部