简体   繁体   中英

How to make a Shell to be always on top in SWT?

I'd like to implement "Always on top" configuration option in my application that takes effect immediately.

I know that I can call Shell constructor with ON_TOP style. Is there a way to do that at runtime, that is after a Shell instance has already been created?

In Cocoa you need to use reflection to get at the Shell instance variable window and then call window.setLevel(OS.NSStatusWindowLevel) .

In Carbon you need to get at the shellHandle instance variable and then call OS.SetWindowGroup(shellHandle, OS.kFloatingWindowClass) . You might be able to get away with doing just that much, depending on your needs.

In either case, you should also forcibly add the SWT.ON_TOP bit to the style field. Particularly in Carbon, lots of things rely on that bit being set.

On windows, you can do it like this:

private static final void toggleAlwaysOnTop(Shell shell, boolean isOnTop){
    long handle = shell.handle;
    Point location = shell.getLocation();
    Point dimension = shell.getSize();
    OS.SetWindowPos(handle, isOnTop ? OS.HWND_TOPMOST : OS.HWND_NOTOPMOST,location.x, location.y, dimension.x, dimension.y, 0);
}

All those api's are public so there is no need for reflection.

The last argument for SetWindowPos is not the same with Shell.getStyle() . Leaving it as 0 currently causing no problem for me.

One time I had similar problem and I had found such thread:

http://dev.eclipse.org/newslists/news.eclipse.platform.swt/msg11143.html

Unfortunately I don't remember if it works...

There is no standard way to change the styles of widgets after they have been created.

You must check which code gets executed during creation time and then call the specific native method (in the class OS ).

Download the source for SWT for your platform to see how it works. It's not magic, just a bit of manual debugging.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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