julianpointer
asked on
Resize a richedit control
I want to resize a richedit control so that all the text in the control is shown. ie as you type in the control it automatically increases/descreses size.
someting like richedit1.Height := round(richedit1.Lines.Coun t * 13.5);
this dosn't work properly, I need to get the logical client area ?
someting like richedit1.Height := round(richedit1.Lines.Coun
this dosn't work properly, I need to get the logical client area ?
Also, if you're bothered with auto-scroll on Enter,
this is improved version that takes care of it:
var OldLineCount: Integer; // global var
(....)
procedure TForm1.ResizeRichEdit(VSiz e: Integer);
var TM : TTextMetric;
Offset: Integer;
begin
Offset:=8;
with RichEdit1 do begin
GetTextMetrics(Canvas.Hand le, TM);
Height := VSize * (TM.tmHeight + TM.tmExternalLeading) + Offset;
end;
end;
procedure TForm1.RichEdit1ResizeRequ est(Sender : TObject; Rect: TRect);
// improved event handler
var s: String;
CurrentLineCount: Integer;
begin
RichEdit1.OnResizeRequest: =nil;
s:=RichEdit1.Text;
if s<>'' then begin
CurrentLineCount:=RichEdit 1.Lines.Co unt;
if s[length(s)]=Chr(10) then Inc(CurrentLineCount);
if (CurrentLineCount<>OldLine Count) then ResizeRichEdit(CurrentLine Count);
OldLineCount:=CurrentLineC ount;
end;
RichEdit1.OnResizeRequest: =RichEdit1 ResizeRequ est;
end;
this is improved version that takes care of it:
var OldLineCount: Integer; // global var
(....)
procedure TForm1.ResizeRichEdit(VSiz
var TM : TTextMetric;
Offset: Integer;
begin
Offset:=8;
with RichEdit1 do begin
GetTextMetrics(Canvas.Hand
Height := VSize * (TM.tmHeight + TM.tmExternalLeading) + Offset;
end;
end;
procedure TForm1.RichEdit1ResizeRequ
// improved event handler
var s: String;
CurrentLineCount: Integer;
begin
RichEdit1.OnResizeRequest:
s:=RichEdit1.Text;
if s<>'' then begin
CurrentLineCount:=RichEdit
if s[length(s)]=Chr(10) then Inc(CurrentLineCount);
if (CurrentLineCount<>OldLine
OldLineCount:=CurrentLineC
end;
RichEdit1.OnResizeRequest:
end;
ASKER
Two Issues,
1. When you alter the font size.
2. When you scroll off the bottom of the screen you lose the caret.
1. When you alter the font size.
2. When you scroll off the bottom of the screen you lose the caret.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Line height is font height+external leading. BUT, you
also have to account for constant offset, consisting of
additional spacing and bevel of top/bottom edges of TRichEdit.
For example (copy/paste):
procedure TForm1.ResizeRichEdit;
var TM : TTextMetric;
Offset: Integer;
begin
Offset:=8;
with RichEdit1 do begin
GetTextMetrics(Canvas.Hand
Height := Lines.Count * (TM.tmHeight + TM.tmExternalLeading) + Offset;
end;
end;
If you want this to happen automatically, probably the easiest
would be placing it in OnResizeRequest event:
procedure TForm1.RichEdit1ResizeRequ
begin
ResizeRichEdit;
end;