Articles   Members Online:
-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 determine how many lines a TMemo is capable of showing 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 All Versions
Views
99
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

How to determine how many lines a TMemo is capable of showing

Answer:

Here is the short and elegant way that works (most of the time):
1   
2   function TForm1.MemoLinesShowing(memo: TMemo): integer;
3   var
4     R: TRect;
5   begin
6     Memo.Perform(EM_GETRECT, 0, Longint(@R));
7     Result := (R.Bottom - R.Top) div Canvas.TextHeight('XXX');
8   end;


The problem with this code is that the TForm and the TMemo must both be using the 
same font. If the fonts are different, then the calculations are not accurate.

You have to retrieve the font height by selecting it into a device context. The 
reason you cannot use the font Height provided by Delphi is because Delphi caches 
the font infomation but doesn't acutally select the font into the DC (canvas) until 
it is actually going to draw something. This occurs in the painting event of the 
memo.

To get around this problem, you can get the memo's device context using the Windows 
API and get the font information from the device context to calculate the text 
height. The function below illustrates this process:
1   
2   function TForm1.MemoLinesShowing(memo: TMemo): integer;
3   var
4     R: TRect;
5   begin
6     Memo.Perform(EM_GETRECT, 0, Longint(@R));
7     Result := (R.Bottom - R.Top) div Canvas.TextHeight('XXX');
8   end;


The problem with this code is that the TForm and the TMemo must both be using the 
same font. If the fonts are different, then the calculations are not accurate.

You have to retrieve the font height by selecting it into a device context. The 
reason you cannot use the font Height provided by Delphi is because Delphi caches 
the font infomation but doesn't acutally select the font into the DC (canvas) until 
it is actually going to draw something. This occurs in the painting event of the 
memo.

To get around this problem, you can get the memo's device context using the Windows 
API and get the font information from the device context to calculate the text 
height. The function below illustrates this process:

function TForm1.MemoLinesShowingLong(Memo: TMemo): integer;
var
  Oldfont: HFont; {the old font}
  DC: THandle; {Device context handle}
  i: integer; {loop variable}
  Tm: TTextMetric; {text metric structure}
  TheRect: TRect;
begin
  DC := GetDC(Memo.Handle); {Get the memo's device context}
  try
    {Select the memo's font}
    OldFont := SelectObject(DC, Memo.Font.Handle);
    try
      GetTextMetrics(DC, Tm); {Get the text metric info}
      Memo.Perform(EM_GETRECT, 0, longint(@TheRect));
      Result := (TheRect.Bottom - TheRect.Top) div (Tm.tmHeight +
        Tm.tmExternalLeading);
    finally
      SelectObject(DC, Oldfont); {Select the old font}
    end;
  finally
    ReleaseDC(Memo.Handle, DC); {Release the device context}
  end;
end;

			
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