3

我有一组相对于复合材料的 x/y 坐标,如何找到这些坐标所在的小部件?

4

1 回答 1

5

这很丑,但你可以做这样的功能:

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

如果您使用 MouseListener 获取坐标,您可以简单地使用:

event.getSource();
于 2012-11-08T13:37:22.353 回答