【问题标题】:NSRunAlertPanel shows up behind the "active window"NSRunAlertPanel 出现在“活动窗口”后面
【发布时间】:2010-04-14 17:12:36
【问题描述】:

我正在尝试编写一个简单的错误报告包。如果我的主程序崩溃,它会保存一个崩溃日志,然后启动一个报告程序。报告程序询问用户是否可以将崩溃日志发送给我,然后发送。我正在使用 NSRunAlertPanel 创建一个基本的消息框。

由于某种原因,该消息框显示在任何其他可能打开的窗口下方。从 Finder 窗口运行主程序包,它显示在顶部,强制它崩溃,报告器窗口显示在 在 Finder 窗口的后面

为什么会发生这种情况,如何解决?

最小测试用例:

#import <AppKit/AppKit.h>

int main(int a, char* av) {
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  NSApplication* q = [[NSApplication alloc] init]; 
  NSRunAlertPanel(@"Hello", @"Aloha", @"OK", nil,nil);
  [pool release];
} 

内置:

g++ test.mm -framework AppKit && ./a.out

【问题讨论】:

    标签: cocoa dialog messagebox foreground nsrunalertpanel


    【解决方案1】:

    我似乎想出了一个解决方案,从许多与切线相关的网页中提炼出来:

    #import <AppKit/AppKit.h>
    
    int main(int a, char* av) {
      NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
      NSApplication* q = [[NSApplication alloc] init]; 
    
      ProcessSerialNumber psn = {0, kCurrentProcess};
      TransformProcessType(&psn, kProcessTransformToForegroundApplication);
    
      [NSApp activateIgnoringOtherApps:YES];
    
      NSRunAlertPanel(@"Hello", @"Aloha", @"OK", nil,nil);
      [pool release];
    } 
    

    我不会假装理解这一点 - 这是最好的货物崇拜节目。非常感谢您提供更好的答案或对每个步骤的解释。

    【讨论】:

    • 它将您的应用程序带到了最前面。为什么你在没有事件循环的情况下在 main 中执行此操作,我不知道。
    • “应用程序”是一个 150 行的程序,它询问用户一个问题,通过 curl 执行几个简单的 HTTP 请求,并可能向用户发送一条短消息。它并不真正需要一个完整的 GUI,我也不想要一个 - 我宁愿让它尽可能跨平台,我真正需要的只是相当于 Windows MessageBox()。
    • 你想要什么并不重要,框架需要一个事件循环。你与这个约定的斗争很可能完全是浪费时间。
    猜你喜欢
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 2017-11-21
    • 2019-05-25
    相关资源
    最近更新 更多