我需要一个简单的TMemo,在不需要滚动条时(即文本不足时),不显示滚动条,但在需要时则显示。类似于 ScrollBars = ssAuto
或像 TRichEdit 的 HideScrollBars
。
我尝试过对 TMemo 进行子类化,并在 CreateParams 中使用 ES_DISABLENOSCROLL
,就像在 TRichEdit
中一样,但它没有起作用。
编辑:这应该可以在启用或禁用 WordWrap
的情况下工作。
我需要一个简单的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, 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;