AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Einfärben des Grids

Ein Thema von Flash68 · begonnen am 4. Okt 2011 · letzter Beitrag vom 7. Okt 2011
 
Benutzerbild von Sir Rufo
Sir Rufo

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

AW: Einfärben des Grids

  Alt 6. Okt 2011, 01:23
Sodele, ich habe das doch nochmal umgerissen

Unter der Eigenschaft GridCustomizer sind dann die Steuermöglichkeiten für Even/Odd (mit/ohne HighLight) einfärben.
Mit OddCount kann man jetzt auch die Anzahl der Zeilen, nach denen die Farbe gewechselt wird, angeben.

Ich glaube ich habe jetzt auch die letzten Darstellungsfehler wegbekommen
Delphi-Quellcode:
unit DBGrids.Customize;

interface

uses
  Classes, Windows, Controls, Graphics, Grids, DBGrids;

type
  TDBGridCustomizeOptionBase = class( TControl )
  private
    fCustomized : Boolean;
    fCustomize : Boolean;
    procedure SetCustomize( const Value : Boolean );
  protected
    procedure AssignTo( Dest : TPersistent ); override;
  public
    procedure Invalidate; override;
    constructor Create( AOwner : TComponent ); override;
  published
    property Customize : Boolean
      read fCustomize
      write SetCustomize;
  end;

  TDBGridCustomizeOption = class( TDBGridCustomizeOptionBase )
  private
    fRowColor : TColor;
    fFontColorAutomatic : Boolean;
    fFontColor : TColor;
    fFontColorAuto : TColor;
    procedure SetFontColor( const Value : TColor );
    procedure SetFontColorAutomatic( const Value : Boolean );
    procedure SetRowColor( const Value : TColor );
    function GetFontColor : TColor;
  protected
    procedure AssignTo( Dest : TPersistent ); override;
  public
    constructor Create( AOwner : TComponent ); override;
  published
    property RowColor : TColor
      read fRowColor
      write SetRowColor;
    property FontColor : TColor
      read GetFontColor
      write SetFontColor;
    property FontColorAutomatic : Boolean
      read fFontColorAutomatic
      write SetFontColorAutomatic;
  end;

  TDBGridCustomizeOptions = class( TDBGridCustomizeOptionBase )
  private
    fEvenRow : TDBGridCustomizeOption;
    fOddRow : TDBGridCustomizeOption;
    fOddHilightRow : TDBGridCustomizeOption;
    fEvenHilightRow : TDBGridCustomizeOption;
    fOddCount : integer;
    function GetOddCount : integer;
    procedure SetOddCount( const Value : integer );
  protected
    procedure AssignTo( Dest : TPersistent ); override;
  public
    constructor Create( AOwner : TComponent ); override;
    function GetCustomizedColors( RowNr : integer; RowHighLight : Boolean; var ARowColor, AFontColor : TColor ) : Boolean;
  published
    property OddRow : TDBGridCustomizeOption
      read fOddRow;
    property EvenRow : TDBGridCustomizeOption
      read fEvenRow;
    property OddHilightRow : TDBGridCustomizeOption
      read fOddHilightRow;
    property EvenHilightRow : TDBGridCustomizeOption
      read fEvenHilightRow;
    property OddCount : integer
      read GetOddCount
      write SetOddCount;
  end;

  TDBGrid = class( DBGrids.TDBGrid )
  private
    fIsHighlight : Boolean;
    fGridCustomizer : TDBGridCustomizeOptions;
  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 GridCustomizer : TDBGridCustomizeOptions
      read fGridCustomizer;
  end;

function GetAutomaticFontColor( AColor : TColor ) : TColor;

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;
  fGridCustomizer := TDBGridCustomizeOptions.Create( Self );
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;
  lRowNr : integer;
  lRowColor : TColor;
  lFontColor : TColor;

  lSaveColor : TColor;
  lSaveFontColor : TColor;
begin
  if Assigned( DataLink.DataSet )
  then
    begin
      lRowColor := Canvas.Brush.Color;
      lFontColor := Canvas.Font.Color;

      if GridCustomizer.GetCustomizedColors( DataLink.DataSet.RecNo, fIsHighlight, lRowColor, lFontColor )
      then
        begin

          lSaveColor := Canvas.Brush.Color;
          lSaveFontColor := Canvas.Font.Color;

          lRect := Rect;
          lRect.Left := lRect.Left + 1;

          Canvas.Brush.Color := lRowColor;
          Canvas.FillRect( Rect );
          Canvas.Font.Color := lFontColor;

          DefaultDrawColumnCell( lRect, DataCol, Column, State );

          Canvas.Brush.Color := lSaveColor;
          Canvas.Font.Color := lSaveFontColor;

        end;
    end;
  fIsHighlight := False;
  inherited;
end;

{ TDBGridCustomizeOptions }

procedure TDBGridCustomizeOptions.AssignTo( Dest : TPersistent );
begin
  if Dest is TDBGridCustomizeOptions
  then
    with TDBGridCustomizeOptions( Dest ) do
      begin
        EvenRow.Assign( Self.EvenRow );
        EvenHilightRow.Assign( Self.EvenHilightRow );
        OddRow.Assign( Self.OddRow );
        OddHilightRow.Assign( Self.OddRow );
        OddCount := Self.OddCount;
      end;
  inherited;
end;

constructor TDBGridCustomizeOptions.Create( AOwner : TComponent );
begin
  inherited;
  fEvenRow := TDBGridCustomizeOption.Create( Self );
  fOddRow := TDBGridCustomizeOption.Create( Self );
  fEvenHilightRow := TDBGridCustomizeOption.Create( Self );
  fOddHilightRow := TDBGridCustomizeOption.Create( Self );
end;

function TDBGridCustomizeOptions.GetCustomizedColors( RowNr : integer; RowHighLight : Boolean; var ARowColor, AFontColor : TColor ) : Boolean;
var
  lCustomizer : TDBGridCustomizeOption;
begin

  Result := False;

  if not Customize
  then
    Exit;

  if ( OddCount = 0 ) or ( ( RowNr + OddCount - 1 ) mod ( OddCount * 2 ) >= OddCount )
  then
    begin

      if RowHighLight
      then
        lCustomizer := EvenHilightRow
      else
        lCustomizer := EvenRow;
    end
  else
    begin
      if RowHighLight
      then
        lCustomizer := OddHilightRow
      else
        lCustomizer := OddRow;
    end;

  if not lCustomizer.Customize
  then
    Exit;

  ARowColor := lCustomizer.RowColor;
  AFontColor := lCustomizer.FontColor;

  Result := True;

end;

function TDBGridCustomizeOptions.GetOddCount : integer;
begin
  if fOddCount < 0
  then
    fOddCount := 0;
  Result := fOddCount;
end;

procedure TDBGridCustomizeOptions.SetOddCount( const Value : integer );
begin
  if fOddCount <> Value
  then
    begin
      fOddCount := Value;
      Invalidate;
    end;
end;

{ TDBGridCustomizeOption }

procedure TDBGridCustomizeOption.AssignTo( Dest : TPersistent );
begin
  if Dest is TDBGridCustomizeOption
  then
    with TDBGridCustomizeOption( Dest ) do
      begin
        FontColor := Self.fFontColor;
        FontColorAutomatic := Self.FontColorAutomatic;
        RowColor := Self.fRowColor;
      end;
  inherited;
end;

constructor TDBGridCustomizeOption.Create( AOwner : TComponent );
begin
  inherited;
  fRowColor := clWindow;
  fFontColor := clWindowText;
  fFontColorAuto := GetAutomaticFontColor( fRowColor );
  fFontColorAutomatic := True;
end;

function TDBGridCustomizeOption.GetFontColor : TColor;
begin
  if fFontColorAutomatic
  then
    Result := fFontColorAuto
  else
    Result := fFontColor;
end;

procedure TDBGridCustomizeOption.SetFontColor( const Value : TColor );
begin
  if ( fFontColor <> Value ) and not fFontColorAutomatic
  then
    begin
      fFontColor := Value;
      Invalidate;
    end;
end;

procedure TDBGridCustomizeOption.SetFontColorAutomatic( const Value : Boolean );
begin
  if fFontColorAutomatic <> Value
  then
    begin
      fFontColorAutomatic := Value;
      Invalidate;
    end;
end;

procedure TDBGridCustomizeOption.SetRowColor( const Value : TColor );
begin
  if fRowColor <> Value
  then
    begin
      fRowColor := Value;
      fFontColorAuto := GetAutomaticFontColor( fRowColor );
      Invalidate;
    end;
end;

{ TDBGridCustomizeOptionBase }

procedure TDBGridCustomizeOptionBase.AssignTo( Dest : TPersistent );
begin
  if Dest is TDBGridCustomizeOptionBase
  then
    begin
      with TDBGridCustomizeOptionBase( Dest ) do
        begin

          Customize := Self.Customize;

        end;

    end
  else
    inherited;
end;

constructor TDBGridCustomizeOptionBase.Create( AOwner : TComponent );
begin
  inherited;
  fCustomized := False;
  fCustomize := False;
end;

procedure TDBGridCustomizeOptionBase.Invalidate;
begin
  if ( fCustomize or fCustomized )
  then
    begin
      if Assigned( Owner ) and ( Owner is TControl )
      then
        TControl( Owner ).Invalidate;
      fCustomized := fCustomize;
    end;
end;

procedure TDBGridCustomizeOptionBase.SetCustomize( const Value : Boolean );
begin
  if fCustomize <> Value
  then
    begin
      fCustomize := Value;
      Invalidate;
    end;
end;

end.
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)
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 22:35 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz