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
Antwort Antwort
Seite 3 von 4     123 4      
Flash68

Registriert seit: 24. Apr 2006
102 Beiträge
 
Delphi XE2 Architect
 
#21

AW: Einfärben des Grids

  Alt 4. Okt 2011, 12:51
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.
Miniaturansicht angehängter Grafiken
grid.png  

Geändert von Flash68 ( 4. Okt 2011 um 12:55 Uhr)
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.542 Beiträge
 
Delphi 11 Alexandria
 
#22

AW: Einfärben des Grids

  Alt 4. Okt 2011, 12:55
Das kann ich Dir leider nicht sagen. Ich selbst habe am Grid überhaupt nichts geändert, sondern nur das Ereignis behandelt.
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
Flash68

Registriert seit: 24. Apr 2006
102 Beiträge
 
Delphi XE2 Architect
 
#23

AW: Einfärben des Grids

  Alt 4. Okt 2011, 13:00
Ich hab auch mal ein neues Projekt eröffnet mit der gleichen DB und habe dort den gleichen Fehler.
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.542 Beiträge
 
Delphi 11 Alexandria
 
#24

AW: Einfärben des Grids

  Alt 4. Okt 2011, 13:02
Da hilft dann leider nur Durchsteppen. Ich habe da rein gefühlsmäßig RecNo in Verdacht, die solltest Du einmal überprüfen.
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
Benutzerbild von Sir Rufo
Sir Rufo

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

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
Flash68

Registriert seit: 24. Apr 2006
102 Beiträge
 
Delphi XE2 Architect
 
#26

AW: Einfärben des Grids

  Alt 5. Okt 2011, 08:24
das klappt gut. jetzt fehlt mir nur noch etwas um den aktuellen Cursor des Grids einzufärben.
  Mit Zitat antworten Zitat
Benutzerbild von Sir Rufo
Sir Rufo

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

AW: Einfärben des Grids

  Alt 5. Okt 2011, 10:09
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)
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
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
Flash68

Registriert seit: 24. Apr 2006
102 Beiträge
 
Delphi XE2 Architect
 
#29

AW: Einfärben des Grids

  Alt 6. Okt 2011, 09:30
Wie setzte ich denn jetzt die Unit ein?
  Mit Zitat antworten Zitat
Benutzerbild von Sir Rufo
Sir Rufo

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

AW: Einfärben des Grids

  Alt 6. Okt 2011, 09:34
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;
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
Antwort Antwort
Seite 3 von 4     123 4      


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 06:56 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