如何禁用 TScrollBox 的滚动到视图行为?
我有一个 TScrollBox,它有一个比滚动框大的 RichEdit,因此两侧滚动条都出现在滚动框中。然后我有一个调用 RichEdit.SetFocus
的函数 DoTask
。
当我向下滚动到想要查看部分文本控件的位置,然后调用 DoTask 时,ScrollBox 将自动滚动到 RichEdit 的顶部。我怎样才能避免这种情况?
I have a TScrollBox
that has a RichEdit that is bigger than the scrollbox, so both side scrollbars appear in the scrollbox. Then I have a function DoTask
that calls RichEdit.SetFocus
.
When I scroll down to where I want to see part of the text control, and then call DoTask
, the ScrollBox will automatically scroll to the top of the RichEdit. How can I avoid that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如您所愿,这里有一些建议:
以以下形式覆盖
SetFocusedControl
:或者:
<前><代码>类型
TCustomMemoAccess = 类(TCustomMemo);
函数 TForm1.SetFocusedControl(Control: TWinControl): Boolean;
变量
备忘录:TCustomMemoAccess;
滚动条:TScrollingWinControl;
Pt:T点;
开始
结果 := 继承 SetFocusedControl(Control);
if (Control 是 TCustomMemo) and (Control.Parent <> nil) and
(Control.Parent是TScrollingWinControl)然后
开始
备忘录 := TCustomMemoAccess(Control);
滚动器 := TScrollingWinControl(Memo.Parent);
SendMessage(Memo.Handle, EM_POSFROMCHAR, Integer(@Pt), Memo.SelStart);
Scroller.VertScrollBar.Position := Scroller.VertScrollBar.Position +
Memo.Top + Pt.Y;
结尾;
结尾;
Interpose
TScrollBox
:<前><代码>类型
TScrollBox = 类(Forms.TScrollBox)
受保护的
过程 AutoScrollInView(AControl: TControl);覆盖;
结尾;
过程 TScrollBox.AutoScrollInView(AControl: TControl);
开始
如果不是(AControl 是 TCustomMemo)那么
继承 AutoScrollInView(AControl);
结尾;
或者:
或者使用上述所有内容的任意创意组合。只有您知道如何以及何时滚动它。
As you wish, here are some suggestions:
Override
SetFocusedControl
in the form:Or:
Interpose
TScrollBox
:Or:
Or use any creative combination of all of the above. How and when you like it to be scrolled only you know.
最简单的解决方案是
the simpliest solution would be
在不侵入 VCL/派生自定义组件的情况下,只有一种解决方案 - TForm.SetFocusedControl 覆盖 + 重新设置滚动条的位置,如上所述。我添加的一件事是禁用/启用窗口重绘以避免难看的跳转。
这是我的最后一个片段:
sbContainer 是 TScrollBox,NoScrCtrl 是位于其中的控件,它获得焦点,但我们不希望它在视图中滚动。
Without hacking into VCL/deriving custom components there's only one solution - TForm.SetFocusedControl override + re-setting the positions of scrollbars as said above. One thing I added is disabling/enabling window redraw to avoid ugly jumps.
Here's my final snippet:
sbContainer is TScrollBox and NoScrCtrl is a control laying inside it which gets focus but we don't want it to be scrolled-in-view.
要从主窗体禁用滚动到视图行为,我使用了以下解决方案:(C++Builder)
To disable scroll-into-view behavior from my main form, I used this solution: (C++Builder)