注:把變更現實分辨率的代碼完成了。
變更的時候,因為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);}
- 前言
- Mac OS X:如何將個人文件夾從默認卷移到其他卷(適合OSX10.5 Leopard)
- Mac OS X:Finder菜單中的Connect to Server選項禁止和打開
- Mac OS X: 蘋果機支持的分區表類型及問題解決
- Mac OS X: 偏好Preferences基礎(1)
- Mac OS X: 如何變更Office for Mac的注冊碼
- Mac OS X: 禁止/防止網絡用戶復制app程序到桌面
- Mac OS X: 用戶帳戶的選擇
- Mac OS X: 實用腳本程序(bash scripts)系列
- Mac OS X: 共享iPhoto圖片
- Mac OS X: 實用腳本程序(bash scripts)系列-2
- Mac OS X: 實用腳本程序(bash scripts)系列-3
- Mac OS X: 實用腳本程序(bash scripts)系列-4
- Mac OS X: 實用腳本程序(bash scripts)系列-5
- Mac OS X: 實用腳本程序(bash scripts)系列-6
- Mac OS X:升級rsync和同步應用實例
- Mac OS X: XAMP在Mac上的實現
- Mac OS X瘦身多法
- Mac OS X 10.5快速系統部署實踐-計劃(2) v1
- Mac OS X 10.5快速系統部署實踐-制作核心系統鏡像(1) v1
- Mac OS X數據備份方案一例
- Mac OS X數據備份方案一例(2)
- Mac OS X 10.5快速系統部署實踐-制作核心系統鏡像(2) v1
- Mac OS X: 打印系統治療
- Mac OS X:禁止崩潰報告
- Mac OS X:Snow Leopard的幾個改動(管理員相關)
- Mac OS X排錯:雪豹的最新更新Airport可能不支持無線802.11n網絡
- Mac OS X:雪豹內置支持讀寫NTFS卷
- Mac OS X:定制生成新用戶的程序包
- Mac OS X:在Windows里面使用命令行設置啟動系統
- Mac OS X: 實用腳本程序(bash scripts)系列-7
- Mac OS X:一種從APP中獲得PKG的方法
- Mac OS X: bash腳本實現添加無線SSID(Update2009-10-09)
- Mac OS X: 實用腳本程序(bash scripts)系列-8
- Mac OS X 10.5快速系統部署實踐-制作核心系統鏡像(4) v1
- Mac OS X: 徹底刪除GeekTool(bash腳本)
- Mac OS X: 實用腳本程序(bash scripts)系列-9
- 把你的Leopard變成Mac OS X的系統發布服務器(1)
- 把你的Leopard變成Mac OS X的系統發布服務器(2)
- 把你的Leopard變成Mac OS X的系統發布服務器(3)
- Mac OS X: 訪問控制遠程CUPS服務
- Mac &amp; Win混合平臺訪問和工具
- Mac OS X: DeployStudio更新v1.0rc16
- Mac: 想當Apple菜鳥不容易(1)
- Mac: 想當 Apple菜鳥不容易(2)
- Mac: 想當 Apple菜鳥不容易(3)
- Mac: 想當 Apple菜鳥不容易(4完)
- Mac電腦上處理攝像機上的存儲內容
- Mac OS X: Google自動更新程序的卸載
- 雪豹系統和SMB不兼容的問題的解決匯總
- Mac OS X: 實用腳本程序(bash scripts)系列-10
- Mac OS X:MagicPrefs介紹
- Mac OS X: 系統部署后程序安裝的用戶提示
- Mac OS X:開啟單程序模式
- Mac OS X: 在腳本里關閉Airport
- Mac筆記本電池:十點建議和其它
- Mac OS X: Launchd執行程序的一個例子
- Mac OS X:修改SMART Utility期限限制
- Mac OS X:網絡用戶環境個性配置實例
- Mac OS X:強制退出CD/DVD盤
- Mac OS X: launchd plist在線編輯器
- Mac OS X: 實用腳本程序(bash scripts)系列-11
- Mac OS X: 實用腳本程序(bash scripts)系列-12
- Mac: 卸載Safari 5.0.2
- Mac OS X: 實用腳本程序(bash scripts)系列-13
- Mac OS X: 文件圖標制作和變更(Resource Fork)
- Mac OS X 10.6.6更新之后NTFS只讀的解決
- Mac: 網絡用戶遇到Adobe Reader 9.x/10.x異常退出的解決
- Mac OS X: airport命令的參數
- Mac OS X的Spotlight綜述
- Mac OS X:AFP和SMB共享配置詳細配置
- Mac OS X:詳細解讀Munki和應用
- Mac OS X:顯示/設置分辨率的命令(源程序)
- Mac OS X: 是否升級到Lion?
- OSX: 禁止Flash Player 10.3自動更新
- OSX腳本:禁止系統自動添加AppStore圖標到用戶Dock上
- 獅子GM版本的安裝
- OS X Lion獅子安裝盤的初步研究
- OS X Lion獅子的恢復盤的刪除二法
- OSX: Finder的側邊欄(Sidebar)不顯示已裝載的網絡共享
- OSX:隱藏文件或文件夾
- OS X: 實用腳本程序(bash scripts)系列-13
- OS X: 實用腳本程序(bash scripts)系列-14
- OSX:Lion支持登錄窗口的Policy Banner
- OSX: 讓獅子說中文