2

我没有太多的 Mac 编程经验,我来自 Windows 背景。

所以,我使用这段代码来显示一个 NSOpenPanel,但我还想在对话框出现时指定它在屏幕上的位置和大小。我怎么做?

NSOpenPanel *openPanel  = [NSOpenPanel openPanel];
[openPanel setAllowsMultipleSelection:NO];
[openPanel setCanChooseDirectories:YES];
[openPanel setCanChooseFiles:NO];
NSInteger result    = [openPanel runModalForTypes:nil];

编辑


似乎通过调用

[openPanel setFrame:NSMakeRect(0, 0, 500, 500) display:YES];

我可以设置 NSOpenPanel 的大小(它变成 500x500 像素),但不能设置它的左上角,它以屏幕为中心,而不是出现在屏幕的左上角。

4

2 回答 2

4

您必须继承 NSOpenPanel 并覆盖该center方法,该方法在面板放置在屏幕上之前被调用。以下是您将其放在右上角的方法:

- (void)center {

    NSRect myFrame = [self frame];
    NSRect screenFrame = [[self screen] visibleFrame];

    myFrame.size.height = round(screenFrame.size.height / 2);
    myFrame.origin.x = screenFrame.origin.x + screenFrame.size.width - myFrame.size.width;
    myFrame.origin.y = screenFrame.origin.y + screenFrame.size.height - myFrame.size.height;

    [self setFrame:myFrame display:YES];
}
于 2011-12-20T20:18:30.013 回答
1

NSOpenPanel 继承自 NSWindow。我从未尝试过,但您似乎应该能够使用普通的 NSWindow 方法来设置它的大小和位置。

于 2011-12-20T16:01:53.490 回答