objective-c - NSWindow 始终位于顶部(或不可见)

标签 objective-c cocoa window manual

我一直在从事一个项目,在该项目中,我基于 GLFW 的代码在 Objective C 中手动创建一个 float NSWindow。问题是,每当我运行以下代码时,窗口绝对位于所有内容之上。当切换到其他应用程序时,它仍然位于其他所有应用程序之上,直到我关闭我的应用程序。我想要的是典型的 OS X 窗口行为,它将位于其他已“事件”的窗口后面。

问题似乎与我调用 [window orderFront:nil] 的行有关。但如果我不调用它,那么我的窗口根本不会出现。有谁知道这可能是什么原因造成的?

@interface MyApplicationDelegate : NSObject
@end

@implementation MyApplicationDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
    [NSApp stop:nil];
}

@end

@interface MyWindowDelegate : NSObject
@end

@implementation MyWindowDelegate

- (BOOL)windowShouldClose:(id)sender
{
    return NO;
}

@end


int main(int argc, const char * argv[]) {

[NSApplication sharedApplication];

MyApplicationDelegate* appDelegate = [[MyApplicationDelegate alloc] init];

[NSApp setDelegate:appDelegate];
[NSApp run];


NSRect contentRect = NSMakeRect(0, 0, 400, 400);
unsigned int styleMask = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask;

NSWindow* window = [[NSWindow alloc]
                     initWithContentRect:contentRect
                     styleMask:styleMask
                     backing:NSBackingStoreBuffered
                     defer:NO];

[window center];
[window setLevel:NSFloatingWindowLevel];

MyWindowDelegate* windowDelegate = [[MyWindowDelegate alloc] init];

[window setTitle:@"Manual"];
[window setDelegate:windowDelegate];

[window orderFront:nil];

while (true)
{
    for (;;)
    {
        NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
                                            untilDate:[NSDate distantPast]
                                               inMode:NSDefaultRunLoopMode
                                              dequeue:YES];
        if (event == nil)
            break;

        [NSApp sendEvent:event];
    }
}
}

最佳答案

我想我可能已经找到了解决方案。如果我添加这个:

[window setHidesOnDeactivate:true];                      

...然后窗口的行为正如我所期望的那样。令我有点困惑的是,默认情况下它并未启用,但它似乎可以满足我的需要。

关于objective-c - NSWindow 始终位于顶部(或不可见),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28585960/

相关文章:

objective-c - NSToolbar 在 Leopard 和 Snow Leopard 中看起来不同

cocoa - 本地文件的 NSURLThumbnailDictionaryKey 为空

C# Window Form Application,如何高效地在窗口窗体上创建动态面板

iphone - 我正在尝试在 map View 上放置多个图钉,但出现错误

ios - 方法中返回 Json 对象,无法将 NSDictionary 转换为 Void

swift - 使用 Swift 垂直对齐 NSTextField 中的文本

vb.net - 如何制作表单后面窗口的屏幕截图?

c++ - 如何捕获 EM_SHOWBALLOONTIP CEdit 消息?

用于 Web URL 的 iOS 孟加拉语文本解析器

iphone - 裁剪和缩放 CGContext - iOS