我需要简单的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?.