Home » Tipps & Tricks » Komponenten » TStringGrid » StringGrid Text vertikal ausgeben
StringGrid Text vertikal ausgeben
Folgende Methode zeigt, wie man Text in StringGrid-Zellen vertikal, also von unten nach oben, ausgeben kann. Aufgerufen wird die Methode StringGridRotateTextOut im OnDrawCell-Ereignis von TStringGrid (s. Beispielaufruf ganz unten).
//Vertikale Textausgabe in den Zellen eines StringGrid procedure StringGridRotateTextOut(Grid: TStringGrid; ARow, ACol: Integer; Rect: TRect; Schriftart: string; Size: Integer; Color: TColor; Alignment: TAlignment); var lf: TLogFont; tf: TFont; begin //wenn Schrift zu groß, dann anpassen if (Size > Grid.ColWidths[ACol] div 2) then Size := Grid.ColWidths[ACol] div 2; with Grid.Canvas do begin //Font setzen Font.Name:=Schriftart; Font.Size:=Size; Font.Color:=Color; tf:=TFont.Create; try tf.Assign(Font); GetObject(tf.Handle, sizeof(lf), @lf); lf.lfEscapement:=900; lf.lfOrientation:=0; tf.Handle:=CreateFontIndirect(lf); Font.Assign(tf); finally tf.Free; end; //Rechteck füllen FillRect(Rect); //Text nach Ausrichtung ausgeben if Alignment = taLeftJustify then TextRect(Rect, Rect.Left+2, Rect.Bottom-2, Grid.Cells[ACol,ARow]); if Alignment = taCenter then TextRect(Rect, Rect.Left+Grid.ColWidths[ACol] div 2 - Size + Size div 3, Rect.Bottom-2, Grid.Cells[ACol,ARow]); if Alignment = taRightJustify then TextRect(Rect, Rect.Right-Size - Size div 2 - 2, Rect.Bottom-2, Grid.Cells[ACol,ARow]); end; end; //Methode im OnDrawCell aufrufen procedure TForm1.StringGridDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); begin //Text um 90°gedreht ausgeben, linksbündig in der ersten Spalte if ACol = 0 then StringGridRotateTextOut(StringGrid, ARow, ACol, Rect, 'ARIAL', 12, clRed, taLeftJustify); //Ausgabe zentriert in der zweiten Spalte if ACol = 1 then StringGridRotateTextOut(StringGrid, ARow, ACol, Rect, 'ARIAL', 12, clBlue, taCenter); //Ausgabe rechtsbündig in den restlichen Spalten if ACol > 1 then StringGridRotateTextOut(StringGrid, ARow, ACol, Rect, 'ARIAL', 12, clGreen, taRightJustify); end;
Zweite Variante:
procedure StringGridRotateTextOut(Grid: TStringGrid; ARow, ACol: Integer; Rect: TRect; Schriftart: string; Size: Integer; Color: TColor; Alignment: TAlignment); var NewFont, OldFont: Integer; FontStyle, FontItalic, FontUnderline, FontStrikeout: Integer; begin //wenn Schrift zu groß dann anpassen if (Size > Grid.ColWidths[ACol] div 2) then Size := Grid.ColWidths[ACol] div 2; with Grid.Canvas do begin //Font setzen if (fsBold in Font.Style) then FontStyle := FW_BOLD else FontStyle := FW_NORMAL; if (fsItalic in Font.Style) then FontItalic := 1 else FontItalic := 0; if (fsUnderline in Font.Style) then FontUnderline := 1 else FontUnderline := 0; if (fsStrikeOut in Font.Style) then FontStrikeout:=1 else FontStrikeout:=0; Font.Color := Color; NewFont := CreateFont(Size, 0, 900, 0, FontStyle, FontItalic, FontUnderline, FontStrikeout, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, PChar(Schriftart)); OldFont := SelectObject(Handle, NewFont); //Rechteck füllen FillRect(Rect); //Text nach Ausrichtung ausgeben if Alignment = taLeftJustify then TextRect(Rect,Rect.Left+2,Rect.Bottom-2,Grid.Cells[ACol,ARow]); if Alignment = taCenter then TextRect(Rect,Rect.Left+Grid.ColWidths[ACol] div 2 - Size + Size div 3,Rect.Bottom-2,Grid.Cells[ACol,ARow]); if Alignment = taRightJustify then TextRect(Rect,Rect.Right-Size - Size div 2 -2, Rect.Bottom-2, Grid.Cells[ACol,ARow]); //Referenz auf alten Font wiederherstellen SelectObject(Handle, OldFont); //Referenz auf neuen Font löschen DeleteObject(NewFont); end; end; //Methode im OnDrawCell aufrufen procedure TForm1.StringGridDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); begin //Text um 90°gedreht ausgeben, linksbündig in der ersten Spalte if ACol = 0 then StringGridRotateTextOut(StringGrid, ARow, ACol, Rect, 'ARIAL', 12, clRed, taLeftJustify); //Ausgabe zentriert in der zweiten Spalte if ACol = 1 then StringGridRotateTextOut(StringGrid, ARow, ACol, Rect, 'ARIAL', 12, clBlue, taCenter); //Ausgabe rechtsbündig in den restlichen Spalten if ACol > 1 then StringGridRotateTextOut(StringGrid, ARow, ACol, Rect, 'ARIAL', 12, clGreen, taRightJustify); end;