Tener retornos de carro en una celda de un TStringGrid

2488 vistas

Para ello, tendremos que activar la opción goAlwaysShowEditor y codificar el evento OnKeyDown de la siguiente manera:



delphi
  1. procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
  2.   Shift: TShiftState);
  3. begin
  4.   if Key = VK_RETURN then
  5.     StringGrid1.Cells[StringGrid1.Col, StringGrid1.Row] := StringGrid1.Cells[StringGrid1.Col, StringGrid1.Row] + sLineBreak;
  6. end;



y en el OnDrawCell



delphi
  1. procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  2.   Rect: TRect; State: TGridDrawState);
  3. begin
  4.   with Sender as TStringGrid do with Canvas do
  5.   begin
  6.     { selección del color de fondo }
  7.     if gdFixed in State then Brush.Color := clBtnFace
  8.     else
  9.       if gdSelected in State then Brush.Color := clNavy
  10.       else Brush.Color := clWhite;
  11.  
  12.     { pintamos el fondo }
  13.     FillRect(Rect);
  14.  
  15.     { selección del color del texto }
  16.     if gdSelected in State then SetTextColor(Canvas.Handle,clWhite)
  17.     else SetTextColor(Canvas.Handle,clBlack);
  18.  
  19.     { pintado del texto usando la API }
  20.     DrawText(Canvas.Handle, PChar(Cells[ACol,ARow]), -1, Rect ,DT_NOPREFIX or DT_WORDBREAK );
  21.   end;
  22. end;