Advertisement
raava403

memoAutoSizeHeight Delphi

Aug 21st, 2019
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.18 KB | None | 0 0
  1. {
  2. Usage example: place this code in your TMemo onChange event
  3.   memoAutoSizeHeight(Memo1, Form1.Canvas, 277);
  4. Note: 277 is the preferred max size of the TMemo height, change on your needs
  5. Reference: http://www.delphigroups.info/2/59/101367.html
  6. }
  7.  
  8. procedure memoAutoSizeHeight(memo: TMemo; formCanvas: TCanvas; maxHeight: Integer);
  9. var
  10.   rectA, rectB: TRect;
  11.   s: String;
  12.   saveFont: TFont;
  13.   heightResult: Integer;
  14. begin
  15.   try
  16.     s:=memo.Text;
  17.     memo.Perform(EM_GETRECT, 0, LongInt(@rectA));
  18.     rectB:=rectA;
  19.     saveFont:=TFont.Create;
  20.     try
  21.       saveFont.Assign(formCanvas.Font);
  22.       formCanvas.Font:=memo.Font;
  23.       DrawTextEx(formCanvas.Handle, pChar(S), Length(S), rectB,
  24.         DT_CALCRECT or DT_EDITCONTROL or DT_WORDBREAK or DT_NOPREFIX, nil);
  25.     finally
  26.       formCanvas.Font:=saveFont;
  27.       saveFont.Free;
  28.     end;
  29.     heightResult:=memo.Height+rectB.Bottom-rectA.Bottom;
  30.     if Copy(memo.Text, Length(memo.Text)-1, 1)=#13 then
  31.       heightResult:=heightResult+memo.Font.Height;
  32.     if (heightResult>=maxHeight) or (heightResult-memo.Font.Height>=maxHeight) then
  33.       heightResult:=maxHeight;
  34.   finally
  35.     memo.Height:=heightResult;
  36.   end;
  37. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement