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

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

[复制链接]

尚未签到

发表于 2016-5-17 08:37:50 | 显示全部楼层 |阅读模式
  注:把变更现实分辨率的代码完成了。
  变更的时候,因为10.6不再提供诸如10.5之前的设置最佳分辨率的系统功能,所以需要自己编写。
  
  设置和显示显示分辨率-源码

下面是一个设置和显示当前系统显示设备和分辨率的源码
如果有兴趣,大家给测试一下各自的环境下,它工作的情况。

几个概念:
Display: 每个当前连接到系统中的显示器
Main Display: 当前的主显示设备
Mode: 是每个显示设备和系统之间连接后,系统自动生成的管理它的一个内存对象,分辨率是其中的一个部分,每个显示设备可能有好多mode,这些mode组成一个列表。

编译步骤:
首先要安装了xcode开发环境,把下面的源码复制到一个文本编辑器中,比如TextEdit,然后使用文本方式保存,比如该源码的文件命是screenresolution.m,保存在你的Desktop上。再编译:在Terminal里面输入命令:
cd ~/Desktopc++ screenresolution.m -framework ApplicationServices -o screenresolution -arch i386
这样就在你的Desktop上生成了一个交screenresolution的程序.

具体使用,在Termianl里面运行:

  • 获得帮助:~/Desktop/screenresolution -h
  • 获得当前显示器的个数:~/Desktop/screenresolution -a
  • 当前主显示器的分辨率:~/Desktop/screenresolution
  • 获得当前第2个显示器的分辨率:~/Desktop/screenresolution 2
  • 获得主显示器支持的分辨率列表:~/Desktop/screenresolution -l
  • 获得第2个显示器支持的分辨率列表:~/Desktop/screenresolution -l 2
  • 设置当前主显示器的分辨率为800x600:~/Desktop/screenresolution -s 800 600
  • 设置第2个显示器的分辨率为800x600:~/Desktop/screenresolution -s 2 800 600
  
注意:
这个版本是为OS X 10.6以上版本做的. 在10.6系统上编译成功并运行成功;没有在10.5上编译过;10.6编译成功的执行命令,也没有在10.5系统上运行过-应该是无法运行的-谁有条件给测试一下。因为10.6把好多10.5里面的底层函数都改了。

已知的:
  

  • 没有考虑两个显示器mirro的状态
  • 设置分辨率尽量使用系统列出的所支持的分辨率,否则设置可能不成功。
  

其它的:

  • 测试过的给个回信。
  • 希望新功能的, 请说
  • 这个程序主要是用来控制远程电脑的分辨率。
  
源代码
  
  /** 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.*    2011-06-08: Display adding flags and not display duplicate modes.*    2011-06-09: Adding set the best fit resolution function.** COMPILE:*    c++ screenresolution.m -framework ApplicationServices -o screenresolution -arch i386**/#include <ApplicationServices/ApplicationServices.h>struct sLIST {double width, height;CGDisplayModeRef mode;};typedef int (*compfn)(const void*, const void*);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 flag);int GetModeParms (CGDisplayModeRef mode, double *width, double *height, double *depth, double *freq, int *flag);int GetDisplayParms(CGDirectDisplayID disp, double *width, double *height, double *depth, double *freq, int *flag);bool GetBestDisplayMod(CGDirectDisplayID display, double dwidth, double dheight);int modecompare(struct sLIST *elem1, struct sLIST *elem2);uint32_t maxDisplays = 20;CGDirectDisplayID onlineDisplayIDs[20];uint32_t displayTotal;char *sysFlags[]={"Unknown","Interlaced,", "Multi-Display,", "Not preset,", "Stretched,"};char flagString[200];int main (int argc, const char * argv[]){double width, height, depth, freq;int flag;int displayNum;CGDirectDisplayID theDisplay;// 1. Getting system info.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 (! strcmp(argv[1],"-l")) {if (argc == 2) {ListDisplays(displayTotal);return 0;}else if (argc == 3)  {displayNum = atoi(argv[2]);if (displayNum <= displayTotal && displayNum > 0) {ListDisplayAllMode (onlineDisplayIDs[displayNum-1], 0);}}return 0;}if (! strcmp(argv[1],"-a")) {printf("Total online displays: %d/n", displayTotal);return 0;}if ((! strcmp(argv[1],"-?")) || (! strcmp(argv[1],"-h"))) {PrintUsage(argv);return 0;}if (! strcmp(argv[1],"-s")) {if (argc == 4) {displayNum = 1; width = atoi(argv[2]); height = atoi(argv[3]);}else if (argc == 5) {displayNum = atoi(argv[2]); width = atoi(argv[3]); height = atoi(argv[4]);}if (displayNum <= displayTotal)flag = GetBestDisplayMod(displayNum-1, width, height);return flag;}displayNum = atoi(argv[1]);if (displayNum <= displayTotal) {GetDisplayParms(onlineDisplayIDs[displayNum-1], &width, &height, &depth, &freq, &flag);PrintModeParms (width, height, depth, freq, flag);return 0;}else {fprintf(stderr, "ERROR: display number out of bounds; displays on this mac: %d./n", displayTotal);return -1;}return 0;}void ListDisplays(uint32_t displayTotal){uint32_t i;CGDisplayModeRef mode;double width, height, depth, freq;int flag;// 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, &flag);if ( i = 0 )printf(" (main) ");PrintModeParms (width, height, depth, freq, flag);}}void ListDisplayAllMode (CGDirectDisplayID displayID, int iNum){CFArrayRef modeList;CGDisplayModeRef mode;CFIndex index, count;double width, height, depth, freq;int flag;double width1, height1, depth1, freq1;int flag1;modeList = CGDisplayCopyAllDisplayModes (displayID, NULL);if (modeList == NULL)return;count = CFArrayGetCount (modeList);width1=0; height1=0; depth1=0; freq1=0; flag1=0;if (iNum <= 0) {for (index = 0; index < count; index++){mode = (CGDisplayModeRef)CFArrayGetValueAtIndex (modeList, index);GetModeParms(mode, &width, &height, &depth, &freq, &flag);PrintModeParms (width, height, depth, freq, flag);}}}else if (iNum <= count) {mode = (CGDisplayModeRef)CFArrayGetValueAtIndex (modeList, iNum-1);GetModeParms(mode, &width, &height, &depth, &freq, &flag);PrintModeParms (width, height, depth, freq, flag);}CFRelease(modeList);}void PrintModeParms (double width, double height, double depth, double freq, int flag){printf ("%ld x %ld x %ld @ %ld Hz, <%d>/n", (long int)width, (long int)height, (long int)depth, (long int)freq, flag);}int GetDisplayParms(CGDirectDisplayID disp, double *width, double *height, double *depth, double *freq, int *flag){int iReturn=0;CGDisplayModeRef Mode = CGDisplayCopyDisplayMode(disp);iReturn = GetModeParms (Mode, width, height, depth, freq, flag);CGDisplayModeRelease (Mode);return iReturn;}int GetModeParms (CGDisplayModeRef Mode, double *width, double *height, double *depth, double *freq, int *sflag){*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;*sflag = CGDisplayModeGetIOFlags(Mode);CFRelease(pixelEncoding);return 0;}bool GetBestDisplayMod(CGDirectDisplayID display, double dwidth, double dheight){CFArrayRef modeList;CGDisplayModeRef mode;CFIndex index, count, sindex, scount=0;double width, height, depth, freq;double width1, height1, depth1, freq1;int flag, flag1;struct sLIST mList[100];int ireturn=0;modeList = CGDisplayCopyAllDisplayModes (display, NULL);if (modeList == NULL)return;count = CFArrayGetCount (modeList);scount=0;for (index = 0; index < count; index++){mode = (CGDisplayModeRef)CFArrayGetValueAtIndex (modeList, index);GetModeParms(mode, &width, &height, &depth, &freq, &flag);// printf("........ scount=%d/n", (int)scount);if (!((width==width1) && (height==height1) && (depth==depth1) && (freq==freq1) && (flag==flag1))) {if (CGDisplayModeIsUsableForDesktopGUI(mode)) {mList[scount].mode=mode; mList[scount].width=width; mList[scount].height=height;width1=width; height1=height; depth1=depth; freq1=freq; flag1=flag;scount++;}}}mode=NULL;qsort ((void *) mList, scount, sizeof(struct sLIST), (compfn) modecompare);for (index=0; index<scount; index++){if (mList[index].width >= dwidth) {if (mList[index].height >= dheight) {mode = mList[index].mode;break;}}}CGDisplayConfigRef pConfigRef;CGConfigureOption option=kCGConfigurePermanently;if ((mode != NULL) && (CGBeginDisplayConfiguration(&pConfigRef) == kCGErrorSuccess)) {CGConfigureDisplayWithDisplayMode(pConfigRef, display, mode, NULL);if (CGCompleteDisplayConfiguration (pConfigRef, option) !=kCGErrorSuccess) CGCancelDisplayConfiguration (pConfigRef);}else ireturn = -1;CFRelease(modeList);return ireturn;}int modecompare(struct sLIST *elem1, struct sLIST *elem2){if ( elem1->width < elem2->width)return -1;else if (elem1->width > elem2->width) return 1;if (elem1->height < elem2->height) return -1;else if (elem1->height > elem2->height) return 1;else return 0;}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:");printf("       %s -a/n", fname);printf("       %s [-l] [1..9]/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/n");printf("    1..9  display # (default: main display)/n");printf("  -l 1-9  List all support for the display #/n");printf("      -s  Set 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);}

运维网声明 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-217955-1-1.html 上篇帖子: Mac OS X 系统自带拼音输入法里输入不会读的汉字的方法 下篇帖子: Installing JBoss Application Server 7.1 on Mac OS X with Eclipse Integration
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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