我有一个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;
或者使用上述所有创意组合.你喜欢滚动的方式和时间只有你知道.