SWT无标题栏窗口的拖动

当我们想用SWT做自定义风格的窗口时,最容易想到的就是去除Shell的Title,但是默认情况下没有Title的Shell是不能拖动的。以下代码实现无Title窗口的拖动:

Listener listener = new Listener() {
    int startX, startY;
    public void handleEvent(Event e) {
        if (e.type == SWT.MouseDown && e.button == 1) {
            startX = e.x;
            startY = e.y;
        }
        if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0) {
            Point p = shell.toDisplay(e.x, e.y);
            p.x -= startX;
            p.y -= startY;
            shell.setLocation(p);
        }
    }
};
shell.addListener(SWT.MouseDown, listener);
shell.addListener(SWT.MouseMove, listener);

你可能感兴趣的:(窗口,拖动,SWT,无标题栏)