UPD: The indentation sizes I had correctly calculated for the 14th size Times New Roman font. The total height is 6 pixels, you can safely substitute for calculations on any font size. On other font sizes, incorrect calculation of the height of indents is noticed.
Thanks to Comrade Mega for the tip. I enclose a code listing:
function TfrmMain.GetLastVisibleLine(RichEdit: TRichEdit): Integer; const EM_EXLINEFROMCHAR = WM_USER + 54; var r: TRect; i: Integer; begin { The EM_GETRECT message retrieves the formatting rectangle of an edit control. } RichEdit.Perform(EM_GETRECT, 0, Longint(@r)); r.Left := r.Left + 1; r.Top := r.Bottom - 2; { The EM_CHARFROMPOS message retrieves information about the character closest to a specified point in the client area of an edit control } i := RichEdit.Perform(EM_CHARFROMPOS, 0, Integer(@r.topleft)); { The EM_EXLINEFROMCHAR message determines which line contains the specified character in a rich edit control } Result := RichEdit.Perform(EM_EXLINEFROMCHAR, 0, i); end; procedure TfrmMain.FormShow(Sender: TObject); var r: TRect; begin redt.Lines.LoadFromFile('text.txt'); r.TopLeft.X := 0; r.TopLeft.Y := 0; r.BottomRight.X := redt.ClientWidth; r.BottomRight.Y := redt.ClientHeight; AdjustWindowRect(r, WS_BORDER, False); BordersHeight := r.BottomRight.Y - (GetLastVisibleLine(redt) * GetCharHeight); end; procedure TfrmMain.btnSetHeightClick(Sender: TObject); var CharHeight: Integer; begin Canvas.Font.Assign(redt.Font); CharHeight := GetCharHeight; redt.Height := (4 * CharHeight) + BorderHeight; end; procedure TfrmMain.btnSetFontClick(Sender: TObject); begin if dlgFont.Execute then redt.Font.Assign(dlgFont.Font); end; function TfrmMain.GetCharHeight: Integer; var i, Max: Integer; begin Max := 0; Canvas.Font.Assign(redt.Font); for i := 0 to Length(redt.Text) do if Max < Canvas.TextHeight(redt.Text[i + 1])then Max := Canvas.TextHeight(redt.Text[i + 1]); Result := Max; end;
Go through the code:
The GetLastVisibleLine function determines the number of the last visible line.
At the OnShow event of the form, we know the total height of the upper and lower indents in pixels (we do not store it in a local variable and generally do not change the value while the form is being displayed).
The btnSetHeightClick procedure hangs on the button and sets a new height for RichEdit , taking into account the total height of the upper and lower indents and the number of lines (I have four lines).
The GetCharHeight function determines the current height of a character in RichEdit .
This example correctly exposes (at least I think so, I have not tested much) the height of RichEdit for the specified number of rows. When changing the font, the height recalculation is also correct. No truncation of the characters of the string and the display of the symbols of the string following the last visible line were found.
AdjustWindowRect. - mega