相关疑难解决方法(0)

显示窗口而不激活(将应用程序保持在其下方)

我需要在第三方应用程序上方显示一个窗口(没有标题栏),而我的窗口没有关注.

我尝试使用NSPanel和设置启用非激活,但这没有帮助.

我试过了orderFront:self,但这也没有帮助.

我总是需要添加,[NSApp activateIgnoringOtherApps:YES];因为窗口不会显示.

我这里有一个仅用于此功能的示例项目:http:
//users.telenet.be/prullen/TopW2.zip

UIElement被设置为true在应用程序的plist文件,所以没有码头.您可以ALT + SPACE同时按下来激活窗口.您将看到它下面的应用程序失去焦点.有关如何解决此问题的任何想法?我见过其他应用程序这样做,所以我知道它是可能的.

编辑:这是迄今为止的代码.请记住,窗口是非激活的NSPanel.我仍然需要最后NSApp activateIgnoringOtherApps一行,否则它不会显示.但当然这使得窗口成为活跃的窗口.

 _windowController = [[MyWindowController alloc] initWithWindowNibName:@"MyWindowController"];

    [[_windowController window] setLevel:NSNormalWindowLevel+1];
    [[_windowController window] orderFrontRegardless];

    [_windowController showWindow:self];

   [NSApp activateIgnoringOtherApps:YES];
Run Code Online (Sandbox Code Playgroud)

我还将NSPanel子类化,并添加了两种方法:

- (BOOL)canBecomeKeyWindow
{
    return YES;
}

- (BOOL)canBecomeMainWindow
{
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

编辑:好的,取消选中setHidesOnDeactivate会修复此问题,但现在窗口永远不会隐藏.当用户按下它下方的应用程序或切换到另一个应用程序时,我需要隐藏它.

编辑2:好的,这似乎解决了上述问题:

- (void)awakeFromNib
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideWindow) name:NSWindowDidResignKeyNotification object:nil];
}
- (void)hideWindow {
    [self setHidesOnDeactivate:YES];
}
Run Code Online (Sandbox Code Playgroud)

不确定是否有更好的方法.

对于那些想知道如何显示窗口的人:

    [[_windowController window] setLevel:NSPopUpMenuWindowLevel];
    [[_windowController window] …
Run Code Online (Sandbox Code Playgroud)

cocoa objective-c nsview nswindow nswindowcontroller

7
推荐指数
1
解决办法
2133
查看次数