Delphi: TMemo垂直滚动条自动显示

项目中碰到此问题,之前一直没留意,研究一番用上,以做备忘。参考其它解决方法,不尽完美,自试之,达到效果。

直上代码:

type
  TMemo = class(StdCtrls.TMemo)
  protected
    procedure Change; override;
  end;
{ TMemo }

procedure TMemo.Change;
var
  lc, lh: Integer;
  cvs: TControlCanvas;
begin
  inherited;

  cvs := TControlCanvas.Create;
  try
    cvs.Control := Self;
    cvs.Font.Assign(Font);
    lh := cvs.TextHeight('z') + 2;
  finally
    cvs.Free;
  end;

  lc := Lines.Count;
  if (Text <> '') and (Text[Length(Text)] = #10) then
    Inc(lc);
  if lc * lh > Height then
  begin
    if ScrollBars = ssNone then
    begin
      ScrollBars := ssVertical;
      Perform(WM_VSCROLL, SB_BOTTOM, 0);
    end;
  end
  else if ScrollBars = ssVertical then
    ScrollBars := ssNone;
end;

 

posted on 2019-02-26 19:50  癫狂编程  阅读(606)  评论(0编辑  收藏  举报

导航

好的代码像粥一样,都是用时间熬出来的