unit DBGrids.Customize;
interface
uses
Classes, Windows, Graphics, Grids, DBGrids;
type
TDBGrid =
class( DBGrids.TDBGrid )
private
fIsOdd : Boolean;
fIsHighlight : Boolean;
fOddColor : TColor;
fOddFont : TFont;
fOddColorize : Boolean;
fOddFontColorAutomatic : Boolean;
procedure SetOddColor(
const Value : TColor );
procedure SetOddColorize(
const Value : Boolean );
procedure SetOddFont(
const Value : TFont );
procedure SetOddFontColorAutomatic(
const Value : Boolean );
protected
procedure DrawColumnCell(
const Rect : TRect; DataCol : Integer; Column : TColumn; State : TGridDrawState );
override;
procedure DrawCellHighlight(
const ARect : TRect; AState : TGridDrawState; ACol : Integer; ARow : Integer );
override;
public
constructor Create( AOwner : TComponent );
override;
published
property OddColor : TColor
read fOddColor
write SetOddColor
default clWhite;
property OddFont : TFont
read fOddFont
write SetOddFont;
property OddColorize : Boolean
read fOddColorize
write SetOddColorize
default False;
property OddFontColorAutomatic : Boolean
read fOddFontColorAutomatic
write SetOddFontColorAutomatic
default False;
end;
implementation
function GetAutomaticFontColor( AColor : TColor ) : TColor;
var
lColor : TColor;
r, g, b : Byte;
begin
lColor := ColorToRGB( AColor );
r := ( lColor
shr 0 )
and $FF;
g := ( lColor
shr 8 )
and $FF;
b := ( lColor
shr 16 )
and $FF;
if ( r * 2 + g * 4 + b >= ( $FF ) * 4 )
then
Result := clBlack
else
Result := clWhite;
end;
{ TDBGrid }
constructor TDBGrid.Create( AOwner : TComponent );
begin
inherited;
fOddColor := clWhite;
fOddColorize := False;
fOddFont := TFont.Create;
fOddFont.Assign( Font );
end;
procedure TDBGrid.DrawCellHighlight(
const ARect : TRect; AState : TGridDrawState; ACol, ARow : Integer );
begin
inherited;
fIsHighlight := True;
end;
procedure TDBGrid.DrawColumnCell(
const Rect : TRect; DataCol : Integer; Column : TColumn; State : TGridDrawState );
var
lRect : TRect;
begin
if Assigned( DataLink.DataSet )
then
fIsOdd := Odd( DataLink.DataSet.RecNo );
if OddColorize
and not fIsHighlight
and fIsOdd
and ( ( State - [gdHotTrack] = [] )
or not ( Focused
or ( dgAlwaysShowSelection
in Options ) ) )
then
begin
lRect := Rect;
lRect.Left := lRect.Left + 1;
Canvas.Brush.Color := OddColor;
Canvas.FillRect( Rect );
Canvas.Font := fOddFont;
DefaultDrawColumnCell( lRect, DataCol, Column, State );
end;
fIsHighlight := False;
inherited;
end;
procedure TDBGrid.SetOddColor(
const Value : TColor );
begin
if fOddColor <> Value
then
begin
fOddColor := Value;
if fOddFontColorAutomatic
then
fOddFont.Color := GetAutomaticFontColor( fOddColor );
if fOddColorize
then
Invalidate;
end;
end;
procedure TDBGrid.SetOddColorize(
const Value : Boolean );
begin
if fOddColorize <> Value
then
begin
fOddColorize := Value;
Invalidate;
end;
end;
procedure TDBGrid.SetOddFont(
const Value : TFont );
begin
fOddFont.Assign( Value );
if fOddFontColorAutomatic
then
fOddFont.Color := GetAutomaticFontColor( fOddColor );
if fOddColorize
then
Invalidate;
end;
procedure TDBGrid.SetOddFontColorAutomatic(
const Value : Boolean );
begin
if fOddFontColorAutomatic <> Value
then
begin
fOddFontColorAutomatic := Value;
if fOddFontColorAutomatic
then
fOddFont.Color := GetAutomaticFontColor( fOddColor );
if fOddColorize
then
Invalidate;
end;
end;
end.