繁体   English   中英

如何创建没有标题栏/关闭按钮的可移动SWT外壳?

[英]How to create a moveable SWT shell without title bar/ close button?

如果我使用以下代码创建新的外壳:

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

然后,这给了我一个没有标题栏或最小化/最大化按钮的外壳,这正是我想要的。 我可以将此窗口调整为任意大小,效果很好。 但是问题是,窗口固定在其位置上,我无法通过拖动来移动它。

如果我添加SWT.CASCADESWT.CLOSE ,这会给我标题栏和关闭按钮,这是我不想要的,但是,这限制了窗口可以调整的大小,即我不能将其水平调整为超过特定限制。

如何在没有关闭按钮/标题栏的情况下移动窗口? 如果SWT中没有本地方法可以执行此操作,是否可以通过侦听鼠标拖动事件并手动设置外壳的位置来执行此操作? 如果是这样,我如何从鼠标的移动中获取鼠标坐标?

帮助将不胜感激。 谢谢!

您需要使用自己的侦听器。 下面的代码应该有帮助:-

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

}

这是我的实现:

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

暂无
暂无

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

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