获取禁用控件的光标控件

Get Cursor Control for Disabled Control

我想获得鼠标悬停的控制权,通常由 Display#getCursorControl 完成。但是,当层次结构中的一个控件被禁用时,此方法不再有效:

示例:

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setSize(400, 300);
    shell.setLayout(new GridLayout(2, false));

    final Label mouseControl = new Label(shell, SWT.BORDER);
    mouseControl.setLayoutData(GridDataFactory.fillDefaults().span(2, 1).grab(true, true).create());
    display.addFilter(SWT.MouseMove,
            e -> mouseControl.setText("" + e.display.getCursorControl()));

    final Group enabledGroup = new Group(shell, SWT.NONE);
    enabledGroup.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());
    enabledGroup.setText("Enabled Group");
    createControls(enabledGroup);

    final Group disabledGroup = new Group(shell, SWT.NONE);
    disabledGroup.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());
    disabledGroup.setText("Disabled Group");
    disabledGroup.setEnabled(false);
    createControls(disabledGroup);

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

private static void createControls(Composite parent) {
    parent.setLayout(new GridLayout());

    final Label label = new Label(parent, SWT.NONE);
    label.setText("Label");

    final Text text = new Text(parent, SWT.BORDER);
    text.setText("Text");
}

将鼠标悬停在左边的标签上,然后再悬停在右边的标签上。该控件仅对启用的父项显示,否则显示 shell。

如何获取鼠标指针下方的控件?我必须自己实现这个功能吗?有什么方法可以帮助我,还是我必须计算树内每个控件的边界并检查它是否在鼠标位置?

我在 Display 中看不到任何有用的信息。

以下将在 Shell 的子项中搜索包含光标的控件并使用禁用的控件:

static Control findCursorinShellChildren(final Shell shell)
{
  return findLocationInCompositeChildren(shell, shell.getDisplay().getCursorLocation());
}


static Control findLocationInCompositeChildren(final Composite composite, final Point displayLoc)
{
  final var compositeRelativeLoc = composite.toControl(displayLoc);

  for (final var child : composite.getChildren())
   {
     if (child.getBounds().contains(compositeRelativeLoc))
      {
        if (child instanceof Composite)
         {
           final var containedControl = findLocationInCompositeChildren((Composite)child, displayLoc);
           return containedControl != null ? containedControl : child;
         }

        return child;
      }
   }

  return null;
}

我想这会比 Display.getCursorControl

慢很多