C++ version of NSScreen frame

Discussion in 'Mac Programming' started by MACaveli7, Oct 22, 2010.

  1. macrumors newbie

    Joined:
    Oct 15, 2010
    #1
    I have heard that you can use NSScreen frame to get the current resolution of the screen, is there an alternative version that doesn't need objective c code?
     
  2. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
  3. macrumors 6502

    Joined:
    Nov 30, 2005
    #3
    Even if there weren't, it would be sufficient to declare:

    ScreenFrame.h:

    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    CGRect GetMainScreenFrame();
    
    #ifdef __cplusplus
    }
    #endif
    
    ScreenFrame.m:

    Code:
    CGRect GetMainScreenFrame()
    {
        return [[NSScreen mainScreen] frame];
    }
    
    Or even just rename your .cpp file to .mm (Objective-C++) and put the [[NSScreen mainScreen] frame] right in there. Apple allow Objective-C, C and C++ to be freely intermingled.

    Note that you'll get the frame of the main screen. You can use [[window screen] frame] to get the frame for the screen that a particular NSWindow is on.
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    ^ NScreen should only be used within a proper GUI app. Otherwise you should use CGDisplay.

    IMO, NSScreen is a poorly written API and the CGDisplay functions are much clearer. e.g. mainScreen != CGMainDisplayID
     

Share This Page