Java SWT: How to set the window to be resizable \ not resizable on the fly?

最后都变了- 提交于 2019-12-11 04:32:07

问题


After a lot of search and research, I couldn't find any answer. I will very appreciate the person that could help me.

In SWT java, I want my window to be not resizable, so I define it like this:

shell = new Shell(SWT.CLOSE | SWT.TITLE | SWT.MIN);

But then I want to change it to be resizable (for example after I click a button). How can I change the style of the shell on the fly?


回答1:


You can't change the style of a Shell once it has been created so you can't do this directly.

What you can do is create a new Shell (and all its contents) with the desired style and set its bounds to be the same as the old Shell and then close the old shell. Eclipse does this when it wants to convert a tool tip window in to a normal window.




回答2:


If you develop exclusively for windows, you can achieve this with non pure SWT code (non portable). There are no visible glitches, no problems with re-parenting controls (see this bug, for example). Only the resizable property is changed. Here is the snippet:

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);

    final int style = OS.GetWindowLong(shell.handle, OS.GWL_STYLE);
    OS.SetWindowLong(shell.handle, OS.GWL_STYLE, style & ~0x00040000);

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

WS_SIZEBOX = 0x00040000L

Edit: simple functions to use:

private static final int WS_SIZEBOX = 0x00040000;

public static void setResizable(Shell shell, boolean resizable) {
    final int style = OS.GetWindowLong(shell.handle, OS.GWL_STYLE);
    final int newStyle = resizable ? style | WS_SIZEBOX : style & ~WS_SIZEBOX;
    OS.SetWindowLong(shell.handle, OS.GWL_STYLE, newStyle);
}

public static boolean isResizable(Shell shell) {
    final int style = OS.GetWindowLong(shell.handle, OS.GWL_STYLE);
    return (style & WS_SIZEBOX) != 0;
}


来源:https://stackoverflow.com/questions/19875083/java-swt-how-to-set-the-window-to-be-resizable-not-resizable-on-the-fly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!