简体   繁体   English

在 OS X 中以编程方式获取屏幕分辨率

[英]Get screen resolution programmatically in OS X

I'd like to launch a fullscreen 3D C++ application in native resolution on mac.我想在 Mac 上以原始分辨率启动全屏 3D C++ 应用程序。 How can I retrieve the native screen resolution ?如何检索本机屏幕分辨率?

If you don't wish to use Objective C, get the display ID that you wish to display on (using eg CGMainDisplayID ), then use CGDisplayPixelsWide and CGDisplayPixelsHigh to get the screen width and height, in pixels.如果您不想使用 Objective C,请获取您希望显示的显示 ID(使用例如CGMainDisplayID ),然后使用CGDisplayPixelsWideCGDisplayPixelsHigh获取屏幕宽度和高度(以像素为单位)。 See " Getting Information About Displays " for how to get other display information.有关如何获取其他显示信息,请参阅“ 获取有关显示器的信息”。

If you're willing to use a bit of Objective-C, simply use [[NSScreen mainScreen] frame] .如果您愿意使用一些 Objective-C,只需使用[[NSScreen mainScreen] frame]

Note that there are other concerns with full screen display, namely ensuring other applications don't do the same.请注意,全屏显示还有其他问题,即确保其他应用程序不会这样做。 Read " Drawing to the Full Screen " in Apple's OpenGL Programming Guide for more.阅读 Apple 的 OpenGL 编程指南中的“ 全屏绘制”以了解更多信息。

If you are looking for a multiplatform solution for both mac and windows如果您正在寻找适用于 mac 和 windows 的多平台解决方案

#include "ScreenSize.h"

#if WIN32
#include <windows.h>
#else
#include <CoreGraphics/CGDisplayConfiguration.h>
#endif

void ScreenSize::getScreenResolution(unsigned int& width, unsigned int& height) {
#if WIN32
    width = (int)GetSystemMetrics(SM_CXSCREEN);
    height = (int)GetSystemMetrics(SM_CYSCREEN);
#else
    auto mainDisplayId = CGMainDisplayID();
    width = CGDisplayPixelsWide(mainDisplayId);
    height = CGDisplayPixelsHigh(mainDisplayId);
#endif
}

Note: You also need to link the CoreGraphics framework to your project.注意:您还需要将 CoreGraphics 框架链接到您的项目。 If you are using cmake, link your needed framework like the following:如果您使用的是 cmake,请像下面这样链接您需要的框架:

    target_link_libraries(${PROJECT_NAME}
        "-framework CoreGraphics"
        "-framework Foundation"
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM