Firemonkey TVertScrollBox鼠标位置

时间:2015-08-06 08:28:09

标签: delphi firemonkey delphi-xe8

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

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

当滚动条位于顶部并且我点击滚动框的顶部时,消息中的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,添加了一些属性)。

1 个答案:

答案 0 :(得分:3)

您需要添加VertScrollBox1.ViewportPosition.Y属性才能获得绝对坐标。

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

显示正确的结果。