Napravio sam custom kontrolu koja naslijeđuje TDBGrid. Ova moja bi trebala imati svojstva svijetlije, tamnije boje kolone i prikazivanja (boolean) za dbgrid obojan u "zebru".
No prema ovome jednostavno neće. Instaliram kontrolu, ona uredno radi kao običan dbgrid, vide se svojstva u inspectoru, ali ne farba grid. Jesam li negdje promašio u radu s eventima, vjerojatno jesam?
unit B8DBGrid;
interface
uses
SysUtils, Classes, Controls, Grids, DBGrids, Types, Graphics;
type
TZebra = Array [ boolean ] of TColor;
type
TB8DBGrid = class( TDBGrid )
private
fZebra: TZebra;
fZebraDark: TColor;
fZebraLight: TColor;
fShowZebra: boolean;
fOnDrawColumnCell: TDrawColumnCellEvent;
function getShowZebra: boolean;
procedure setShowZebra( const Value: boolean );
function getZebraDark: TColor;
function getZebraLight: TColor;
procedure setZebraDark( const Value: TColor );
procedure setZebraLight( const Value: TColor );
protected
procedure DrawColumnCell( Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState );
public
published
property ColorZebraLight: TColor read getZebraLight write setZebraLight;
property ColorZebraDark: TColor read getZebraDark write setZebraDark;
property ShowZebra: boolean read getShowZebra write setShowZebra Default True;
property ondrawcolumncell : TDrawColumnCellEvent read fOnDrawColumnCell write fOnDrawColumnCell;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents( '8Bita', [ TB8DBGrid ] );
end;
{ T8BDBGrid }
function TB8DBGrid.getShowZebra: boolean;
begin
Result := fShowZebra;
end;
function TB8DBGrid.getZebraDark: TColor;
begin
Result := fZebraDark;
end;
function TB8DBGrid.getZebraLight: TColor;
begin
Result := fZebraLight;
end;
procedure TB8DBGrid.DrawColumnCell( Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState );
VAR
ColOdd: boolean;
BEGIN
TRY
ColOdd := ( ( Sender AS TDBGrid ).DataSource.DataSet.RecNo MOD 2 <> 0 );
fZebra[ False ] := fZebraLight;
fZebra[ True ] := fZebraDark;
IF ( gdSelected IN State ) THEN
BEGIN
( Sender AS TDBGrid ).Canvas.Brush.Color := clHighlight;
( Sender AS TDBGrid ).Canvas.Font.Color := clHighlightText;
END
ELSE
IF fShowZebra then
( Sender AS TDBGrid ).Canvas.Brush.Color := fZebra[ ColOdd ];
( Sender AS TDBGrid ).DefaultDrawColumnCell( Rect, DataCol, Column, State );
EXCEPT
END;
end;
procedure TB8DBGrid.setShowZebra( const Value: boolean );
begin
fShowZebra := Value;
end;
procedure TB8DBGrid.setZebraDark( const Value: TColor );
begin
fZebraDark := Value;
end;
procedure TB8DBGrid.setZebraLight( const Value: TColor );
begin
fZebraLight := Value;
end;
end.