如果我使用以下代码创建一个新shell:
shell = new Shell( Display.getDefault(), SWT.RESIZE);
然后这给了我一个没有标题栏或最小化/最大化按钮的shell,这就是我想要的。我可以将这个窗口调整到任何大小,这很好。但问题是,窗户固定在它的位置,我不能通过拖动它来移动它。
如果我添加SWT.CASCADE
或SWT.CLOSE
,这会给我标题栏和关闭按钮,这是我不想要的,但此外,它限制了窗口的小小程度调整大小,即我无法横向调整大小超过一定限度。
如果没有关闭按钮/标题栏,如何使窗口可以移动?如果在SWT中没有本地方式来执行此操作,我可以通过侦听鼠标拖动事件并手动设置shell的位置来实现吗?如果是这样,我如何从鼠标移动中获得鼠标坐标?
帮助将不胜感激。谢谢!
答案 0 :(得分: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();
}
}
答案 1 :(得分: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 isnull
or disposed. * * @param control * the control to verify. * @returntrue
if the control isnull
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; } }