2013-09-22 51 views
1

如果我創建使用下面的代碼一個新的外殼:如何創建不帶標題欄/關閉按鈕的可移動SWT外殼?

shell = new Shell(Display.getDefault(), SWT.RESIZE);   

然後這給了我沒有標題欄的殼或最小化/最大化按鈕,這就是我想要的。我可以將此窗口調整爲任何大小,這非常棒。但問題是,窗戶是固定的,我無法通過拖動它來移動它。

如果我添加SWT.CASCADESWT.CLOSE,這給了我不想要的標題欄和關閉按鈕,而且它限制了窗口可以調整大小的限制,也就是說我不能調整它的水平超過一定的限制。

如何在不關閉按鈕/標題欄的情況下使窗口移動?如果在SWT中沒有本地方法來做到這一點,我可以通過監聽鼠標拖動事件並手動設置shell的位置來做到這一點嗎?如果是這樣,我將如何從鼠標的移動中獲得鼠標座標?

幫助將不勝感激。謝謝!

回答

5

您需要使用自己的偵聽器。下面的代碼應該有所幫助: -

public class Demo { 

    static Boolean blnMouseDown=false; 
    static int xPos=0; 
    static int yPos=0; 

    public static void main(final String[] args) { 
     Display display=new Display(); 
     final Shell shell = new Shell(Display.getDefault(), SWT.RESIZE); 
     shell.open(); 

     shell.addMouseListener(new MouseListener() { 

      @Override 
      public void mouseUp(MouseEvent arg0) { 
       // TODO Auto-generated method stub 
       blnMouseDown=false; 
      } 

      @Override 
      public void mouseDown(MouseEvent e) { 
       // TODO Auto-generated method stub 
       blnMouseDown=true; 
       xPos=e.x; 
       yPos=e.y; 
      } 

      @Override 
      public void mouseDoubleClick(MouseEvent arg0) { 
       // TODO Auto-generated method stub 

      } 
     }); 
     shell.addMouseMoveListener(new MouseMoveListener() { 

      @Override 
      public void mouseMove(MouseEvent e) { 
       // TODO Auto-generated method stub 
       if(blnMouseDown){ 

        shell.setLocation(shell.getLocation().x+(e.x-xPos),shell.getLocation().y+(e.y-yPos)); 
       } 
      } 
     }); 

     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) { 
      display.sleep(); 
      } 
     } 
     display.close(); 
    } 

} 
+0

謝謝,我已經實現了一個類似的解決方案,但我與它有另外一個問題。有什麼機會可以幫忙? http://stackoverflow.com/questions/18952545/how-to-show-a-rapidly-changing-image-in-an-swt-canvas-paintcontrol-listener – user2790209

3

這是我實現:

 
/** 
* Class to allow user to move a shell without a title. 
* 
* @author Laurent Muller 
* @version 1.0 
*/ 
public class MoveShellListener implements Listener { 

    /* 
    * the parent shell 
    */ 
    private final Shell parent; 

    /* 
    * the mouse down location 
    */ 
    private Point ptMouseDown; 

    /** 
    * Creates a new instance of this class. 
    * 
    * @param parent 
    *   the shell to handle. 
    */ 
    public MoveShellListener(final Shell parent) { 
     if (parent == null) { 
      SWT.error(SWT.ERROR_NULL_ARGUMENT); 
     } 
     if (parent.isDisposed()) { 
      SWT.error(SWT.ERROR_WIDGET_DISPOSED); 
     } 

     // copy and add listener 
     this.parent = parent; 
     addControl(parent); 
    } 

    /** 
    * Adds the given control to the list of listened controls. If the given 
    * control is an instance of {@link Composite}, the children controls are 
    * also added. 
    * 
    * @param control 
    *   the control to add. 
    */ 
    public void addControl(final Control control) { 
     // check control 
     if (isDisposed(control) || control.getShell() != parent) { 
      return; 
     } 

     // add listeners 
     control.addListener(SWT.MouseDown, this); 
     control.addListener(SWT.MouseUp, this); 
     control.addListener(SWT.MouseMove, this); 

     // children 
     if (control instanceof Composite) { 
      final Control[] children = ((Composite) control).getChildren(); 
      for (final Control child : children) { 
       addControl(child); 
      } 
     } 
    } 

    /** 
    * Adds the given controls to the list of listened controls. If one of the 
    * given controls is an instance of {@link Composite}, the children controls 
    * are also added. 
    * 
    * @param controls 
    *   the controls to add. 
    */ 
    public void addControls(final Control... controls) { 
     if (controls != null) { 
      for (final Control control : controls) { 
       addControl(control); 
      } 
     } 
    } 

    /** 
    * {@inheritDoc} 
    */ 
    @Override 
    public void handleEvent(final Event e) { 
     switch (e.type) { 
     case SWT.MouseDown: 
      onMouseDown(e); 
      break; 
     case SWT.MouseUp: 
      onMouseUp(e); 
      break; 
     case SWT.MouseMove: 
      onMouseMove(e); 
      break; 
     } 
    } 

    /** 
    * Removes the given control to the list of listened controls. If the given 
    * control is an instance of {@link Composite}, the children controls are 
    * also removed. 
    * 
    * @param control 
    *   the control to remove. 
    */ 
    public void removeControl(final Control control) { 
     // check control 
     if (control == parent || isDisposed(control) 
       || control.getShell() != parent) { 
      return; 
     } 

     // remove listeners 
     control.removeListener(SWT.MouseDown, this); 
     control.removeListener(SWT.MouseUp, this); 
     control.removeListener(SWT.MouseMove, this); 

     // children 
     if (control instanceof Composite) { 
      final Control[] children = ((Composite) control).getChildren(); 
      for (final Control child : children) { 
       removeControl(child); 
      } 
     } 
    } 

    /** 
    * Removes the given controls to the list of listened controls. If one of 
    * the given controls is an instance of {@link Composite}, the children 
    * controls are also removed. 
    * 
    * @param controls 
    *   the controls to remove. 
    */ 
    public void removeControls(final Control... controls) { 
     if (controls != null) { 
      for (final Control control : controls) { 
       removeControl(control); 
      } 
     } 
    } 

    /** 
    * Checks if the given control is null or disposed. 
    * 
    * @param control 
    *   the control to verify. 
    * @return true if the control is null or 
    *   disposed. 
    */ 
    private boolean isDisposed(final Control control) { 
     return control == null || control.isDisposed(); 
    } 

    /** 
    * Handles the mouse down event. 
    * 
    * @param e 
    *   the event data. 
    */ 
    private void onMouseDown(final Event e) { 
     if (e.button == 1) { 
      ptMouseDown = new Point(e.x, e.y); 
     } 
    } 

    /** 
    * Handles the mouse move event. 
    * 
    * @param e 
    *   the event data. 
    */ 
    private void onMouseMove(final Event e) { 
     if (ptMouseDown != null) { 
      final Point location = parent.getLocation(); 
      location.x += e.x - ptMouseDown.x; 
      location.y += e.y - ptMouseDown.y; 
      parent.setLocation(location); 
     } 
    } 

    /** 
    * Handles the mouse up event. 
    * 
    * @param e 
    *   the event data. 
    */ 
    private void onMouseUp(final Event e) { 
     ptMouseDown = null; 
    } 
} 
相關問題