![]() |
StringGrids farbig darstellen
Liste der Anhänge anzeigen (Anzahl: 1)
Hai,
da es immer wieder gefragt wird habe ich hier mal einen "kleinen" Democode um zu zeigen wie man die Zellen eines TStringGrid einfärben kann oder den Text formatiert. Das ganze passiert einfach im ![]()
Delphi-Quellcode:
Ein Mini-Demo-Projekt hänge ich auch mal an den Beitrag:
procedure TDemoForm.StringGrid1DrawCell(Sender: TObject; ACol, ARow: integer;
Rect: TRect; State: TGridDrawState); var celltext: string; myDrawRect: TRect; cellHeight: integer; cellWidth: integer; monocolor: boolean; textformat: cardinal; begin textformat := DT_SINGLELINE; with (Sender as TStringGrid) do begin celltext := Cells[ACol, ARow]; cellHeight := RowHeights[ARow]; // Die aktuelle Zellenhöhe cellWidth := ColWidths[aCol]; // Die aktuelle Zellenbreite monocolor := True; // Standardmässig sind die Zellen einfarbig if (ACol = 1) and (ARow = 1) then // Nur für Celle 1:1 begin canvas.Brush.Color := clred; // Hintergrundfarbe canvas.Font.Color := clblue; // Schriftfarbe end; if (ARow = 3) then // Für die dritte Zeile begin textformat := textformat or DT_CENTER or DT_VCENTER; // Text wird zentriert canvas.Font.Style := canvas.Font.Style + [fsBold]; // Fett end; if (ACol = 2) and (ARow > 0) then // Die zweite Spalte begin canvas.Brush.Color := clGreen; end; if (ARow = 4) and (ACol > 0) then // Hier ist es horizontal zweifarbig begin monocolor := False; myDrawRect := Rect; Canvas.Brush.Color := clRed; // Obere Farbe myDrawRect.Bottom := myDrawRect.Bottom - (cellHeight div 2); // Oberes rechteck berechnen Canvas.FillRect(myDrawRect); // Hintergrund für die obere Hälfe zeichnen Canvas.Brush.Color := clBlue; // untere Farbe myDrawRect.Top := myDrawRect.Top + (cellHeight div 2); myDrawRect.Bottom := Rect.Bottom; Canvas.FillRect(myDrawRect); // Hintergrund für die untere Hälfe zeichnen end; if (ARow > 0) and (ACol = 4) then // Hier ist es vertikal zweifarbig begin monocolor := False; myDrawRect := Rect; Canvas.Brush.Color := clLime; // Linke Farbe myDrawRect.Right := myDrawRect.Right - (cellWidth div 2); // Oberes rechteck berechnen Canvas.FillRect(myDrawRect); // Hintergrund für die obere Hälfe zeichnen Canvas.Brush.Color := clMaroon; // Rechte Farbe myDrawRect.Left := myDrawRect.Left + (cellWidth div 2); myDrawRect.Right := Rect.Right; Canvas.FillRect(myDrawRect); // Hintergrund für die untere Hälfe zeichnen end; if (ARow = 5) then begin textformat := textformat or DT_BOTTOM or DT_RIGHT; end; if (monocolor) then begin SetBkMode(StringGrid1.Canvas.Handle, OPAQUE); // Den Hintergrund überschreiben Canvas.FillRect(Rect) end else begin SetBkMode(StringGrid1.Canvas.Handle, TRANSPARENT); // Den Hintergrund nich überschreiben end; // Hier wird nun der Text ausgegeben DrawText(Canvas.Handle, PChar(celltext), Length(celltext), Rect, textformat); // Textausgeben // Optional kann der Text auch Zentriert werden. // dann mit diesem Parameter; // DT_SINGLELINE or DT_CENTER or DT_VCENTER // Für Rechtsbündige Ausgabe // DT_SINGLELINE or DT_RIGHT end; end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:41 Uhr. |
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 by Thomas Breitkreuz