问题描述
我试图汇编一个简单的错误报告包。如果我的主程序崩溃,它保存crashlog,然后启动记者程序。记者程序询问用户是否可以发送崩溃日志给我,然后这样做。我使用NSRunAlertPanel创建一个基本的消息框。
I'm trying to put together a simple error reporting package. If my main program crashes, it saves a crashlog, then starts a reporter program. The reporter program asks the user if it can send the crash log to me, then does so. I'm using NSRunAlertPanel to create a basic message box.
由于某些原因,该消息框显示隐藏在任何其他可能打开的窗口下。从Finder窗口运行主程序包,它会显示在顶部,强制它崩溃,记者窗口会显示在 Finder窗口之后。
For some reason, that message box is showing up buried underneath any other windows that may be open. Run the main package from a Finder window, it shows up on top, force it to crash, the reporter window shows up behind the Finder window.
为什么会发生这种情况,如何解决?
Why is this happening, and how can it be solved?
最小测试用例:
#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
推荐答案
从许多切向相关的网页中蒸馏出的解决方案:
I seem to have come up with a solution, distilled from many tangentially-related webpages:
#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];
}
我不假装理解这一点 - 它是最好的货币邪教。更好的答案或每个步骤的解释,将非常感谢。
I do not pretend to understand this - it's cargo cult programming at its finest. Better answers, or explanations of what each step does, would be greatly appreciated.
这篇关于NSRunAlertPanel显示在“活动窗口”之后。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!