加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 具有自动显示/隐藏滚动条的TMemo

发布时间:2020-12-15 03:34:04 所属栏目:大数据 来源:网络整理
导读:我需要简单的TMemo,当它们不需要时不显示滚动条(即不足够的文本),但是当它们是.像ScrollBars = ssAuto或类似于TRichEdit HideScrollBars的东西. 我试图子类化一个TMemo,并在CreateParams中使用ES_DISABLENOSCROLL,就像在TRichEdit中一样,但它不起作用. 编辑
我需要简单的TMemo,当它们不需要时不显示滚动条(即不足够的文本),但是当它们是.像ScrollBars = ssAuto或类似于TRichEdit HideScrollBars的东西.

我试图子类化一个TMemo,并在CreateParams中使用ES_DISABLENOSCROLL,就像在TRichEdit中一样,但它不起作用.

编辑:这可以启用或禁用WordWrap.

解决方法

如果您的备忘录放在表单上,??则当文本已更改时,表单将通过EN_UPDATE通知,内容将被重新绘制.你可以在这里决定是否有滚动条.我假设我们正在使用垂直滚动条,没有水平滚动条:
type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  protected
    procedure WMCommand(var Msg: TWMCommand); message WM_COMMAND;
  public

...

procedure SetMargins(Memo: HWND);
var
  Rect: TRect;
begin
  SendMessage(Memo,EM_GETRECT,Longint(@Rect));
  Rect.Right := Rect.Right - GetSystemMetrics(SM_CXHSCROLL);
  SendMessage(Memo,EM_SETRECT,Longint(@Rect));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo1.ScrollBars := ssVertical;
  Memo1.Lines.Text := '';
  SetMargins(Memo1.Handle);
  Memo1.Lines.Text := 'The EM_GETRECT message retrieves the formatting ' +
  'rectangle of an edit control. The formatting rectangle is the limiting ' +
  'rectangle into which the control draws the text.';
end;

procedure TForm1.WMCommand(var Msg: TWMCommand);
begin
  if (Msg.Ctl = Memo1.Handle) and (Msg.NotifyCode = EN_UPDATE) then begin
    if Memo1.Lines.Count > 6 then   // maximum 6 lines
      Memo1.ScrollBars := ssVertical
    else begin
      if Memo1.ScrollBars <> ssNone then begin
        Memo1.ScrollBars := ssNone;
        SetMargins(Memo1.Handle);
      end;
    end;
  end;
  inherited;
end;

设置正确边距的事情是,如果文本必须重新组合以适应,则删除/放置垂直滚动条看起来完全丑陋.

请注意,上述示例假定最多6行.要知道您的备忘录中有多少行可以看到这个问题:
How do I determine the height of a line of text in a TMemo programmatically?.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读