如何禁用 TScrollBox 的滚动视图行为? [英] How can I disable the scroll-into-view behavior of TScrollBox?
问题描述
我有一个 TScrollBox
,它有一个比滚动框大的 RichEdit,所以两个侧滚动条都出现在滚动框中.然后我有一个函数 DoTask
调用 RichEdit.SetFocus
.
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
.
当我向下滚动到我想看到部分文本控件的地方,然后调用DoTask
时,ScrollBox会自动滚动到RichEdit的顶部.我怎样才能避免这种情况?
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?
推荐答案
如您所愿,这里有一些建议:
As you wish, here are some suggestions:
以以下形式覆盖
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;
或者使用上述所有内容的任意创意组合.您喜欢滚动的方式和时间只有您自己知道.
Or use any creative combination of all of the above. How and when you like it to be scrolled only you know.
这篇关于如何禁用 TScrollBox 的滚动视图行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!