Recent

Author Topic: Automatically setting the height of a Memo  (Read 378 times)

seany

  • Jr. Member
  • **
  • Posts: 99
Automatically setting the height of a Memo
« on: January 27, 2023, 09:03:31 am »
As stated previously, my goal is to make a markdown renderer --> with some additional functionality, such as colors, and some groff/troff capacity.

So I have this code:
Code: Pascal  [Select][+][-]
  1. if (lineType = renderTarget) then
  2.        begin
  3.           showMessage('good to go - this will be a target to render');
  4.  
  5.           // add line
  6.  
  7.           currSection.isRendered := False;                                       // NOT YET Rendered
  8.          
  9.           // The following attributes and details need to be considered for a successful render
  10.  
  11.           // #1 The Text Content
  12.           SetLength(currSection.Text,0);                                         // TEXT ARRAY is set to empty
  13.           SetLength(currSection.Text, length(currSection.Text) + 1);             // add one element
  14.           currSection.Text[length (currSection.Text) -1 ] := currLine ;          // This is the current text to be rendered
  15.          
  16.           // #2 The Text COLOR
  17.           if (currColor = clNone) then
  18.           begin
  19.              currSection.TextColor:= colorLabel.Color;                           // have set the global color
  20.           end
  21.           else
  22.           begin
  23.              currSection.TextColor:= currColor;                                  // have set the global color
  24.           end;
  25.  
  26.  
  27.           // Document.DocumentTree.appendChild(currsection);               // Under development
  28.  
  29.           // render line
  30.           SetLength(memoList , Length(memoList) + 1);                            // increase size of render container by 1
  31.           memoList[length(memoList) -1 ]  := TMemo.Create(self);                 // new memo created
  32.  
  33.           memoList[length(memoList) -1].Parent := renderPanel;
  34.  
  35.           memoList[length(memoList) -1].Color  := currSection.TextColor;         // backgroundColor
  36.           memoList[length(memoList) -1].Left   := 10;
  37.           memoList[length(memoList) -1].Width  := renderPanel.Width - ScrollBar2.Width - memoList[length(memoList) -1].Left ;
  38.  
  39.           memoList[length(memoList) -1].Text   := currSection.Text[0];
  40.          
  41.           // memoList[length(memoList) -1].Height := NEED SOME MAGIC HERE
  42.  
  43.          
  44.        end;
  45.            

I have this text:

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

I want to find out programmatically the height of the memo that is necessary so that no scrolling would be required.  I looked for things like TMemo.textHeight, but can't find it by a simple googling. Please help. Thank you.

WooBean

  • Full Member
  • ***
  • Posts: 183
Re: Automatically setting the height of a Memo
« Reply #1 on: January 27, 2023, 09:35:39 am »
Hi,

for start try to use self.Memo1.Font.GetTextHeight('Ay') to get height of a single line and then multiply it by Memo1.Lines.Count.

Platforms: Win7/64, Linux Mint Ulyssa/64

WooBean

  • Full Member
  • ***
  • Posts: 183
Re: Automatically setting the height of a Memo
« Reply #2 on: January 31, 2023, 02:51:28 pm »
Well, seany
the goals mentioned at starting post (as coloring and formatting text) will need more capable component than TMemo is. Switch to RichMemo  (use Online Package Manager and install it in Lazarus IDE).
As far as you are trying to calculate Memo (TMemo) height needed to display some text lines you can use code below (only for Windows):
Code: Pascal  [Select][+][-]
  1. uses ... windows;
  2.  
  3. procedure TForm1.Memo1Change(Sender: TObject);
  4. var
  5.   LineHeight: Integer;
  6.   DC: HDC;
  7.   SaveFont : HFont;
  8.   Metrics : TTextMetric;
  9.   LC,dy: Integer;
  10. begin
  11.   //self.label1.caption:=inttostr(self.Memo1.Font.GetTextHeight('A'));  //=16
  12.   DC := GetDC(Memo1.Handle);
  13.   SaveFont := SelectObject(DC, Memo1.Font.Handle);
  14.   getTextMetrics(DC, Metrics);
  15.   SelectObject(DC, SaveFont);
  16.   ReleaseDC(Memo1.Handle, DC);
  17.   LineHeight := Metrics.tmHeight;  //=17
  18.   dy:=2*Metrics.tmDescent;  //=2*3
  19.   LC := Memo1.Lines.Count;
  20.   if LC < 1 then
  21.     LC := 1;
  22.   Memo1.Height := LC * LineHeight + dy;
  23. end;
  24.  

Do not give up.


« Last Edit: January 31, 2023, 02:55:18 pm by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

jamie

  • Hero Member
  • *****
  • Posts: 5315
Re: Automatically setting the height of a Memo
« Reply #3 on: January 31, 2023, 05:41:04 pm »
"Drawtext" function can give u a rectangle needed using the complete text string from the memo
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018