Avoid scrolling in your Memo  

Send By: Radikal (Q3 Team)
Web : http://www.q3.nu
Email: radikal@q3.nu
Date: 26/12/99

Tip accessed 484 times

 


Useful if you want that your memo only shows what fits in the screen, neither one line more.


 procedure TForm1.Memo1Change(Sender: TObject);

   function LineasVisibles(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);
      try
        OldFont := SelectObject(DC, Memo.Font.Handle);
        try
          GetTextMetrics(DC, Tm);
          Memo.Perform(EM_GETRECT, 0, longint(@TheRect));
          Result := (TheRect.Bottom - TheRect.Top) div
             (Tm.tmHeight + Tm.tmExternalLeading);
        finally
          SelectObject(DC, Oldfont);
        end;
      finally
        ReleaseDC(Memo.Handle, DC);
      end;
    end;

 var
   Limite: integer;

 begin
   Limite:=LineasVisibles ((Sender As TMemo));
   with (Sender As TMemo) do
   begin
     If Lines.Count = Limite+1 then
       Lines.Delete(Lines.Count-1);
     If Perform(EM_LINEFROMCHAR,SelStart,0) = Limite then
        Text:=Copy(Text,1,Length(Text)-2);
   end;
 end;