我有一个 TScrollBox
,它的 RichEdit 比滚动框大,因此两侧滚动条都出现在滚动框中。然后我有一个调用 RichEdit.SetFocus
的函数 DoTask
。
当我向下滚动到想要查看部分文本控件的位置,然后调用 DoTask
时,ScrollBox 将自动滚动到 RichEdit 的顶部。我怎样才能避免这种情况?
最佳答案
如您所愿,这里有一些建议:
重写表单中的
SetFocusedControl
:function TForm1.SetFocusedControl(Control: TWinControl): Boolean; begin if Control = RichEdit then Result := True else Result := inherited SetFocusedControl(Control); end;
或者:
type TCustomMemoAccess = class(TCustomMemo); function TForm1.SetFocusedControl(Control: TWinControl): Boolean; var Memo: TCustomMemoAccess; Scroller: TScrollingWinControl; Pt: TPoint; begin Result := inherited SetFocusedControl(Control); if (Control is TCustomMemo) and (Control.Parent <> nil) and (Control.Parent is TScrollingWinControl) then begin Memo := TCustomMemoAccess(Control); Scroller := TScrollingWinControl(Memo.Parent); SendMessage(Memo.Handle, EM_POSFROMCHAR, Integer(@Pt), Memo.SelStart); Scroller.VertScrollBar.Position := Scroller.VertScrollBar.Position + Memo.Top + Pt.Y; end; end;
插入
TScrollBox
:type TScrollBox = class(Forms.TScrollBox) protected procedure AutoScrollInView(AControl: TControl); override; end; procedure TScrollBox.AutoScrollInView(AControl: TControl); begin if not (AControl is TCustomMemo) then inherited AutoScrollInView(AControl); end;
或者:
procedure TScrollBox.AutoScrollInView(AControl: TControl); begin if (AControl.Top > VertScrollBar.Position + ClientHeight) xor (AControl.Top + AControl.Height < VertScrollBar.Position) then inherited AutoScrollInView(AControl); end;
或者使用以上所有内容的任意创意组合。只有您知道如何以及何时滚动它。
关于delphi - 如何禁用 TScrollBox 的滚动到 View 行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7118194/