delphi - 如何禁用 TScrollBox 的滚动到 View 行为?

标签 delphi scroll

我有一个 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/

相关文章:

delphi - 检查 64 位常数乘法参数

java - Libgdx scrollPane 在我添加 scrollPaneStyel 后不滚动

delphi - Delphi 最好的报告组件?

delphi - 如何为我自己的组件创建 Vcl-Theme-Style?

javascript - 在浏览器中,在窗口滚动时中断

ios - 以编程方式在 iOS 上滚动 UIWebView

javascript - 如何在 React JS 中使用 Scroll 魔法

jquery - 平滑滚动生成:active attribute on target div

database - Delphi - 在运行时将字段更改为计算字段。这是一个好习惯吗?

delphi - 用Delphi如何获取当前安装的IE版本?