我有一个TScrollBox,它有一个比滚动框大的RichEdit,所以两侧的滚动条都会出现在滚动框中。然后我有一个调用RichEdit.SetFocus的函数DoTask。
当我向下滚动到要查看部分文本控件的位置,然后调用DoTask时,ScrollBox将自动滚动到RichEdit的顶部。我该如何避免这种情况呢?
发布于 2011-08-19 18:15:37
如你所愿,这里有一些建议:
表单中的
SetFocusedControl:函数TForm1.SetFocusedControl(控件: TWinControl):布尔值;begin如果控件= RichEdit,则结果:=为True,否则结果:=继承了SetFocusedControl(控件);
或者:
类型控制= TCustomMemoAccess ( TCustomMemo);函数TForm1.SetFocusedControl(控制: TWinControl):布尔值;var Memo: TCustomMemoAccess;滚动条: TScrollingWinControl;Pt: TPoint;begin Result :=继承控件(SetFocusedControl);如果(TCustomMemo是TCustomMemo),(Control.Parent <> nil)和(Control.Parent是TScrollingWinControl),则begin Memo := TCustomMemoAccess(控制);滚动条:= TScrollingWinControl(Memo.Parent);SendMessage(,,Integer(@Pt),);Scroller.VertScrollBar.Position := Scroller.VertScrollBar.Position + Memo.Top + Pt.Y;end;end;
TScrollBox:类型TScrollBox = class(Forms.TScrollBox) protected procedure AutoScrollInView (AControl : TControl);override;end;procedure TScrollBox.AutoScrollInView(AControl: TControl);begin if not (AControl is TCustomMemo)则继承AutoScrollInView(AControl);end;
或者:
procedure TScrollBox.AutoScrollInView(AControl: TControl);begin if (AControl.Top > VertScrollBar.Position + ClientHeight) xor (AControl.Top + AControl.Height < VertScrollBar.Position) then inherited (AControl);end;
或者使用上面的任何创造性的组合。只有你自己知道如何以及何时想要滚动它。
发布于 2011-08-19 18:03:36
最简单的解决方案是
var a, b : Integer;
begin
a := ScrollBox1.VertScrollBar.Position;
b := ScrollBox1.HorzScrollBar.Position;
richEdit1.SetFocus;
ScrollBox1.VertScrollBar.Position:=a ;
ScrollBox1.HorzScrollBar.Position:=b ;
end;发布于 2012-06-22 21:20:06
在不使用VCL/派生自定义组件的情况下,只有一种解决方案- TForm.SetFocusedControl覆盖+如上所述重新设置滚动条的位置。我添加的一件事是禁用/启用窗口重绘,以避免难看的跳转。这是我的最后一段代码:
sbContainer是TScrollBox,NoScrCtrl是一个放在里面的控件,它获得焦点,但我们不希望它在视图中滚动。
function TForm1.SetFocusedControl(Control: TWinControl): Boolean;
var hpos, vpos: integer;
begin
if Control = NoScrCtrl then
begin
sbContainer.Perform(WM_SETREDRAW, WPARAM(False), 0);
hpos := sbContainer.HorzScrollBar.Position;
vpos := sbContainer.VertScrollBar.Position;
Result := inherited SetFocusedControl(Control);
sbContainer.HorzScrollBar.Position := hpos;
sbContainer.VertScrollBar.Position := vpos;
sbContainer.Perform(WM_SETREDRAW, WPARAM(True), 0);
sbContainer.Refresh;
end
else
Result := inherited SetFocusedControl(Control);
end;https://stackoverflow.com/questions/7118194
复制相似问题