TStringGrid Text in Zelle rechtsbündig

Für Fragen von Einsteigern und Programmieranfängern...
Antworten
Lubus
Beiträge: 26
Registriert: Mi 17. Feb 2010, 17:07
OS, Lazarus, FPC: x86_64-linux-gtk2
CPU-Target: AMD Ryzen 7 5700G
Wohnort: Lendsiedel

TStringGrid Text in Zelle rechtsbündig

Beitrag von Lubus »

In StringGrid1 möchte ich einer Zelle den String rechtsbündig ausrichten.
So soll ein Text einer Zelle
StringGrid1.Cells[1,1] :='Text 1';
rechtsbündig ausgegeben werden.
Mit canvas habe ich festgestellt, kann ich auf jeder Position des Grids Text ausgeben, also nicht in der Zelle selbst.
Es muss was geben mit
StringGrid1.Cells[1,1]. ...
Wer weiß wie das geht?

wp_xyz
Beiträge: 4869
Registriert: Fr 8. Apr 2011, 09:01

Re: TStringGrid Text in Zelle rechtsbündig

Beitrag von wp_xyz »

Der Zell-Text wird mit der Canvas-Methode TextRect geschrieben, in der die Einstellungen des TextStyle-Record berücksichtigt werden, und dieser Record hat ein Feld Aligment für links-/rechts-bündige oder mittige Ausgabe. Wenn du also Canvas.TextStyle.Alignment für die gewünschte Zelle auf taRightJustify setzt, hast du rechtsbündigen Text. Die richtige Stelle dafür ist das OnPrepareCanvas-Ereignis des Grid, das unmittelbar vor der Zelldarstellung aufgerufen wird.

Code: Alles auswählen

procedure TForm1.StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer; aState: TGridDrawState);   
var
  ts: TTextStyle;
begin
  if (ACol = 2) and (ARow > 0) then  // angenommen, alle Zellen in Spalte 2 sollen rechtsbündig gesetzt werden, außer der Titelzelle
  begin
    ts := StringGrid1.Canvas.TextStyle;
    ts.Alignment := taRightJustify;
    StringGrid1.Canvas.Textstyle := ts;
  end;
end;
Das Setzen der TTextStyle-Recordelemente ist etwas umständiglich: du musst den aktuellen TextStyle des Grid.Canvas in eine Zwischenvariable kopieren, dort das gewünschte Element ändern und dann den ganzen Record wieder nach Grid.Canvas zurückschreiben.

TTextStyle ist ganz interessant und ermöglicht viele Einstellungen, die im Detail sonst recht umständlich zu erzielen sind. Hier die komplette Deklaration von TTextStyle aus der Unit Graphics:

Code: Alles auswählen

type
  TTextLayout = (tlTop, tlCenter, tlBottom);
  TTextStyle = packed record
    Alignment : TAlignment;  // TextRect Only: horizontal alignment

    Layout    : TTextLayout; // TextRect Only: vertical alignment

    SingleLine: boolean;     // If WordBreak is false then process #13, #10 as
                             // standard chars and perform no Line breaking.

    Clipping  : boolean;     // TextRect Only: Clip Text to passed Rectangle

    ExpandTabs: boolean;     // Replace #9 by apropriate amount of spaces (default is usually 8)

    ShowPrefix: boolean;     // TextRect Only: Process first single '&' per
                             //    line as an underscore and draw '&&' as '&'

    Wordbreak : boolean;     // TextRect Only: If line of text is too long
                             //    too fit between left and right boundaries
                             //    try to break into multiple lines between
                             //    words
                             //    See also EndEllipsis.

    Opaque    : boolean;     // TextRect: Fills background with current Brush
                             // TextOut : Fills background with current
                             //            foreground color

    SystemFont: Boolean;     // Use the system font instead of Canvas Font
    
    RightToLeft: Boolean;    //For RightToLeft text reading (Text Direction)

    EndEllipsis: Boolean;    // TextRect Only: If line of text is too long
                             //    to fit between left and right boundaries
                             //    truncates the text and adds "..."
                             //    If Wordbreak is set as well, Workbreak will
                             //    dominate.
  end;

Eb
Lazarusforum e. V.
Beiträge: 238
Registriert: Di 5. Feb 2008, 15:32
OS, Lazarus, FPC: Linux Mint - Laz 2.2.0
CPU-Target: 64Bit
Wohnort: Stuttgart

Re: TStringGrid Text in Zelle rechtsbündig

Beitrag von Eb »

Vielen Dank für die perfekte Erklärung!

Antworten