I'd like to implement "Always on top" configuration option in my application that takes effect 开发者_如何学Pythonimmediately.
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.
精彩评论