Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to calculate the line length of a TRichEdit Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
12-Oct-02
Category
Reporting /Printing
Language
Delphi 2.x
Views
111
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

I want to fill a whole line with the same char in a TRichEdit but I can't figure 
out how I could retrieve the maximum char number for a line.

Answer:

The number of characters you would be able to fit into a line depends on the font 
and the character used (unless the font is fixed-pitch). The first order of the day 
is to find out how much space is available for the line. This you do by asking the 
control for its formatting rectangle, which is usually a bit smaller than the 
client area. Then you determine the font to use and measure the character, which 
gives you a first estimate of the number of characters that may fit. You construct 
a string of this character of the calculated length and measure it again. It may 
turn out to be too long since the length also includes intercharacter distance, so 
you remove characters till it fits.
1   
2   function MakeLine(re: TRichEdit; ch: Char): string;
3   var
4     cv: TControlCanvas;
5     r: TRect;
6     max, len: Integer;
7   begin
8     cv := TControlCanvas.Create;
9     try
10      cv.Control := re;
11      cv.Font.Assign(re.SelAttributes);
12      re.Perform(EM_GETRECT, 0, lparam(@r));
13      max := r.right - r.Left;
14      len := max div cv.TextWidth(ch);
15      Result := StringOfChar(ch, len);
16      while cv.TextWidth(result) > max do
17        Delete(result, Length(Result), 1);
18    finally
19      cv.Free;
20    end;
21  end;
22  
23  procedure TForm1.Button1Click(Sender: TObject);
24  begin
25    richedit1.SelText := MakeLine(richedit1, '-');
26  end;


You may want to enhance this to take account of any margin settings in the current paragraph.

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC