开发者

Get screen resolution programmatically in OS X

I'd like to laun开发者_Go百科ch 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 e.g. 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"
    )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜