windows 如何使 Shell 在 SWT 中始终处于领先地位?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2800893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a Shell to be always on top in SWT?
提问by parxier
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?
我知道我可以调用Shell
带有ON_TOP
样式的构造函数。有没有办法在运行时做到这一点,即在Shell
实例已经创建之后?
回答by Scott K.
In Cocoa you need to use reflection to get at the Shell instance variable window
and then call window.setLevel(OS.NSStatusWindowLevel)
.
在 Cocoa 中,您需要使用反射来获取 Shell 实例变量window
,然后调用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.
在 Carbon 中,您需要获取shellHandle
实例变量,然后调用OS.SetWindowGroup(shellHandle, OS.kFloatingWindowClass)
. 根据您的需要,您可能能够避免做那么多事情。
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.
在任何一种情况下,您还应该将SWT.ON_TOP
位强行添加到style
字段中。特别是在 Carbon 中,很多事情都依赖于该位的设置。
回答by Chris
On windows, you can do it like this:
在 Windows 上,您可以这样做:
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.
所有这些 api 都是公开的,所以不需要反思。
The last argument for SetWindowPos
is not the same with Shell.getStyle()
. Leaving it as 0 currently causing no problem for me.
for 的最后一个参数SetWindowPos
与 不同Shell.getStyle()
。将其保留为 0 目前对我来说没有问题。
回答by Lukasz Stelmach
One time I had similar problem and I had found such thread:
有一次我遇到了类似的问题,我发现了这样的线程:
http://dev.eclipse.org/newslists/news.eclipse.platform.swt/msg11143.html
http://dev.eclipse.org/newslists/news.eclipse.platform.swt/msg11143.html
Unfortunately I don't remember if it works...
不幸的是,我不记得它是否有效......
回答by Aaron Digulla
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
).
您必须检查在创建期间执行了哪些代码,然后调用特定的本机方法(在类中OS
)。
Download the source for SWT for your platform to see how it works. It's not magic, just a bit of manual debugging.
下载适用于您平台的 SWT 源代码以了解其工作原理。这不是魔术,只是一点手动调试。