Firemonkey TVertScrollBox 鼠标位置

Firemonkey TVertScrollBox mouse position

我有表单 (Height = 500) 和 TVertScrollBox(对齐设置为 TAlignLayout.Client,范围为 5000px)。我写了一个简单的方法,当我点击滚动框时显示鼠标的位置。

procedure TformMain.VertScrollBox1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
  ShowMessage(FloatToStr(X) + '  ' + FloatToStr(Y));
end;

当滚动条在顶部时,我点击滚动框顶部,message中的Y为0,没错。但是当我向下滚动到一半并单击滚动框顶部时,消息中的 Y 也为 0(不是 2500)。如何获取相对于滚动框的位置?

这是我的 TForm 和 TVertScrollBox 的 FMX 代码:

object formMain: TformMain
  Left = 0
  Top = 0
  BorderIcons = [biSystemMenu, biMinimize]
  BorderStyle = Single
  Caption = 'Gear Studio 1.0'
  ClientHeight = 600
  ClientWidth = 450
  Position = DesktopCenter
  StyleBook = StyleBookPanel
  FormFactor.Width = 320
  FormFactor.Height = 480
  FormFactor.Devices = [Desktop]
  OnCreate = FormCreate
  OnCloseQuery = FormCloseQuery
  DesignerMasterStyle = 0
  object VertScrollBox1: TVertScrollBox
    Align = Client
    Size.Width = 450.000000000000000000
    Size.Height = 576.000000000000000000
    Size.PlatformDefault = False
    StyleLookup = 'VertScrollBox1Style1'
    TabOrder = 1
    OnMouseDown = VertScrollBox1MouseDown
    Viewport.Width = 450.000000000000000000
    Viewport.Height = 576.000000000000000000
  end
  ...
  ...
end

这就是我添加面板的方式:

  SetLength(MyItems, i+1);
  MyItems[i] := TItem.Create(i);
  with MyItems[i] do begin
    ...
  end;

constructor TItem.Create(number: integer);
var
  ThisItem: TItem;
begin
  inherited Create(nil);
  ThisItem := Self;
  with ThisItem do begin
    if number = -1 then begin
       ... //not important
      end;
    end else if number > 0 then begin
      Width := 370;
      Height := 35;
      ...
    end;
    Position.X := 10;
    Parent := formMain.VertScrollBox1;
    PopupMenu := formMain.PopupMenu1;
    OnDblClick := DblClick;
    OnMouseEnter := MouseEnter;
    OnMouseLeave := MouseLeave;
  end;
end;

MyItems 是 TItem 的动态数组(它是添加了一些属性的普通 TPanel)。

需要加上VertScrollBox1.ViewportPosition.Y属性得到绝对坐标

ShowMessage(FloatToStr(X) + '  ' + FloatToStr(VertScrollBox1.ViewportPosition.Y+Y));

显示正确的结果。