我需要简单的TMemo,它不会在不需要时显示滚动条(即文本不足),但是当它们存在时会显示.类似于ScrollBars = ssAuto或类似于TRichEdit HideScrollBars.
我试图将TMemo子类化并ES_DISABLENOSCROLL在CreateParams中使用,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;
Run Code Online (Sandbox Code Playgroud)
设置右边距的事情是,如果文本必须重新构造以适应,则移除/放置垂直滚动条看起来非常难看.
请注意,上面的示例假设最多6行.要知道备忘录中可以包含多少行,请参阅以下问题:
如何以编程方式确定TMemo中文本行的高度?.