Solved

Resize a richedit control

Posted on 2002-07-06
4
1,493 Views
Last Modified: 2013-11-22
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
Comment
Question by:julianpointer
  • 3
4 Comments
 
LVL 7

Expert Comment

by:Cynna
ID: 7133713
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
 
LVL 7

Expert Comment

by:Cynna
ID: 7133836
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
 
LVL 3

Author Comment

by:julianpointer
ID: 7136129
Two Issues,
  1. When you alter the font size.
  2. When you scroll off the bottom of the screen you lose the caret.
0
 
LVL 7

Accepted Solution

by:
Cynna earned 200 total points
ID: 7136493
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now