牛骨文教育服务平台(让学习变的简单)

注:把变更现实分辨率的代码完成了。

变更的时候,因为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[i]); GetDisplayParms(onlineDisplayIDs[i], &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);}