[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1678
  • Last Modified:

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.Count * 13.5);

this dosn't work properly, I need to get the logical client area ?

0
julianpointer
Asked:
julianpointer
  • 3
1 Solution
 
CynnaCommented:
julianpointer,

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.Handle, TM);
    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.RichEdit1ResizeRequest(Sender: TObject; Rect: TRect);
begin
  ResizeRichEdit;
end;
0
 
CynnaCommented:
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(VSize: Integer);
var TM : TTextMetric;
    Offset: Integer;
begin
  Offset:=8;
  with RichEdit1 do begin
    GetTextMetrics(Canvas.Handle, TM);
    Height := VSize * (TM.tmHeight + TM.tmExternalLeading) + Offset;
  end;
end;

procedure TForm1.RichEdit1ResizeRequest(Sender: TObject; Rect: TRect);
// improved event handler
var s: String;
    CurrentLineCount: Integer;
begin
  RichEdit1.OnResizeRequest:=nil;
    s:=RichEdit1.Text;
    if s<>'' then begin
       CurrentLineCount:=RichEdit1.Lines.Count;
       if s[length(s)]=Chr(10) then Inc(CurrentLineCount);
       if (CurrentLineCount<>OldLineCount) then ResizeRichEdit(CurrentLineCount);
       OldLineCount:=CurrentLineCount;
    end;
  RichEdit1.OnResizeRequest:=RichEdit1ResizeRequest;
end;
0
 
julianpointerAuthor Commented:
Two Issues,
  1. When you alter the font size.
  2. When you scroll off the bottom of the screen you lose the caret.
0
 
CynnaCommented:
julianpointer,

> 1. When you alter the font size.

I'm sorry, I didn't do much testing on the code, and I went for a straight-forward,
hammer-approach - a mistake. The code that does much better is ridiculously
simple - in fact, you can find it right in the Delphi Help :). It turns out that Delphi
implements EN_REQUESTRESIZE message, along with the REQRESIZE structure.
Moreover, it's all nicely wrapped up in a OnResizeRequest event - so you
actually have the exact size of the RE control to embend inner text right in
the Rect argument, which makes your problem trivial (if you read Help...:)



> 2. When you scroll off the bottom of the screen you lose the caret.

I'm not sure I follow you on this one. If you mean that RE becomes longer
than your form, than all you have to do is scroll the form down (assuming you
left the default form AutoScroll property on).

OK, this is the code that implements my comments:


procedure TForm2.RichEdit1ResizeRequest(Sender: TObject; Rect: TRect);
begin
   // Keep the RE dimensions wrapped around its contens:
   (Sender as TRichEdit).BoundsRect := Rect;
   // Scroll the form contens down:
   VertScrollBar.Position:=VertScrollBar.Range;
end;
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now