sábado, 26 de fevereiro de 2011

Delphi - Stringrid multilinha

Código para StringGrid multilinha - quebrar o texto em linhas.

Depois, basta adicionar o + #13 entre o texto que você quer quebrar.

procedure TForm1.StringGrid1DrawCell(Sender: TObject;
Col, Row: Longint;
Rect: TRect;
State: TGridDrawState);

var
Line1: string;
Line2: string;
ptr: integer;
padding: integer;
begin

If (ACol > 0) and (ARow >0) Then
Begin
ptr := Pos(';', StringGrid1.Cells[ACol, ARow]);
if ptr > 0 then
begin
Line1 := Copy(StringGrid1.Cells[ACol, ARow], 1, ptr - 1);
Line2 := Copy(StringGrid1.Cells[ACol, ARow], ptr + 1,
Length(StringGrid1.Cells[ACol,ARow]) - ptr);
end
else
Line1 := StringGrid1.Cells[ACol, ARow];

StringGrid1.Canvas.FillRect(Rect);
StringGrid1.Canvas.TextOut(Rect.Left, Rect.Top + 2, Line1);
if ptr > 0 then
StringGrid1.Canvas.TextOut(Rect.Left, Rect.Top -
StringGrid1.Canvas.Font.Height + 3, Line2);

End;

2 comentários:

Unknown disse...

Está confuso, tanto o título quanto o conteúdo.
Um Stringgrid naturalmente é multilinha

Unknown disse...

Funcionou legal. Obrigado pela dica.
Meu código ficou assim:
var Line1, Line2: string;
begin
Length(SGridPlanilha.Cells[aCol, aRow]), Rect, DT_WORDBREAK);
If (ACol > 2) and (ARow = 0) Then
Begin
Line1 := 'Entrada';
Line2 := 'Saída';
SGridPlanilha.Canvas.FillRect(Rect);
SGridPlanilha.Canvas.TextOut(Rect.Left+4, Rect.Top + 2, Line1);
SGridPlanilha.Canvas.TextOut(Rect.Left+4, Rect.Top - SGridPlanilha.Canvas.Font.Height + 3, Line2);
end;