Text im StringGrid ausrichten |
|
| System | Win9x, WinNT, Win2000, WinXP, Vista, Win7 |
|---|---|
| Ab Delphi-Version | Delphi 1 |
| Letzte Änderung | 30.11.2011 |
Folgende Methode zeigt, wie man Text in StringGrid-Zellen linksbündig, zentriert und rechtsbündig sowohl mit als auch ohne Zeilenumbruch ausgeben kann. Aufgerufen wird die Methode im OnDrawCell-Ereignis von TStringGrid (s. Beispielaufruf ganz unten).
procedure StringGridAlignment(Grid: TStringGrid; Rect: TRect; ACol, ARow: Integer;
Alignment: TAlignment; LineBreak: Boolean);
var
TextOut: string;
begin
Grid.Canvas.FillRect(Rect);
TextOut := Grid.Cells[ACol,ARow];
if LineBreak = false then
begin
if Alignment = taLeftJustify then
DrawText(Grid.Canvas.Handle, PChar(TextOut), StrLen(PChar(TextOut)), Rect,DT_LEFT);
if Alignment = taCenter then
DrawText(Grid.Canvas.Handle, PChar(TextOut), StrLen(PChar(TextOut)), Rect,DT_CENTER);
if Alignment = taRightJustify then
DrawText(Grid.Canvas.Handle, PChar(TextOut), StrLen(PChar(TextOut)), Rect,DT_RIGHT);
end
else
begin
if Alignment = taLeftJustify then
DrawText(Grid.Canvas.Handle, PChar(TextOut), StrLen(PChar(TextOut)),
Rect,DT_LEFT+DT_WORDBREAK);
if Alignment = taCenter then
DrawText(Grid.Canvas.Handle, PChar(TextOut), StrLen(PChar(TextOut)), Rect,
DT_CENTER+DT_WORDBREAK);
if Alignment = taRightJustify then
DrawText(Grid.Canvas.Handle, PChar(TextOut), StrLen(PChar(TextOut)), Rect,
DT_RIGHT+DT_WORDBREAK);
end;
end;
//Methode im OnDrawCell aufrufen
procedure TForm1.StringGridDrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
//Text linksbündig ohne Zeilenumbruch in der ersten Zeile
if ARow = 0 then
StringGridAlignment(StringGrid, Rect, ACol, ARow, taLeftJustify, False);
//Text zentriert mit Zeilenumbruch in der zweiten Zeile
if ARow = 1 then
StringGridAlignment(StringGrid, Rect, ACol, ARow, taCenter, True);
//Text rechtsbündig ohne Zeilenumbruch restliche Zeilen
if ARow > 1 then
StringGridAlignment(StringGrid, Rect, ACol, ARow, taRightJustify, False);
end;
Alignment: TAlignment; LineBreak: Boolean);
var
TextOut: string;
begin
Grid.Canvas.FillRect(Rect);
TextOut := Grid.Cells[ACol,ARow];
if LineBreak = false then
begin
if Alignment = taLeftJustify then
DrawText(Grid.Canvas.Handle, PChar(TextOut), StrLen(PChar(TextOut)), Rect,DT_LEFT);
if Alignment = taCenter then
DrawText(Grid.Canvas.Handle, PChar(TextOut), StrLen(PChar(TextOut)), Rect,DT_CENTER);
if Alignment = taRightJustify then
DrawText(Grid.Canvas.Handle, PChar(TextOut), StrLen(PChar(TextOut)), Rect,DT_RIGHT);
end
else
begin
if Alignment = taLeftJustify then
DrawText(Grid.Canvas.Handle, PChar(TextOut), StrLen(PChar(TextOut)),
Rect,DT_LEFT+DT_WORDBREAK);
if Alignment = taCenter then
DrawText(Grid.Canvas.Handle, PChar(TextOut), StrLen(PChar(TextOut)), Rect,
DT_CENTER+DT_WORDBREAK);
if Alignment = taRightJustify then
DrawText(Grid.Canvas.Handle, PChar(TextOut), StrLen(PChar(TextOut)), Rect,
DT_RIGHT+DT_WORDBREAK);
end;
end;
//Methode im OnDrawCell aufrufen
procedure TForm1.StringGridDrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
//Text linksbündig ohne Zeilenumbruch in der ersten Zeile
if ARow = 0 then
StringGridAlignment(StringGrid, Rect, ACol, ARow, taLeftJustify, False);
//Text zentriert mit Zeilenumbruch in der zweiten Zeile
if ARow = 1 then
StringGridAlignment(StringGrid, Rect, ACol, ARow, taCenter, True);
//Text rechtsbündig ohne Zeilenumbruch restliche Zeilen
if ARow > 1 then
StringGridAlignment(StringGrid, Rect, ACol, ARow, taRightJustify, False);
end;
Ähnliche Seiten:
- StringGrid Text vertikal ausgeben
- Stringgrid-Zelle per Programmcode selektieren
- Einträge in einer Listbox markieren
- Schreibschutz bei bestimmten Zellen eines TStringgrid
- Zeilen eines Stringgrids färben
- Trennlinie im Stringgrid hervorheben
- Äquivalent zu ColSpan in HTML in TStringGrid
- Zeile eines StringGrids löschen
- Zeilen in einem StringGrid tauschen
- Bitmap in ein StringGrid zeichnen