gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:11:01 25 4
gpt4 key购买 nike

我一直在从事一个项目,在该项目中,我基于 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com