Lazarus

Programming => LCL => Topic started by: LacaK on January 26, 2021, 07:03:28 pm

Title: TStringGrid with stretched rows heights according to content of cells
Post by: LacaK on January 26, 2021, 07:03:28 pm
I would like to show content of table like it is done in HTML pages. Rows are stretched to accomodate its content.
So table row height is set to greater height of cell in the given row.

Is there any effective way how to achieve similar behavior in any components placed on TForm?
For example using TStringGrid, where multiline content in cells is wrapped and row heights are adjusted to show whole content (without clipping)
Title: Re: TStringGrid with stretched rows heights according to content of cells
Post by: dsiders on January 26, 2021, 09:08:35 pm
I would like to show content of table like it is done in HTML pages. Rows are stretched to accomodate its content.
So table row height is set to greater height of cell in the given row.

Is there any effective way how to achieve similar behavior in any components placed on TForm?
For example using TStringGrid, where multiline content in cells is wrapped and row heights are adjusted to show whole content (without clipping)

This discussion might be of interest:

https://forum.lazarus.freepascal.org/index.php?topic=35488.0
Title: Re: TStringGrid with stretched rows heights according to content of cells
Post by: jamie on January 27, 2021, 12:38:41 am
maybe you can implement the onDrawCell event where you can adjust cell size to min requirement..
Title: Re: TStringGrid with stretched rows heights according to content of cells
Post by: LacaK on January 29, 2021, 06:59:09 am
Thank you for your suggestions.
I'm done with this so far:

Calculate height of a row:
Code: Pascal  [Select][+][-]
  1.  { TStringGridHelper }
  2.  
  3.   TStringGridHelper = class helper for TStringGrid
  4.     protected
  5.       procedure AutoSizeRow(aRow: integer);
  6.   end;
  7.  
  8. { TStringGridHelper }
  9.  
  10. procedure TStringGridHelper.AutoSizeRow(aRow: integer);
  11. var
  12.   aCanvas: TCanvas;
  13.   aCol, maxRowHeight:integer;
  14.   aText: String;
  15.   textRect: TRect;
  16. begin
  17.   aCanvas := GetWorkingCanvas(Canvas);
  18.   maxRowHeight := DefaultRowHeight;
  19.   for aCol:=0 to ColCount-1 do begin
  20.     aText := Cells[aCol,aRow];
  21.     textRect := Rect(0, 0, ColWidths[aCol], MaxInt);
  22.     DrawText(aCanvas.Handle, PChar(aText), Length(aText), textRect, DT_CALCRECT or DT_WORDBREAK);
  23.     if maxRowHeight < textRect.Height then
  24.       maxRowHeight := textRect.Height
  25.   end;
  26.   if aCanvas<>Canvas then FreeWorkingCanvas(aCanvas);
  27.   RowHeights[aRow] := maxRowHeight+2;
  28. end;          
  29.  

and then draw cells:
Code: Pascal  [Select][+][-]
  1. procedure TMyForm.MyStringGridDrawCell(Sender: TObject; aCol, aRow: Integer; aRect: TRect; aState: TGridDrawState);
  2. var
  3.   aText: String;
  4.   textStyle: TTextStyle;
  5. begin
  6.   with MyStringGrid do begin
  7.     aText := Cells[aCol,aRow];
  8.  
  9.     textStyle:=Canvas.TextStyle;
  10.     textStyle.Layout:=tlTop;
  11.     textStyle.SingleLine:=False;
  12.     textStyle.Wordbreak:=True;
  13.  
  14.     Canvas.FillRect(aRect);
  15.     Canvas.TextRect(aRect, ARect.Left+1, ARect.Top+1, aText, textStyle);
  16.   end;
  17.  

I think, that this automatic row height sizing will be useful extension to TStringGrid  8)
TinyPortal © 2005-2018