简体   繁体   中英

Get screen resolution programmatically in OS X

I'd like to launch a fullscreen 3D C++ application in native resolution on mac. 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. 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] .

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.

If you are looking for a multiplatform solution for both mac and 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. If you are using cmake, link your needed framework like the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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