如何显示所有支持的分辨率以在Mac App中显示

时间:2019-04-12 11:45:39

标签: objective-c macos cocoa quartz-core

我想显示所有兼容的屏幕分辨率,但只获得很少的分辨率,即通过nil时获得的分辨率  达到CGDisplayCopyAllDisplayModes相同的分辨率,即使我将kCGDisplayShowDuplicateLowResolutionModes传递给kCFBooleanTrue我正在使用的以下块。

+(NSArray *)menuItemsForCurrentDisplay :(CGDirectDisplayID)display{

    //Get Current mode

    CGDisplayModeRef currentMode = CGDisplayCopyDisplayMode(display);

    // Use a dictionary with title keys to avoid 'duplicates'
    NSMutableDictionary *menuItemsByTitle = [NSMutableDictionary new];

    // Get Number of Supported Displays
//    NSArray* numberOfDisplayModes  =  (__bridge NSArray *)(CGDisplayCopyAllDisplayModes ( display, nil ));

    CFStringRef keys[1] = { kCGDisplayShowDuplicateLowResolutionModes };
    CFBooleanRef values[1] = { kCFBooleanTrue };

    CFDictionaryRef options = CFDictionaryCreate(kCFAllocatorDefault, (const void**) keys, (const void**) values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks );

    NSArray* numberOfDisplayModes  =  (__bridge NSArray *)CGDisplayCopyAllDisplayModes(display, options);
    for (int i = 0; i < numberOfDisplayModes.count; i++) {

        if (CGDisplayModeIsUsableForDesktopGUI((__bridge CGDisplayModeRef)(numberOfDisplayModes[i]))) {


            DisplayModeMenuItem *menuItem = [[DisplayModeMenuItem alloc] initWithCustomDisplay:display andMode:(__bridge CGDisplayModeRef)(numberOfDisplayModes[i])];
            DisplayModeMenuItem *previousMenuItem = menuItemsByTitle[menuItem.title];

            if (previousMenuItem == nil) {
                if (currentMode == (__bridge CGDisplayModeRef)(numberOfDisplayModes[i])|| previousMenuItem.state == NSOnState) {
                    [menuItem setState:NSOnState];
                }

                menuItemsByTitle[menuItem.title] = menuItem;
            }else if ((__bridge CGDisplayModeRef)(numberOfDisplayModes[i]) == currentMode ){

                [menuItem setState:NSOnState];
            }
        }

    }

     CFRelease(options);
    CGDisplayModeRelease(currentMode);
    NSLog(@"menuItemsByTitle %@",[menuItemsByTitle allValues]);
    return [[menuItemsByTitle allValues] sortedArrayUsingSelector:@selector(customCompare:)];


}

0 个答案:

没有答案