【发布时间】:2014-02-10 20:11:19
【问题描述】:
我需要能够同时拦截平板电脑上的多次点击的代码。在关于如何同时处理多个 OnMouseDown(不可能)的previous question 中,提供了一个link,它回答了有关如何在 Delphi-Android 中处理多点触控点击的问题。但是,此代码返回屏幕坐标中的 (x, y) 位置,我不知道如何将这些转换为特定控件的本地坐标。 Delphi documentation 指的是一个 ScreenToClient 函数,但它只将屏幕坐标转换为表单坐标,这在 Android 中几乎没有用(文档是关于 XE2 但该函数仍然存在于 XE5 中,但该函数已从 FMX.Platform 中删除到FMX.表格)。
有没有一种简单的方法可以像在 VCL 中一样将屏幕坐标转换为 FMX 中的 TControl 坐标?当然,我可以“取消父级”控件,记下它的左上角坐标并为每个父级这样做,直到到达基本表单,但这很乏味。
编辑 1
我目前的方法是获取 TControl(一个 TPanel)的顶部(x,y)坐标,它是要点击的控件(实际上是 TRectangle)的父级,并添加这些坐标以检查是否在该矩形内单击.请参阅下面的示例代码。
procedure TKeyBoard.process_touch (Event: TTouchEvent; status_byte: Int32);
var
key: TKey;
p, q: TPointF;
i: Integer;
x, y: single;
begin
// Check whether at least one event is present. If so, i points to the last event
i := Length (Event.Points) - 1;
if i < 0 then Exit;
// Get (x, y) coordinates from event. It's in screen coordinates
x := Event.Points [i].Position.X;
y := Event.Points [i].Position.Y;
// FControl is a reference to the panel holding the keys
// Find its rectangle position and convert to screen coordinates
p := TPointF.Create (FControl.Position.X, FControl.Position.Y);
q := TPointF.Create (p.X + FControl.Width, p.Y + FControl.Height);
p := Application.MainForm.ClientToScreen (p);
q := Application.MainForm.ClientToScreen (q);
logd ('control [%.0f %.0f - %.0f, %.0f]', [FControl.Position.X, FControl.Position.Y, FControl.Width, FControl.Height]);
logd ('to screen [%.0f %.0f - %.0f, %.0f]', [p.X, p.Y, q.X, q.Y]);
// Determine whether a black key has been pressed
for i := Low (Fkeys) to High (FKeys) do
begin
if not cOctave_Major [i mod nOctave] then
begin
key := FKeys [i];
logd ('%d (%.0f, %.0f) - (%.0f, %.0f) (%.0f, %.0f)', [i, x, y,
key.Position.X + p.X, key.Position.Y + p.Y,
key.Position.X + P.X + Key.Width, key.Position.Y + p.Y + key.Height]);
if (x >= key.Position.X + p.X) and (x <= key.Position.X + p.X + Key.Width) and
(y >= key.Position.Y + p.Y) and (y <= key.Position.Y + p.Y + key.Height)
then break;
key := nil;
end; // if
end; // for
// if not, check whether a white key has been pressed
if key = nil then
begin
logd ('Major');
for i := Low (Fkeys) to High (FKeys) do
begin
if cOctave_Major [i mod nOctave] then
begin
key := FKeys [i];
logd ('%d (%.0f, %.0f) - (%.0f, %.0f) (%.0f, %.0f)', [i, x, y,
key.Position.X + p.X, key.Position.Y + p.Y,
key.Position.X + P.X + Key.Width, key.Position.Y + p.Y + key.Height]);
if (x >= key.Position.X + p.X) and (x <= key.Position.X + p.X + Key.Width) and
(y >= key.Position.Y + p.Y) and (y <= key.Position.Y + p.Y + key.Height)
then break;
key := nil;
end; // if
end; // for
end; // if
if key <> nil
then putShort (status_byte, key.Note, 127);
if key <> nil
then logd (' found %s', [key.Text.Text]);
end; // process_touch //
这段代码实际上很不整洁,因为它假定父控件将 Application.MainForm 作为其父控件,而不必如此。另一个观察结果是,在 Y 位置的抽头可能仍然略有错误。出于这个原因,我想将屏幕坐标直接传输到控件的坐标。
编辑 2
我尝试按照@Sentient 的建议对每个键控件使用 IsMouseOver 检查,但奇怪的是,只有在处理 MouseUp 事件时才会产生 true。
【问题讨论】:
-
使用控件的 .IsMouseOver 会起作用吗?我不知道同时有多个触摸时是否正确注册。
-
唉,这在 Android 上不起作用。它在 MouseUp 事件期间触发,而不是在 MouseDown 期间触发。
标签: android delphi delphi-xe5