Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- Usage example: place this code in your TMemo onChange event
- memoAutoSizeHeight(Memo1, Form1.Canvas, 277);
- Note: 277 is the preferred max size of the TMemo height, change on your needs
- Reference: http://www.delphigroups.info/2/59/101367.html
- }
- procedure memoAutoSizeHeight(memo: TMemo; formCanvas: TCanvas; maxHeight: Integer);
- var
- rectA, rectB: TRect;
- s: String;
- saveFont: TFont;
- heightResult: Integer;
- begin
- try
- s:=memo.Text;
- memo.Perform(EM_GETRECT, 0, LongInt(@rectA));
- rectB:=rectA;
- saveFont:=TFont.Create;
- try
- saveFont.Assign(formCanvas.Font);
- formCanvas.Font:=memo.Font;
- DrawTextEx(formCanvas.Handle, pChar(S), Length(S), rectB,
- DT_CALCRECT or DT_EDITCONTROL or DT_WORDBREAK or DT_NOPREFIX, nil);
- finally
- formCanvas.Font:=saveFont;
- saveFont.Free;
- end;
- heightResult:=memo.Height+rectB.Bottom-rectA.Bottom;
- if Copy(memo.Text, Length(memo.Text)-1, 1)=#13 then
- heightResult:=heightResult+memo.Font.Height;
- if (heightResult>=maxHeight) or (heightResult-memo.Font.Height>=maxHeight) then
- heightResult:=maxHeight;
- finally
- memo.Height:=heightResult;
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement