gpt4 book ai didi

cocoa - 在 Mac OS X 中以编程方式获取屏幕尺寸

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

我可以使用以下方法返回屏幕尺寸:

- (void) getScreenResolution {

NSArray *screenArray = [NSScreen screens];
NSScreen *mainScreen = [NSScreen mainScreen];
unsigned screenCount = [screenArray count];
unsigned index = 0;

for (index; index < screenCount; index++)
{
NSScreen *screen = [screenArray objectAtIndex: index];
NSRect screenRect = [screen visibleFrame];
NSString *mString = ((mainScreen == screen) ? @"Main" : @"not-main");

NSLog(@"Screen #%d (%@) Frame: %@", index, mString, NSStringFromRect(screenRect));
}
}

输出:

Screen #0 (Main) Frame: {{0, 4}, {1344, 814}}

是否可以将 {1344, 814} 格式设置为 1344x814

<小时/>

编辑:

这非常有效:

- (NSString*) screenResolution {

NSRect screenRect;
NSArray *screenArray = [NSScreen screens];
unsigned screenCount = [screenArray count];
unsigned index = 0;

for (index; index < screenCount; index++)
{
NSScreen *screen = [screenArray objectAtIndex: index];
screenRect = [screen visibleFrame];
}

return [NSString stringWithFormat:@"%.1fx%.1f",screenRect.size.width, screenRect.size.height];
}

最佳答案

在 Swift 4.0 中你可以获取主屏幕的屏幕尺寸:

if let screen = NSScreen.main {
let rect = screen.frame
let height = rect.size.height
let width = rect.size.width
}

如果您要查找特定现有窗口的屏幕大小,您可以通过以下方式获取:

var window: NSWindow = ... //The Window laying on the desired screen
var screen = window.screen!
var rect = screen.frame
var height = rect.size.height
var width = rect.size.width

关于cocoa - 在 Mac OS X 中以编程方式获取屏幕尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4982656/

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