获得禁用控件的光标控制

huangapple go评论73阅读模式
英文:

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");
}

将鼠标悬停在左侧标签上,然后再悬停在右侧标签上。只有启用的父控件才会显示,否则将显示窗口。

我如何获取鼠标指针下面的控件?我是否需要自己实现这个功能?是否有任何方法可以帮助我,或者我是否必须计算树中每个控件的边界,并检查它是否位于鼠标位置?

英文:

I want to get the control the mouse hovers over which normally is done by Display#getCursorControl. However when one control in the hierarchy is disabled, this method doesn't work any longer:

Example:

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");
}

Hold the mouse over the left label and then over the right one. The control is only displayed for an enabled parent, else the shell is displayed.

How do I get the control below the mouse pointer? Do I have to implement this functionality myself? Are there any methods that can help me or do I have to calculate the bounds of each control inside the tree and check if it is on the mouse position?

答案1

得分: 1

我在“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”要慢得多。

英文:

I can't see anything in Display that would help.

The following will search the children of a Shell for a control containing the cursor and works with disabled controls:

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;
}

I imagine this is going to be significantly slower than Display.getCursorControl

huangapple
  • 本文由 发表于 2020年5月5日 16:02:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/61608452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定