简体   繁体   English

如何使Shell在SWT中始终处于领先地位?

[英]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. 我想在我的应用程序中实现“Always on top”配置选项,该选项立即生效。

I know that I can call Shell constructor with ON_TOP style. 我知道我可以使用ON_TOP样式调用Shell构造函数。 Is there a way to do that at runtime, that is after a Shell instance has already been created? 有没有办法在运行时执行此操作,即在已创建Shell实例之后?

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) . 在Carbon中,您需要获取shellHandle实例变量,然后调用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. 在任何一种情况下,您还应该强制将SWT.ON_TOP位添加到style字段。 Particularly in Carbon, lots of things rely on that bit being set. 特别是在Carbon中,很多东西都依赖于那个位置。

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() . SetWindowPos的最后一个参数与Shell.getStyle() Leaving it as 0 currently causing no problem for me. 将它保留为0目前对我没有任何问题。

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... 不幸的是我不记得它是否有效......

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. 下载适用于您的平台的SWT源,了解它的工作原理。 It's not magic, just a bit of manual debugging. 这不是魔术,只是一些手动调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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