Mac OS X:顯示/設定解析度的命令(來源程式)

來源:互聯網
上載者:User

註:把變更現實解析度的程式碼完成了。

變更的時候,因為10.6不再提供諸如10.5之前的設定最佳解析度的系統功能,所以需要自己編寫。

 

設定和顯示顯示解析度-源碼

下面是一個設定和顯示當前系統
顯示裝置和解析度的源碼
如果有興趣,大家給測試一下各自的環境下,它工作的情況。

幾個概念:

Display: 每個當前串連到系統中的顯示器
Main Display: 當前的主顯示裝置
Mode: 是每個顯示裝置和系統之間串連後,系統自動產生的管理它的一個記憶體
對象,解析度是其中的一個部分,每個顯示裝置可能有好多mode,這些mode組成一個列表。

編譯步驟

   首先要安裝了xcode開發
環境,把下面的源碼複製到一個文字編輯器中,比如TextEdit,然後使用文本方式儲存,比如該源碼的檔案
命是screenresolution.m,儲存在你的Desktop上。再編譯:在Terminal裡面輸入命令:
cd ~/Desktop<br />c++ screenresolution.m -framework ApplicationServices -o screenresolution -arch i386<br />
這樣就在你的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的狀態
  • 設定解析度盡量使用系統列出的所支援的解析度,否則設定可能不成功。

其它的:

  • 測試過的給個回信。
  • 希望新功能的, 請說
  • 這個程式主要是用來控制遠程電腦的解析度。


原始碼

 

/*<br /> * screenresolution.m<br /> *<br /> * Description:<br /> * It set/get current Online display resolutions.<br /> *<br /> * The version on webpage http://forums.macosxhints.com/showthread.php?t=59575<br /> * is before 10.6. This is for 10.6 and more functions.<br /> *<br /> * Author:<br /> * Tony Liu, June 2011<br /> *<br /> * Version History:<br /> * 2011-06-01: add -a option.<br /> * 2011-06-08: Display adding flags and not display duplicate modes.<br /> * 2011-06-09: Adding set the best fit resolution function.<br /> *<br /> * COMPILE:<br /> * c++ screenresolution.m -framework ApplicationServices -o screenresolution -arch i386<br /> *<br /> */<br />#include <ApplicationServices/ApplicationServices.h><br />struct sLIST {<br />double width, height;<br />CGDisplayModeRef mode;<br />};<br />typedef int (*compfn)(const void*, const void*);<br />void ListDisplays(uint32_t displayTotal);<br />void ListDisplayAllMode (CGDirectDisplayID displayID, int index);<br />void PrintUsage(const char *argv[]);<br />void PrintModeParms (double width, double height, double depth, double freq, int flag);<br />int GetModeParms (CGDisplayModeRef mode, double *width, double *height, double *depth, double *freq, int *flag);<br />int GetDisplayParms(CGDirectDisplayID disp, double *width, double *height, double *depth, double *freq, int *flag);<br />bool GetBestDisplayMod(CGDirectDisplayID display, double dwidth, double dheight);<br />int modecompare(struct sLIST *elem1, struct sLIST *elem2);<br />uint32_t maxDisplays = 20;<br />CGDirectDisplayID onlineDisplayIDs[20];<br />uint32_t displayTotal;<br />char *sysFlags[]={"Unknown","Interlaced,", "Multi-Display,", "Not preset,", "Stretched,"};<br />char flagString[200];<br />int main (int argc, const char * argv[])<br />{<br />double width, height, depth, freq;<br />int flag;<br />int displayNum;<br />CGDirectDisplayID theDisplay;</p><p>// 1. Getting system info.<br />if (CGGetOnlineDisplayList (maxDisplays, onlineDisplayIDs, &displayTotal) != kCGErrorSuccess) {<br />printf("Error on getting online display List.");<br />return -1;<br />}</p><p>if (argc == 1) {<br /> CGRect screenFrame = CGDisplayBounds(kCGDirectMainDisplay);<br />CGSize screenSize = screenFrame.size;<br />printf("%.0f %.0f/n", screenSize.width, screenSize.height);<br />return 0;<br />}</p><p>if (! strcmp(argv[1],"-l")) {<br />if (argc == 2) {<br />ListDisplays(displayTotal);<br />return 0;<br />}<br />else if (argc == 3) {<br />displayNum = atoi(argv[2]);<br />if (displayNum <= displayTotal && displayNum > 0) {<br />ListDisplayAllMode (onlineDisplayIDs[displayNum-1], 0);<br />}<br />}<br />return 0;<br />}</p><p>if (! strcmp(argv[1],"-a")) {<br />printf("Total online displays: %d/n", displayTotal);<br />return 0;<br />}</p><p>if ((! strcmp(argv[1],"-?")) || (! strcmp(argv[1],"-h"))) {<br />PrintUsage(argv);<br />return 0;<br />}<br />if (! strcmp(argv[1],"-s")) {<br />if (argc == 4) {<br />displayNum = 1; width = atoi(argv[2]); height = atoi(argv[3]);<br />}<br />else if (argc == 5) {<br />displayNum = atoi(argv[2]); width = atoi(argv[3]); height = atoi(argv[4]);<br />}<br />if (displayNum <= displayTotal)<br />flag = GetBestDisplayMod(displayNum-1, width, height);<br />return flag;<br />}<br />displayNum = atoi(argv[1]);<br />if (displayNum <= displayTotal) {<br />GetDisplayParms(onlineDisplayIDs[displayNum-1], &width, &height, &depth, &freq, &flag);<br />PrintModeParms (width, height, depth, freq, flag);<br />return 0;<br />}<br />else {<br />fprintf(stderr, "ERROR: display number out of bounds; displays on this mac: %d./n", displayTotal);<br />return -1;<br />}<br />return 0;<br />}<br />void ListDisplays(uint32_t displayTotal)<br />{<br />uint32_t i;<br />CGDisplayModeRef mode;<br />double width, height, depth, freq;<br />int flag;</p><p>// CGDirectDisplayID mainDisplay = CGMainDisplayID();<br />printf("Total Online Displays: %d/n", displayTotal);<br />for(i = 0 ; i < displayTotal ; i++ ) {<br />printf (" Display %d (id %d): ", i+1, onlineDisplayIDs[i]);<br />GetDisplayParms(onlineDisplayIDs[i], &width, &height, &depth, &freq, &flag);<br />if ( i = 0 )printf(" (main) ");<br />PrintModeParms (width, height, depth, freq, flag);<br />}<br />}<br />void ListDisplayAllMode (CGDirectDisplayID displayID, int iNum)<br />{<br /> CFArrayRef modeList;<br /> CGDisplayModeRef mode;<br /> CFIndex index, count;<br /> double width, height, depth, freq;<br /> int flag;<br />double width1, height1, depth1, freq1;<br />int flag1;</p><p> modeList = CGDisplayCopyAllDisplayModes (displayID, NULL);<br /> if (modeList == NULL)return;<br /> count = CFArrayGetCount (modeList);<br /> width1=0; height1=0; depth1=0; freq1=0; flag1=0;<br /> if (iNum <= 0) {<br />for (index = 0; index < count; index++)<br />{<br />mode = (CGDisplayModeRef)CFArrayGetValueAtIndex (modeList, index);<br />GetModeParms(mode, &width, &height, &depth, &freq, &flag);<br />PrintModeParms (width, height, depth, freq, flag);<br />}<br />}<br />}<br />else if (iNum <= count) {<br />mode = (CGDisplayModeRef)CFArrayGetValueAtIndex (modeList, iNum-1);<br />GetModeParms(mode, &width, &height, &depth, &freq, &flag);<br />PrintModeParms (width, height, depth, freq, flag);<br />}<br /> CFRelease(modeList);<br />}<br />void PrintModeParms (double width, double height, double depth, double freq, int flag)<br />{<br />printf ("%ld x %ld x %ld @ %ld Hz, <%d>/n", (long int)width, (long int)height, (long int)depth, (long int)freq, flag);<br />}<br />int GetDisplayParms(CGDirectDisplayID disp, double *width, double *height, double *depth, double *freq, int *flag)<br />{<br />int iReturn=0;<br />CGDisplayModeRef Mode = CGDisplayCopyDisplayMode(disp);<br />iReturn = GetModeParms (Mode, width, height, depth, freq, flag);<br />CGDisplayModeRelease (Mode);<br />return iReturn;<br />}<br />int GetModeParms (CGDisplayModeRef Mode, double *width, double *height, double *depth, double *freq, int *sflag)<br />{<br />*width = CGDisplayModeGetWidth (Mode);<br />*height = CGDisplayModeGetHeight (Mode);<br />*freq = CGDisplayModeGetRefreshRate (Mode);<br />CFStringRef pixelEncoding = CGDisplayModeCopyPixelEncoding (Mode);<br />*depth = 0;<br />if (pixelEncoding = NULL) return -1;<br />if (pixelEncoding = CFSTR(IO32BitDirectPixels))<br />*depth = 32;<br />else if (pixelEncoding = CFSTR(IO16BitDirectPixels))<br />*depth = 16;<br />else*depth = 8;</p><p>*sflag = CGDisplayModeGetIOFlags(Mode);<br />CFRelease(pixelEncoding);<br />return 0;<br />}<br />bool GetBestDisplayMod(CGDirectDisplayID display, double dwidth, double dheight)<br />{<br /> CFArrayRef modeList;<br /> CGDisplayModeRef mode;<br /> CFIndex index, count, sindex, scount=0;<br /> double width, height, depth, freq;<br />double width1, height1, depth1, freq1;<br /> int flag, flag1;<br />struct sLIST mList[100];<br />int ireturn=0;</p><p> modeList = CGDisplayCopyAllDisplayModes (display, NULL);<br /> if (modeList == NULL)return;<br /> count = CFArrayGetCount (modeList);<br /> scount=0;<br />for (index = 0; index < count; index++)<br />{<br />mode = (CGDisplayModeRef)CFArrayGetValueAtIndex (modeList, index);<br />GetModeParms(mode, &width, &height, &depth, &freq, &flag);<br />// printf("........ scount=%d/n", (int)scount);<br />if (!((width==width1) && (height==height1) && (depth==depth1) && (freq==freq1) && (flag==flag1))) {<br />if (CGDisplayModeIsUsableForDesktopGUI(mode)) {<br />mList[scount].mode=mode; mList[scount].width=width; mList[scount].height=height;<br />width1=width; height1=height; depth1=depth; freq1=freq; flag1=flag;<br />scount++;<br />}<br />}<br />}<br />mode=NULL;<br />qsort ((void *) mList, scount, sizeof(struct sLIST), (compfn) modecompare);<br />for (index=0; index<scount; index++)<br />{<br />if (mList[index].width >= dwidth) {<br />if (mList[index].height >= dheight) {<br />mode = mList[index].mode;<br />break;<br />}<br />}<br />}</p><p>CGDisplayConfigRef pConfigRef;<br />CGConfigureOption option=kCGConfigurePermanently;<br />if ((mode != NULL) && (CGBeginDisplayConfiguration(&pConfigRef) == kCGErrorSuccess)) {<br />CGConfigureDisplayWithDisplayMode(pConfigRef, display, mode, NULL);<br />if (CGCompleteDisplayConfiguration (pConfigRef, option) !=kCGErrorSuccess) CGCancelDisplayConfiguration (pConfigRef);<br />}<br />else ireturn = -1;<br /> CFRelease(modeList);<br /> return ireturn;<br />}<br />int modecompare(struct sLIST *elem1, struct sLIST *elem2)<br />{<br /> if ( elem1->width < elem2->width)<br /> return -1;<br /> else if (elem1->width > elem2->width) return 1;<br /> if (elem1->height < elem2->height) return -1;<br /> else if (elem1->height > elem2->height) return 1;<br /> else return 0;<br />}<br />void PrintUsage(const char *argv[])<br />{<br />char *fname = strrchr(argv[0], '/')+1;<br />printf("Screen Resolution v1.0, Mac OS X 10.6 or later, i386/n");<br />printf("Copyright 2010 Tony Liu. All rights reserved. June 1, 2010/n");<br />printf("/nUsage:");<br />printf(" %s -a/n", fname);<br />printf(" %s [-l] [1..9]/n", fname);<br />printf(" %s -s [ 1..9 ] hor_res vert_res/n", fname);<br />printf(" %s -? | -h this help./n/n", fname);<br />printf(" -l list resolution, depth and refresh rate/n");<br />printf(" 1..9 display # (default: main display)/n");<br />printf(" -l 1-9 List all support for the display #/n");<br />printf(" -s Set mode./n");<br />printf(" hor_res horizontal resolution/n");<br />printf("vert_res vertical resolution/n/n");<br />printf("Examples:/n");<br />printf("%s -a get online display number/n", fname);<br />printf("%s get current main diplay resolution/n", fname);<br />printf("%s 3 get current resolution of third display/n", fname);<br />printf("%s -l get resolution, bit depth and refresh rate of all displays/n", fname);<br />printf("%s -l 1 get first display all supported mode/n", fname);<br />printf("%s -l 1 2 get first display the second supported mode/n", fname);<br />printf("%s -s 800 600 set resolution of main display to 800x600/n", fname);<br />printf("%s -s 2 800 600 set resolution of secondary display to 800x600/n", fname);<br />}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.