The Quartz Display Services API provides several functions that adjust the display mode:
CGDisplayBestModeForParameters finds the display mode that is closest to a specified depth and screen size.
CGDisplayBestModeForParametersAndRefreshRate finds the display mode that is closest to a specified depth and resolution, and that also uses a refresh rate equal to or near the specified rate.
CGDisplayBestModeForParametersAndRefreshRateWithProperty finds the display mode that is closest to a specified depth, resolution, and refresh rate and that also has a specific property. Properties include whether the mode is safe for hardware, is interlaced, is stretched, or can provide output suitable for television.
If you want to adjust the display mode, you first need to capture the display, as shown in Listing 3-4. The Quartz Display Services function CGDisplaySwitchToMode switches to the display mode returned by the function CGDisplayBestModeForParameters, which in this case, is the best display mode for the main display with a bit depth of 32 bits per pixel and a screen resolution of 1024 by 768 pixels. The display mode that's returned is not always what you asked for. It's the closest mode for the given parameter. The last parameter passed to this function—exactMatch—specifies whether the returned display mode matches exactly. If you don't need this information, you can pass NULL. When your application quits, Quartz Display Services automatically restores the user’s display settings.
Note: Calling CGDisplaySwitchToMode does not guarantee that the display mode switches successfully. Displays have physical limitations that can prevent them from operating in a particular mode.
Listing 3-4 Adjusting the display mode
CGDisplayCapture (kCGDirectMainDisplay ) ; |
CGDisplaySwitchToMode (kCGDirectMainDisplay, |
CGDisplayBestModeForParameters (kCGDirectMainDisplay, |
32, 1024, 768, NULL) ); |
Listing 3-5 shows how to switch the main display to a pixel depth of 32 bits per pixel, a resolution of 640 x 480, and a refresh rate of 60 Hz. A detailed explanation for each numbered line of code appears following the listing.
Listing 3-5 Switching the resolution of a display
CFDictionaryRef displayMode ; |
CFNumberRef number ; |
boolean_t exactMatch ; |
CGDisplayCapture (kCGDirectMainDisplay); // 1 |
displayMode = |
CGDisplayBestModeForParametersAndRefreshRate (kCGDirectMainDisplay, |
32,640,480,60,&exactMatch); // 2 |
if (exactMatch){ // 3 |
CGDisplaySwitchToMode (kCGDirectMainDisplay, displayMode); |
} |
else { |
// Your code to take appropriate action |
} |
// Run the event loop. |
CGReleaseAllDisplays(); // 4 |
Here's what the code does:
Captures the main display.
Requests a display mode with a depth of 32 bits per pixel, a resolution 640 x 480, and a refresh rate 60 Hz. The function finds the best match for these parameters.
If there is an exact match, then switches to the display mode.
Before the application quits, releases all displays.
Last updated: 2008-06-09