带自动显示/隐藏滚动条的TMemo

14

我需要一个简单的TMemo,在不需要滚动条时(即文本不足时),不显示滚动条,但在需要时则显示。类似于 ScrollBars = ssAuto 或像 TRichEdit 的 HideScrollBars

我尝试过对 TMemo 进行子类化,并在 CreateParams 中使用 ES_DISABLENOSCROLL,就像在 TRichEdit 中一样,但它没有起作用。

编辑:这应该可以在启用或禁用 WordWrap 的情况下工作。


2
有人知道这是VCL问题还是Microsoft Windows(多行)EDIT控件的行为就像这样(即,没有自动设置滚动条可见性)吗? - Andreas Rejbrand
2
我认为这是一个Windows的“问题”。例如,看看记事本。 - Andreas Rejbrand
@zigiz - 你好。你找到了这个问题的完整解决方案吗? - Gabriel
http://delphi.xcjc.net/viewthread.php?tid=44882 - Gabriel
1个回答

6

如果您的备忘录放在表格上,当文本更改并且内容将被重新绘制时,表格将收到一个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, 0, Longint(@Rect));
  Rect.Right := Rect.Right - GetSystemMetrics(SM_CXHSCROLL);
  SendMessage(Memo, EM_SETRECT, 0, 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行。要知道您的备忘录可以容纳多少行,请参见此问题: 如何以编程方式确定TMemo中文本行的高度?

2
当WordWrap为False且文本超出TMemo的可见区域时,应该有水平滚动条。 - ZigiZ
4
@ZigiZ - 我为你提供了一个可能的想法可以供你发挥。问题没有提供具体细节,如果你愿意,你可以自己处理细节 :). - Sertac Akyuz
谢谢你的努力!我会更深入地研究它 :) - ZigiZ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接