Thema: Delphi StringGrid Mehrzeilig

Einzelnen Beitrag anzeigen

Robert_G
(Gast)

n/a Beiträge
 
#11

Re: StringGrid Mehrzeilig

  Alt 31. Mär 2004, 17:45
Ich hätte es so gelöst (im OnDrawCell des Grids) :
Delphi-Quellcode:
Var
  CrLfPos, PrevPos, i : Integer;
  SL : TStrings;
Begin
  With Sender As TStringGrid Do
  Begin

    If aCol < FixedCols Then Exit;
    If aRow < FixedRows Then Exit;

    PrevPos := 1;
    CrLfPos := PosEx(#13#10, Cells[aCol, aRow], PrevPos);
    If CrLfPos > 0 Then
    Begin
      SL := TStringList.Create;
      Try
        // such Zeilenumbrüche
        While CrLfPos > 0 Do
        Begin
          // rein in die SL
          SL.Add(Copy(Cells[aCol, aRow], PrevPos, CrLfPos - PrevPos));
          PrevPos := CrLfPos + 2;
          CrLfPos := PosEx(#13#10, Cells[aCol, aRow], PrevPos);
        End;

        If SL.Count > 0 Then
          // Canvas des Grids
          With Canvas Do
          Begin
            SL.Add(Copy(Cells[aCol, aRow], PrevPos, Length(Cells[aCol, aRow])));
            FillRect(Rect);
            For i := 0 To pred(SL.Count) Do
              // Zeichne Text (-Font.Height +2 entspricht einer Zeilenhöhe)
              TextOut(Rect.Left + 2, Rect.Top + 2 + ((-Font.Height + 2) * i), Sl[i]);
          End;

      Finally
        FreeAndNil(SL);
      End;
    End;
  End;
End;
Nachtrag: Ich habe es noch ausgebessert, in der vorherigen Variante wurde immer die SL erzeugt...
  Mit Zitat antworten Zitat