简体   繁体   English

在位置获取SWT小部件

[英]Get SWT widget at location

我有一组相对于复合材料的x / y坐标,如何找到那些coords落入的小部件?

It's quite ugly but you can do a function like this : 这很难看但你可以做这样的功能:

public Control getControl(Composite composite, int x, int y)
{
  Control control = null;
  Control[] children = composite.getChildren()
  if (children.length == 0)
     control = composite;
  else
    for (Control tmp : children) {
       // The check below will not work because x, y and the control's bounds could be
       // relative to different parents... Better to convert all coordinates to display
       // by using Control.toDisplay() and then compare below
       if (tmp.getBounds().contains(x, y)) 
       {
         if (control instanceof Composite)
             control = getControl(tmp, x, y);
         else
             control =  tmp;
         break;
       }

    }

  return control;
}

If you get the coordinates using a MouseListener, you can simply use : 如果使用MouseListener获取坐标,则可以使用:

event.getSource();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM