2

我正在尝试组合一个简单的错误报告包。如果我的主程序崩溃,它会保存一个崩溃日志,然后启动一个报告程序。报告程序询问用户是否可以将崩溃日志发送给我,然后发送。我正在使用 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
4

1 回答 1

2

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

#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];
} 

我不会假装理解这一点——这是最好的货物崇拜节目。将不胜感激更好的答案或对每个步骤的解释。

于 2010-04-14T18:35:14.750 回答