Solved

Resize a richedit control

Posted on 2002-07-06
4
1,549 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 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