英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论