Determination of the real height of the RichEdit line in Delphi
-
procedure TfrmMain.btnSetHeightClick(Sender: TObject); 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]); redt.Height := 4 * Max; end;
With this code, raise the height, allow for four lines. It follows:
http://savepic.ru/10588114.jpg
Some letters on the last line Cut(Bukwa: "Ooh."r"d". You're welcome. several picsel And it's gonna be okay, but if you're gonna increase or lower the prints, it's gonna be all right. So if you have to pick up your number for every size, which is good. It has also been observed that there are signs of symbols from the next line following the last visible line.
How to measure the actual height of the lineso that each line can be completely and there are no signs of symbols from the row following the last visible (in the figure this is the fourth line)?
-
UPD: I've got the cuts rightly for Times New Roman, size 14. The total height is 6 piccells and can be easily subtracted to any type of print. Other sizes of the print were marked with an incorrect calculation of the height of the derogations.
Thank you, comrade. https://ru.stackoverflow.com/users/8766/mega For the tip. I'm adding leafing code:
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;
We'll go through the code:
Function GetLastVisibleLine determines the number of the last visible line.
At event OnShow The shapes will know the total height of the upper and lower backwards in the peaks (not stored in the local variable and not at all at the time of the shape display).
Procedure btnSetHeightClick hanging on the button and setting a new height for RichEdit Taking into account the total height of the upper and lower detachments and the number of lines (I have four lines).
Function GetCharHeight determines the current height of the symbol in RichEdit♪
This example is correctly displayed (at least I think I didn't test much) height RichEdit for a specified number of lines. When the refixing of the height is changed, it's also correct. The cuttings of the row symbols and the display of symbols from the row are not detected after the last visible.