gpt4 book ai didi

objective-c - 如何设置 NSOpenPanel 对话框的位置和大小?

转载 作者:行者123 更新时间:2023-12-03 16:27:16 26 4
gpt4 key购买 nike

我没有太多 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 像素),但不能设置它的左上角,它以屏幕为中心,而不是出现在屏幕的左上角。

最佳答案

您必须子类化 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];
}

关于objective-c - 如何设置 NSOpenPanel 对话框的位置和大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8577807/

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