Delphi-PRAXiS
Seite 3 von 4     123 4      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Delphi Einfärben des Grids (https://www.delphipraxis.net/163551-einfaerben-des-grids.html)

Flash68 4. Okt 2011 12:51

AW: Einfärben des Grids
 
Liste der Anhänge anzeigen (Anzahl: 1)
Bei mir funktioniert es mit beiden möglichkeiten nicht. Wie gesagt bei meinen anderen Projekten funktioniert meine alte Möglichkeit, nur hier halt nicht. Vielleicht hab ich ja woanders noch was nicht richtig eingestellt. Es ist bei allen Grids das gleiche.

DeddyH 4. Okt 2011 12:55

AW: Einfärben des Grids
 
Das kann ich Dir leider nicht sagen. Ich selbst habe am Grid überhaupt nichts geändert, sondern nur das Ereignis behandelt.

Flash68 4. Okt 2011 13:00

AW: Einfärben des Grids
 
Ich hab auch mal ein neues Projekt eröffnet mit der gleichen DB und habe dort den gleichen Fehler.

DeddyH 4. Okt 2011 13:02

AW: Einfärben des Grids
 
Da hilft dann leider nur Durchsteppen. Ich habe da rein gefühlsmäßig RecNo in Verdacht, die solltest Du einmal überprüfen.

Sir Rufo 4. Okt 2011 20:16

AW: Einfärben des Grids
 
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 :mrgreen:

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 :mrgreen:

Flash68 5. Okt 2011 08:24

AW: Einfärben des Grids
 
das klappt gut. jetzt fehlt mir nur noch etwas um den aktuellen Cursor des Grids einzufärben.

Sir Rufo 5. Okt 2011 10:09

AW: Einfärben des Grids
 
Zitat:

Zitat von Flash68 (Beitrag 1128490)
das klappt gut. jetzt fehlt mir nur noch etwas um den aktuellen Cursor des Grids einzufärben.

Mit ein wenig Transferleistung kannst du das da selber rein basteln ;)

Schau dir an, an welcher Stelle und warum wann da was gemalt wird (und vor allem warum da manchmal doch nicht gemalt wird) ;)

Sir Rufo 6. Okt 2011 01:23

AW: Einfärben des Grids
 
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.

Flash68 6. Okt 2011 09:30

AW: Einfärben des Grids
 
Wie setzte ich denn jetzt die Unit ein?

Sir Rufo 6. Okt 2011 09:34

AW: Einfärben des Grids
 
Zitat:

Zitat von Flash68 (Beitrag 1128787)
Wie setzte ich denn jetzt die Unit ein?

öhhm, genau wie die alte, daran hat sich nichts geändert, nur dass du die Eigenschaften jetzt zusammengefasst unter der Eigenschaft GridCustomizer findest.

Delphi-Quellcode:
uses
  DBGrids, DBGrids.Customizer;

...

DBGrid1.GridCustomizer.OddRow.RowColor := clRed;
DBGrid1.GridCustomizer.OddRow.Customize := True;
DBGrid1.GridCustomizer.Customize := True;


Alle Zeitangaben in WEZ +1. Es ist jetzt 10:35 Uhr.
Seite 3 von 4     123 4      

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz