使窗口位于应用程序的最顶层

发布于 2024-10-10 18:11:05 字数 255 浏览 8 评论 0原文

在我的应用程序中,我有一组窗口。我希望当应用程序处于活动状态时,其中一个窗口始终位于最顶层。我尝试通过更改窗口的级别来做到这一点,但没有成功。

如果我放置 NSNormalWindowLevel ,那么在单击应用程序的任何其他窗口时,该窗口将进入后台。而如果我使用除 NSNormalWindowLevel 之外的任何其他级别,则即使我切换到其他应用程序,该窗口仍保持在最上面。我希望只有当我的应用程序处于活动状态时窗口才位于最上面。如何在可可中执行此操作?

谢谢

In my application,I have a set of windows.I want one of the window to be top most all the time when the application is active.I tried doing this by changing the level of the window, but didn't succeed.

If I put NSNormalWindowLevel , then on clicking any other window of my application this window goes in background.While if I use any other level than NSNormalWindowLevel , then the window remains topmost even if I switch to some other application. I want the window to be topmost only when my application is active.How to do this in cocoa ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你的心境我的脸 2024-10-17 18:11:05

这是可能的,但只能以编程方式完成(这意味着不能在 Interface Builder 中):

如果您有一个窗口,则使用

[window setLevel: NSMainMenuWindowLevel];

如果您使用窗口控制器(NSWindowController),则

[windowController showWindow:self];
[windowController.window setLevel: NSMainMenuWindowLevel];

一般情况下您可以使用以下级别:

NSNormalWindowLevel       
NSFloatingWindowLevel   
NSSubmenuWindowLevel    
NSTornOffMenuWindowLevel
NSMainMenuWindowLevel   
NSStatusWindowLevel      
NSDockWindowLevel        
NSModalPanelWindowLevel   
NSPopUpMenuWindowLevel    
NSScreenSaverWindowLevel 

如果您想要在 Interfacebuilder 中显示一个最顶层的窗口(即从菜单中调用),请在您的 App-Delegate 中创建一个 IBAction 并将调用分配给此操作:

-(IBAction)showWindowCalledFromMenu:(id)sender {

   [self.window setLevel: NSMainMenuWindowLevel];
   [self.window makeKeyAndOrderFront:self];
}

如果您希望窗口仅在应用程序处于活动状态时位于最顶层,您可以了解您的应用程序是否处于活动状态。在“main.xib”中观察“First Responder”中的“actions”。您可以使用以下事件来重置窗口级别:“deminiaturize”、“order Front”、“order Back”等等。

希望这有帮助。玩得开心!

This is possible, but can be only done programatically (that means not in Interface Builder):

if you have a window, use

[window setLevel: NSMainMenuWindowLevel];

if you use a window controller (NSWindowController), use

[windowController showWindow:self];
[windowController.window setLevel: NSMainMenuWindowLevel];

in general you may use on of the following levels:

NSNormalWindowLevel       
NSFloatingWindowLevel   
NSSubmenuWindowLevel    
NSTornOffMenuWindowLevel
NSMainMenuWindowLevel   
NSStatusWindowLevel      
NSDockWindowLevel        
NSModalPanelWindowLevel   
NSPopUpMenuWindowLevel    
NSScreenSaverWindowLevel 

If you would like to show a window topmost from Interfacebuilder (i.e. a call from the menu), create a IBAction in your App-Delegate and assign the call to this action:

-(IBAction)showWindowCalledFromMenu:(id)sender {

   [self.window setLevel: NSMainMenuWindowLevel];
   [self.window makeKeyAndOrderFront:self];
}

If you want the window being topmost only when your application is active, you have to know if your application is active. In your "main.xib" watch into the "actions" from "First Responder". You can use the following events to reset your windowlevels: "deminiaturize", "order Front", "order Back" and a lot more.

Hope this helps. Have fun!

ぃ双果 2024-10-17 18:11:05

我最终做到了这一点,通过使用函数 setHidesonDeactivate 并使用窗口的 NSFloatingWindowLevel 在应用程序停用时隐藏 NSWindow。

I did this finally , by hiding the NSWindow when the application is deactivated by using function setHidesonDeactivate and using a NSFloatingWindowLevel for the window.

时间你老了 2024-10-17 18:11:05

复制/粘贴代码(查看deovrat singh答案)

-(void) windowDidLoad {
    [super windowDidLoad];
    [self.window setLevel: NSFloatingWindowLevel];
    [self.window setHidesonDeactivate:YES];

    //....
}

code for copy/paste (look deovrat singh answer)

-(void) windowDidLoad {
    [super windowDidLoad];
    [self.window setLevel: NSFloatingWindowLevel];
    [self.window setHidesonDeactivate:YES];

    //....
}
帅哥哥的热头脑 2024-10-17 18:11:05

OS X 窗口系统中实际上不存在“应用程序最顶层”这样的概念。应用程序没有自己的级别。完成听起来像您想要的事情的正常方法是拥有一个浮动面板,当应用程序处于非活动状态时该面板会消失(请参阅 NSPanel 的 setFloatingPanel:)。否则,您就必须自己切换窗位。

There is not really any such concept as "topmost within an application" in the OS X windowing system. An application does not have a level of its own. The normal way to do what it sounds like you want is to have a floating panel that disappears when the application is inactive (see NSPanel's setFloatingPanel:). Otherwise, you'd have to switch its window level around yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文