Thema: Delphi Einfärben des Grids

Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#7

AW: Einfärben des Grids

  Alt 4. Okt 2011, 20:16
Also meine Empfehlung wäre hierbei (das wird ja wohl andauernd benötigt, und müllt einem ja nur den Form-Quelltext zu) eine kleine Ableitung vorzunehmen, die auch gar nicht wehtut

Vorneweg, wie man das benutzt:

Ganz, wichtig, diese Unit muss nach der Unit DBGrids eingebunden werden!
Delphi-Quellcode:
unit view.Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, StdCtrls, ExtCtrls,
  // Customizers at the end
  DBGrids.Customize; // <<<--- Das macht die ganze Magie

type
  TForm1 = class( TForm )
    DBGrid1 : TDBGrid;
    procedure FormCreate( Sender : TObject );
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1 : TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate( Sender : TObject );
begin
  DBGrid1.OddColor := clMoneyGreen;
  DBGrid1.OddColorize := True;
end;

end.
fettich, nur noch die Datenquelle dranhauen und schon wird alles bunt.

Delphi-Quellcode:
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.
EDIT: Da gab es ein paar Situationen (MultiSelect, etc.) wo das noch nicht ganz funktioniert hat, jetzt ist aber alles schick
EDIT2: So, jetzt sollte das wirklich schick sein
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)

Geändert von Sir Rufo ( 4. Okt 2011 um 23:59 Uhr)
  Mit Zitat antworten Zitat