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 に答える