|
这个程序显示当前在线显示器的个数,当前主显示的分辨率,各个显示器的当前分辨率,各个显示器所支持的分辨率列表,以及各个显示器所支持的分辨率列表中某一个值。目前还没有包括设置分辨率的代码。它在Mac OS X 10.6的机器上编译成功运行。
/** screenresolution.m* * Description:* It set/get current Online display resolutions.** The version on webpage http://forums.macosxhints.com/showthread.php?t=59575* is before 10.6. This is for 10.6 and more functions.** Author:* Tony Liu, June 2011** Version History:* 2011-06-01: add -a option.** COMPILE:* c++ setgetscreenres.m -framework ApplicationServices -o setgetscreenres -arch i386 -arch ppc* -mmacosx-version-min=10.3.9**/#include <ApplicationServices/ApplicationServices.h>void ListDisplays(uint32_t displayTotal);void ListDisplayAllMode (CGDirectDisplayID displayID, int index);void PrintUsage(const char *argv[]);void PrintModeParms (double width, double height, double depth, double freq);int GetModeParms (CGDisplayModeRef mode, double *width, double *height, double *depth, double *freq);int GetDisplayParms(CGDirectDisplayID disp, double *width, double *height, double *depth, double *freq);uint32_t maxDisplays = 20;CGDirectDisplayID onlineDisplayIDs[20];uint32_t displayTotal;int main (int argc, const char * argv[]){double width, height, depth, freq;int displayNum;CGDirectDisplayID theDisplay;// 1. Getting system info.// CGError isErr=CGGetOnlineDisplayList (maxDisplays, onlineDisplayIDs, &displayTotal);if (CGGetOnlineDisplayList (maxDisplays, onlineDisplayIDs, &displayTotal) != kCGErrorSuccess) {printf("Error on getting online display List.");return -1;}if (argc == 1) {CGRect screenFrame = CGDisplayBounds(kCGDirectMainDisplay);CGSize screenSize = screenFrame.size;printf("%.0f %.0f/n", screenSize.width, screenSize.height);return 0;}if (argc == 2) {if (! strcmp(argv[1],"-l")) {ListDisplays(displayTotal);return 0;}else if (! strcmp(argv[1],"-?")) {PrintUsage(argv);return 0;}else if (! strcmp(argv[1],"-h")) {PrintUsage(argv);return 0;}else if (! strcmp(argv[1],"-a")) {printf("Total online displays: %d/n", displayTotal);return 0;}else if (displayNum = atoi(argv[1])) {if (displayNum <= displayTotal) {GetDisplayParms(onlineDisplayIDs[displayNum-1], &width, &height, &depth, &freq);PrintModeParms (width, height, depth, freq);return 0;}else {fprintf(stderr, "ERROR: display number out of bounds; displays on this mac: %d./n", displayTotal);return -1;}}}if (argc == 3) {if (! strcmp(argv[1],"-l")) {displayNum = atoi(argv[2]);if (displayNum <= displayTotal && displayNum > 0) {ListDisplayAllMode (onlineDisplayIDs[displayNum-1], 0);}return 0;}}if (argc == 4) {if (! strcmp(argv[1],"-l")) {displayNum = atoi(argv[2]);if (displayNum <= displayTotal && displayNum > 0) {ListDisplayAllMode (onlineDisplayIDs[displayNum-1], atoi(argv[3]));}return 0;}}if (argc == 4 && (displayNum = atoi(argv[1])) && (width = atoi(argv[2])) && (height = atoi(argv[3])) ) {if (displayNum <= displayTotal) {theDisplay= onlineDisplayIDs[displayNum-1];}else return -1;}else {if (argc != 3 || !(width = atoi(argv[1])) || !(height = atoi(argv[2])) ) {fprintf(stderr, "ERROR: syntax error./n", argv[0]);PrintUsage(argv);return -1;}theDisplay = CGMainDisplayID();}/* switchMode = CGDisplayBestModeForParameters(theDisplay, 32, h, v, NULL);if (! MyDisplaySwitchToMode(theDisplay, switchMode)) {fprintf(stderr, "Error changing resolution to %d %d/n", h, v);return 1;}*/return 0;}void ListDisplays(uint32_t displayTotal){uint32_t i;CGDisplayModeRef mode;double width, height, depth, freq;// CGDirectDisplayID mainDisplay = CGMainDisplayID();printf("Total Online Displays: %d/n", displayTotal);for(i = 0 ; i < displayTotal ; i++ ) {printf (" Display %d (id %d): ", i+1, onlineDisplayIDs);GetDisplayParms(onlineDisplayIDs, &width, &height, &depth, &freq);if ( i = 0 )printf(" (main) ");PrintModeParms (width, height, depth, freq);}}void ListDisplayAllMode (CGDirectDisplayID displayID, int iNum){CFArrayRef modeList;CGDisplayModeRef mode;CFIndex index, count;double width, height, depth, freq;modeList = CGDisplayCopyAllDisplayModes (displayID, NULL);if (modeList == NULL)return;count = CFArrayGetCount (modeList);if (iNum <= 0) {for (index = 0; index < count; index++){mode = (CGDisplayModeRef)CFArrayGetValueAtIndex (modeList, index);GetModeParms(mode, &width, &height, &depth, &freq);PrintModeParms (width, height, depth, freq);}}else if (iNum <= count) {mode = (CGDisplayModeRef)CFArrayGetValueAtIndex (modeList, iNum-1);GetModeParms(mode, &width, &height, &depth, &freq);PrintModeParms (width, height, depth, freq);}CFRelease(modeList);}void PrintModeParms (double width, double height, double depth, double freq){printf ("%ld x %ld x %ld @ %ld Hz/n", (long int)width, (long int)height, (long int)depth, (long int)freq);}int GetModeParms (CGDisplayModeRef Mode, double *width, double *height, double *depth, double *freq){*width = CGDisplayModeGetWidth (Mode);*height = CGDisplayModeGetHeight (Mode);*freq = CGDisplayModeGetRefreshRate (Mode);CFStringRef pixelEncoding = CGDisplayModeCopyPixelEncoding (Mode);*depth = 0;if (pixelEncoding = NULL) return -1;if (pixelEncoding = CFSTR(IO32BitDirectPixels))*depth = 32;else if (pixelEncoding = CFSTR(IO16BitDirectPixels))*depth = 16;else*depth = 8;CFRelease(pixelEncoding);return 0;}int GetDisplayParms(CGDirectDisplayID disp, double *width, double *height, double *depth, double *freq){int iReturn=0;CGDisplayModeRef Mode = CGDisplayCopyDisplayMode(disp);iReturn = GetModeParms (Mode, width, height, depth, freq);CGDisplayModeRelease (Mode);return iReturn;}void PrintUsage(const char *argv[]){char *fname = strrchr(argv[0], '/')+1;printf("Screen Resolution v1.0, Mac OS X 10.6 or later, i386/n");printf("Copyright 2010 Tony Liu. All rights reserved. June 1, 2010/n");printf("/nUsage: %s -a/n", fname);printf(" %s [-l | 1..9 | -l 1 ..9 ] [ index ]/n", fname);printf(" %s -s [ 1..9 ] hor_res vert_res/n", fname);printf(" %s -? | -h this help./n/n", fname);printf(" -l list resolution, depth and refresh rate of all displays/n");printf(" 1..9 display # (default: main display)/n");printf(" -l 1-9 List all support for display #/n");printf(" -s Set mode./n");printf(" [1..9] Set the display mode./n");printf(" hor_res horizontal resolution/n");printf("vert_res vertical resolution/n/n");printf("Examples:/n");printf("%s -a get online display number/n", fname);printf("%s get current main diplay resolution/n", fname);printf("%s 3 get current resolution of third display/n", fname);printf("%s -l get resolution, bit depth and refresh rate of all displays/n", fname);printf("%s -l 1 get first display all supported mode/n", fname);printf("%s -l 1 2 get first display the second supported mode/n", fname);printf("%s -s 800 600 set resolution of main display to 800x600/n", fname);printf("%s -s 2 800 600 set resolution of secondary display to 800x600/n", fname);}
Tony Liu, June 2011
in Calgary |
|