设为首页 收藏本站
查看: 778|回复: 0

[经验分享] Mac OS X:显示分辨率的命令(源程序)

[复制链接]

尚未签到

发表于 2016-5-16 09:31:05 | 显示全部楼层 |阅读模式
  
  这个程序显示当前在线显示器的个数,当前主显示的分辨率,各个显示器的当前分辨率,各个显示器所支持的分辨率列表,以及各个显示器所支持的分辨率列表中某一个值。目前还没有包括设置分辨率的代码。它在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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-217570-1-1.html 上篇帖子: Mac OS X: Enable/Disable/Login Root user and Others 下篇帖子: Mac Tips
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表