DisplayInfo.h
1 #pragma once 2 3 enum WinVerDef 4 { 5 WIN_VER_UNKNOWN = -1, 6 WIN_VER_95 = 0, 7 WIN_VER_98, 8 WIN_VER_ME, 9 WIN_VER_NT4, 10 WIN_VER_2K, 11 WIN_VER_XP, 12 WIN_VER_XP64, 13 WIN_VER_2K3, 14 WIN_VER_2K3R2, 15 WIN_VER_VISTA, 16 WIN_VER_2K8, 17 WIN_VER_2K8R2, 18 WIN_VER_WIN7, 19 }; 20 21 22 23 class CDisplayInfo 24 { 25 private: 26 CDisplayInfo(void); 27 ~CDisplayInfo(void); 28 public: 29 enum {DISPLAY_TYPE_SINGLE=8,DISPLAY_TYPE_CLONE=2,DISPLAY_TYPE_EXTEND=4}; 30 static int GetDisplayMode(); 31 static int GetDisplayModeXP(); 32 static int GetDisplayModeWin7(); 33 static BOOL SetScreenSaver(BOOL bCancle=TRUE,UINT newTime=-1); 34 static BOOL SetPowerSaver(BOOL bCancle=TRUE,UINT newTime=-1); 35 static BOOL SetDisplayMode(int iType,BOOL ExChange=FALSE); 36 static BOOL SetDisplayModeXP(int iType,BOOL ExChange=FALSE); 37 static BOOL SetDisplayModeWin7(int iType,BOOL ExChange=FALSE); 38 static int GetDisplayCardType(); 39 static int AnalyCardType(LPCTSTR cardStr); 40 static BOOL GetOSVersion( WinVerDef* winVer ); 41 static BOOL IsOlderSystem( WinVerDef specVer, INT* iResult ); 42 static BOOL GetPrimaryMonitorInfo(DEVMODE *dvm); 43 };
DisplayInfo.cpp
1 #include "StdAfx.h" 2 #include "DisplayInfo.h" 3 4 5 enum WinVerMajor 6 { 7 WIN_MAJOR_NT4_ME_98_95 = 4, 8 WIN_MAJOR_2K3R2_2K3_XP_2K = 5, 9 WIN_MAJOR_WIN7_2K8R2_2K8_VISTA = 6, 10 }; 11 12 // 定义目前存在的操作系统次版本号 13 14 enum WinVerminor 15 { 16 WIN_MINOR_2K8_VISTA_2K_NT4_95 = 0, 17 WIN_MINOR_WIN7_2K8R2_XP = 1, 18 WIN_MINOR_2K3R2_2K3_XP64 = 2, 19 WIN_MINOR_98 = 10, 20 WIN_MINOR_ME = 90, 21 }; 22 //typedef void (WINAPI *GetNativeSystemInfoFunc)(LPSYSTEM_INFO); 23 24 #define QDC_ALL_PATHS 0x00000001 25 #define QDC_ONLY_ACTIVE_PATHS 0x00000002 26 #define QDC_DATABASE_CURRENT 0x00000004 27 28 // 29 // Definitions used by SetDisplayConfig. 30 // 31 32 #define SDC_TOPOLOGY_INTERNAL 0x00000001 33 #define SDC_TOPOLOGY_CLONE 0x00000002 34 #define SDC_TOPOLOGY_EXTEND 0x00000004 35 #define SDC_TOPOLOGY_EXTERNAL 0x00000008 36 #define SDC_TOPOLOGY_SUPPLIED 0x00000010 37 #define SDC_USE_DATABASE_CURRENT (SDC_TOPOLOGY_INTERNAL | SDC_TOPOLOGY_CLONE | SDC_TOPOLOGY_EXTEND | SDC_TOPOLOGY_EXTERNAL) 38 39 #define SDC_USE_SUPPLIED_DISPLAY_CONFIG 0x00000020 40 #define SDC_VALIDATE 0x00000040 41 #define SDC_APPLY 0x00000080 42 #define SDC_NO_OPTIMIZATION 0x00000100 43 #define SDC_SAVE_TO_DATABASE 0x00000200 44 #define SDC_ALLOW_CHANGES 0x00000400 45 #define SDC_PATH_PERSIST_IF_REQUIRED 0x00000800 46 #define SDC_FORCE_MODE_ENUMERATION 0x00001000 47 #define SDC_ALLOW_PATH_ORDER_CHANGES 0x00002000 48 49 typedef enum { 50 DISPLAYCONFIG_OUTPUT_TECHNOLOGY_OTHER = -1, 51 DISPLAYCONFIG_OUTPUT_TECHNOLOGY_HD15 = 0, 52 DISPLAYCONFIG_OUTPUT_TECHNOLOGY_SVIDEO = 1, 53 DISPLAYCONFIG_OUTPUT_TECHNOLOGY_COMPOSITE_VIDEO = 2, 54 DISPLAYCONFIG_OUTPUT_TECHNOLOGY_COMPONENT_VIDEO = 3, 55 DISPLAYCONFIG_OUTPUT_TECHNOLOGY_DVI = 4, 56 DISPLAYCONFIG_OUTPUT_TECHNOLOGY_HDMI = 5, 57 DISPLAYCONFIG_OUTPUT_TECHNOLOGY_LVDS = 6, 58 DISPLAYCONFIG_OUTPUT_TECHNOLOGY_D_JPN = 8, 59 DISPLAYCONFIG_OUTPUT_TECHNOLOGY_SDI = 9, 60 DISPLAYCONFIG_OUTPUT_TECHNOLOGY_DISPLAYPORT_EXTERNAL = 10, 61 DISPLAYCONFIG_OUTPUT_TECHNOLOGY_DISPLAYPORT_EMBEDDED = 11, 62 DISPLAYCONFIG_OUTPUT_TECHNOLOGY_UDI_EXTERNAL = 12, 63 DISPLAYCONFIG_OUTPUT_TECHNOLOGY_UDI_EMBEDDED = 13, 64 DISPLAYCONFIG_OUTPUT_TECHNOLOGY_SDTVDONGLE = 14, 65 DISPLAYCONFIG_OUTPUT_TECHNOLOGY_INTERNAL = 0x80000000, 66 DISPLAYCONFIG_OUTPUT_TECHNOLOGY_FORCE_UINT32 = 0xFFFFFFFF 67 } DISPLAYCONFIG_VIDEO_OUTPUT_TECHNOLOGY; 68 69 typedef enum { 70 DISPLAYCONFIG_ROTATION_IDENTITY = 1, 71 DISPLAYCONFIG_ROTATION_ROTATE90 = 2, 72 DISPLAYCONFIG_ROTATION_ROTATE180 = 3, 73 DISPLAYCONFIG_ROTATION_ROTATE270 = 4, 74 DISPLAYCONFIG_ROTATION_FORCE_UINT32 = 0xFFFFFFFF 75 } DISPLAYCONFIG_ROTATION; 76 77 typedef enum { 78 DISPLAYCONFIG_SCALING_IDENTITY = 1, 79 DISPLAYCONFIG_SCALING_CENTERED = 2, 80 DISPLAYCONFIG_SCALING_STRETCHED = 3, 81 DISPLAYCONFIG_SCALING_ASPECTRATIOCENTEREDMAX = 4, 82 DISPLAYCONFIG_SCALING_CUSTOM = 5, 83 DISPLAYCONFIG_SCALING_PREFERRED = 128, 84 DISPLAYCONFIG_SCALING_FORCE_UINT32 = 0xFFFFFFFF 85 } DISPLAYCONFIG_SCALING; 86 typedef struct DISPLAYCONFIG_RATIONAL { 87 UINT32 Numerator; 88 UINT32 Denominator; 89 } DISPLAYCONFIG_RATIONAL; 90 typedef enum { 91 DISPLAYCONFIG_SCANLINE_ORDERING_UNSPECIFIED = 0, 92 DISPLAYCONFIG_SCANLINE_ORDERING_PROGRESSIVE = 1, 93 DISPLAYCONFIG_SCANLINE_ORDERING_INTERLACED = 2, 94 DISPLAYCONFIG_SCANLINE_ORDERING_INTERLACED_UPPERFIELDFIRST = DISPLAYCONFIG_SCANLINE_ORDERING_INTERLACED, 95 DISPLAYCONFIG_SCANLINE_ORDERING_INTERLACED_LOWERFIELDFIRST = 3, 96 DISPLAYCONFIG_SCANLINE_ORDERING_FORCE_UINT32 = 0xFFFFFFFF 97 } DISPLAYCONFIG_SCANLINE_ORDERING; 98 99 typedef enum { 100 DISPLAYCONFIG_PIXELFORMAT_8BPP = 1, 101 DISPLAYCONFIG_PIXELFORMAT_16BPP = 2, 102 DISPLAYCONFIG_PIXELFORMAT_24BPP = 3, 103 DISPLAYCONFIG_PIXELFORMAT_32BPP = 4, 104 DISPLAYCONFIG_PIXELFORMAT_NONGDI = 5, 105 DISPLAYCONFIG_PIXELFORMAT_FORCE_UINT32 = 0xffffffff 106 } DISPLAYCONFIG_PIXELFORMAT; 107 typedef enum { 108 DISPLAYCONFIG_MODE_INFO_TYPE_SOURCE = 1, 109 DISPLAYCONFIG_MODE_INFO_TYPE_TARGET = 2, 110 DISPLAYCONFIG_MODE_INFO_TYPE_FORCE_UINT32 = 0xFFFFFFFF 111 } DISPLAYCONFIG_MODE_INFO_TYPE; 112 113 typedef enum { 114 DISPLAYCONFIG_TOPOLOGY_INTERNAL = 0x00000001, 115 DISPLAYCONFIG_TOPOLOGY_CLONE = 0x00000002, 116 DISPLAYCONFIG_TOPOLOGY_EXTEND = 0x00000004, 117 DISPLAYCONFIG_TOPOLOGY_EXTERNAL = 0x00000008, 118 DISPLAYCONFIG_TOPOLOGY_FORCE_UINT32 = 0xFFFFFFFF 119 } DISPLAYCONFIG_TOPOLOGY_ID; 120 121 typedef struct DISPLAYCONFIG_PATH_TARGET_INFO { 122 LUID adapterId; 123 UINT32 id; 124 UINT32 modeInfoIdx; 125 DISPLAYCONFIG_VIDEO_OUTPUT_TECHNOLOGY outputTechnology; 126 DISPLAYCONFIG_ROTATION rotation; 127 DISPLAYCONFIG_SCALING scaling; 128 DISPLAYCONFIG_RATIONAL refreshRate; 129 DISPLAYCONFIG_SCANLINE_ORDERING scanLineOrdering; 130 BOOL targetAvailable; 131 UINT32 statusFlags; 132 } DISPLAYCONFIG_PATH_TARGET_INFO; 133 typedef struct DISPLAYCONFIG_PATH_SOURCE_INFO { 134 LUID adapterId; 135 UINT32 id; 136 UINT32 modeInfoIdx; 137 UINT32 statusFlags; 138 } DISPLAYCONFIG_PATH_SOURCE_INFO; 139 typedef struct DISPLAYCONFIG_PATH_INFO { 140 DISPLAYCONFIG_PATH_SOURCE_INFO sourceInfo; 141 DISPLAYCONFIG_PATH_TARGET_INFO targetInfo; 142 UINT32 flags; 143 } DISPLAYCONFIG_PATH_INFO; 144 typedef struct DISPLAYCONFIG_2DREGION { 145 UINT32 cx; 146 UINT32 cy; 147 } DISPLAYCONFIG_2DREGION; 148 typedef struct DISPLAYCONFIG_VIDEO_SIGNAL_INFO { 149 UINT64 pixelRate; 150 DISPLAYCONFIG_RATIONAL hSyncFreq; 151 DISPLAYCONFIG_RATIONAL vSyncFreq; 152 DISPLAYCONFIG_2DREGION activeSize; 153 DISPLAYCONFIG_2DREGION totalSize; 154 UINT32 videoStandard; 155 DISPLAYCONFIG_SCANLINE_ORDERING scanLineOrdering; 156 } DISPLAYCONFIG_VIDEO_SIGNAL_INFO; 157 158 typedef struct DISPLAYCONFIG_TARGET_MODE { 159 DISPLAYCONFIG_VIDEO_SIGNAL_INFO targetVideoSignalInfo; 160 } DISPLAYCONFIG_TARGET_MODE; 161 162 typedef struct DISPLAYCONFIG_SOURCE_MODE { 163 UINT32 width; 164 UINT32 height; 165 DISPLAYCONFIG_PIXELFORMAT pixelFormat; 166 POINTL position; 167 } DISPLAYCONFIG_SOURCE_MODE; 168 169 typedef struct DISPLAYCONFIG_MODE_INFO { 170 DISPLAYCONFIG_MODE_INFO_TYPE infoType; 171 UINT32 id; 172 LUID adapterId; 173 union { 174 DISPLAYCONFIG_TARGET_MODE targetMode; 175 DISPLAYCONFIG_SOURCE_MODE sourceMode; 176 }; 177 } DISPLAYCONFIG_MODE_INFO; 178 typedef LONG (WINAPI *SETDISPLAYCONFIGFUNC)(__in UINT32 numPathArrayElements, 179 __in_opt DISPLAYCONFIG_PATH_INFO *pathArray, 180 __in UINT32 numModeInfoArrayElements, 181 __in_opt DISPLAYCONFIG_MODE_INFO *modeInfoArray, 182 __in UINT32 Flags 183 ); 184 typedef LONG (WINAPI *GETDISPLAYBUFFERSIZESFUNC)( 185 __in UINT32 Flags, 186 __out UINT32 *pNumPathArrayElements, 187 __out UINT32 *pNumModeInfoArrayElements 188 ); 189 190 typedef LONG (WINAPI *QUERYDISPLAYCONFIGFUNC)( 191 __in UINT32 Flags, 192 __inout UINT32 *pNumPathArrayElements, 193 __out DISPLAYCONFIG_PATH_INFO *pPathInfoArray, 194 __inout UINT32 *pNumModeInfoArrayElements, 195 __out DISPLAYCONFIG_MODE_INFO *pModeInfoArray, 196 __out_opt DISPLAYCONFIG_TOPOLOGY_ID *pCurrentTopologyId 197 ); 198 199 200 CDisplayInfo::CDisplayInfo(void) 201 { 202 } 203 204 CDisplayInfo::~CDisplayInfo(void) 205 { 206 } 207 208 //功能: 设置/取消屏保 209 //参数: bCancle: TRUE取消屏保,此时会自动记录原来的屏保时间.FALSE设置屏保,如果newTime为-1,则恢复原来的屏保时间. 210 // newTime:屏保时间,如果为-1,则被忽略. 211 BOOL CDisplayInfo::SetScreenSaver(BOOL bCancle/* =TRUE */,UINT newTime/* =-1 */) 212 { 213 static UINT ScrSaverTimeOut=600; 214 if(bCancle) 215 { 216 //读取原来的屏保时间,以便恢复 217 ::SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT,0,&ScrSaverTimeOut,0); 218 219 //取消屏保. 220 ::SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,FALSE,0,SPIF_UPDATEINIFILE|SPIF_SENDCHANGE); 221 } 222 else 223 { 224 UINT TimeOut=ScrSaverTimeOut; 225 if(newTime != UINT(-1)) 226 { 227 TimeOut=newTime; 228 } 229 //设置原来的屏保时间 230 ::SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,TimeOut,NULL,SPIF_UPDATEINIFILE|SPIF_SENDCHANGE); 231 //激活屏保. 232 ::SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,TRUE,0,SPIF_UPDATEINIFILE|SPIF_SENDCHANGE); 233 } 234 return TRUE; 235 } 236 237 //功能: 设置/取消显示器电源自动关闭. 238 //参数: bCancle: TRUE取消自动关闭显示器电源,FLASE设置自动关闭显示器电源. 239 // newTime: 新的显示器电源自动关闭时间,如果为-1,设置时按原来显示器电源时间设置. 240 BOOL CDisplayInfo::SetPowerSaver(BOOL bCancle/* =TRUE */,UINT newTime/* =-1 */) 241 { 242 static UINT PowerTimeout=900; 243 if(bCancle) 244 { 245 //获得原来电源时间. 246 ::SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT,0,&PowerTimeout,0); 247 //取消自动关闭显示器 248 //这里不能用SPI_SETPOWEROFFACTIVE,用这个标记不能成功. 249 ::SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,FALSE,0,SPIF_UPDATEINIFILE|SPIF_SENDCHANGE); 250 } 251 else 252 { 253 UINT TimeOut=PowerTimeout; 254 if(newTime != UINT(-1)) 255 { 256 TimeOut=newTime; 257 } 258 //设置原来的电源时间. 259 ::SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,TimeOut,NULL,SPIF_UPDATEINIFILE|SPIF_SENDCHANGE); 260 //激活自动关闭显示器. 261 ::SystemParametersInfo(SPI_SETPOWEROFFACTIVE,TRUE,0,SPIF_UPDATEINIFILE|SPIF_SENDCHANGE); 262 } 263 return TRUE; 264 } 265 266 //功能: 获得Win7n以前OS当前多显示器的显示模式. 267 //返回值: DISPLAY_TYPE_SINGLE:单显示器; 268 // DISPLAY_TYPE_CLONE ;复制模式 269 // DISPLAY_TYPE_EXTEND:扩展模式; 270 int CDisplayInfo::GetDisplayModeXP() 271 { 272 if( 1 != GetDisplayCardType())//是否ATI显卡 273 { 274 return 0; 275 } 276 DISPLAY_DEVICE DispDev; 277 TCHAR szSaveDeviceName[0x60]; 278 BOOL bRet = TRUE; 279 280 DEVMODE dvm; 281 dvm.dmSize=sizeof(DEVMODE); 282 dvm.dmDriverExtra=0; 283 dvm.dmFields=DM_POSITION; 284 285 ZeroMemory(&DispDev, sizeof(DISPLAY_DEVICE)); 286 DispDev.cb = sizeof(DISPLAY_DEVICE); 287 288 289 DWORD d1=0,d2=0,d1m0=0,d1m1=0,d2m0=0,d2m1=0; 290 291 CString Str,outStr; 292 int i=0,j=0; 293 BOOL bRet2=TRUE; 294 DWORD erro=0; 295 int jDev=0; 296 while(bRet) 297 { 298 bRet=EnumDisplayDevices(NULL,i,&DispDev,0);//遍历当前设备. 299 if(bRet) 300 { 301 memset(szSaveDeviceName,0,0x60); 302 lstrcpy(szSaveDeviceName, DispDev.DeviceName); 303 bRet2=TRUE; 304 j=0; 305 OutputDebugString(szSaveDeviceName); 306 Str.Format(TEXT("%08X ------------------------------------"),DispDev.StateFlags); 307 OutputDebugString(Str); 308 if(i==0) 309 { 310 d1=DispDev.StateFlags; 311 } 312 else if(i==1) 313 { 314 d2=DispDev.StateFlags; 315 } 316 while(bRet2) 317 { 318 bRet2=EnumDisplayDevices(szSaveDeviceName,j,&DispDev,0);//遍历指定设备下的其它设备参数. 319 if(bRet2) 320 { 321 j++; 322 if(_tcsstr(DispDev.DeviceName,TEXT("Default_Monitor"))!=NULL) 323 { 324 continue; 325 } 326 BOOL t=EnumDisplaySettings(DispDev.DeviceName,ENUM_CURRENT_SETTINGS,&dvm);//遍历指定设备的当前配置. 327 if(t==FALSE) 328 { 329 erro=GetLastError(); 330 } 331 OutputDebugString(DispDev.DeviceName); 332 //OutputDebugString(DispDev.DeviceString); 333 OutputDebugString(DispDev.DeviceID);//LED 334 //OutputDebugString(DispDev.DeviceKey); 335 Str.Format(TEXT("%08X "),DispDev.StateFlags); 336 OutputDebugString(Str); 337 if(i==0) 338 { 339 if(jDev==0) 340 { 341 d1m0=DispDev.StateFlags; 342 } 343 else if(jDev==1) 344 { 345 d1m1=DispDev.StateFlags; 346 } 347 } 348 else if(i==1) 349 { 350 if(jDev==0) 351 { 352 d2m0=DispDev.StateFlags; 353 } 354 else if(jDev==1) 355 { 356 d2m1=DispDev.StateFlags; 357 } 358 } 359 jDev++; 360 } 361 362 } 363 } 364 ++i; 365 } 366 367 if((d1&DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)==DISPLAY_DEVICE_ATTACHED_TO_DESKTOP && 368 (d2&DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)==DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) 369 { 370 OutputDebugString(TEXT("扩展模式")); 371 return DISPLAY_TYPE_EXTEND; 372 } 373 374 if( (d1m0&DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)==DISPLAY_DEVICE_ATTACHED_TO_DESKTOP && 375 (d1m1&DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)==DISPLAY_DEVICE_ATTACHED_TO_DESKTOP ) 376 { 377 OutputDebugString(TEXT("复制模式")); 378 return DISPLAY_TYPE_CLONE; 379 } 380 381 OutputDebugString(TEXT("单显示器")); 382 return DISPLAY_TYPE_SINGLE; 383 } 384 385 //功能: 获得Win7下当前多显示器的显示模式. 386 //返回值: DISPLAY_TYPE_SINGLE:单显示器; 387 // DISPLAY_TYPE_CLONE ;复制模式 388 // DISPLAY_TYPE_EXTEND:扩展模式; 389 int CDisplayInfo::GetDisplayModeWin7() 390 { 391 QUERYDISPLAYCONFIGFUNC QueryDisplayConfig=NULL; 392 HMODULE hMod=LoadLibrary(TEXT("user32.dll")); 393 if(hMod) 394 { 395 QueryDisplayConfig=(QUERYDISPLAYCONFIGFUNC)GetProcAddress(hMod,"QueryDisplayConfig"); 396 if( QueryDisplayConfig) 397 { 398 UINT32 NumPathArrayElements = 0; 399 UINT32 NumModeInfoArrayElements = 0; 400 LONG returnValue=0; 401 DISPLAYCONFIG_TOPOLOGY_ID topID=DISPLAYCONFIG_TOPOLOGY_FORCE_UINT32; 402 returnValue = QueryDisplayConfig( 403 QDC_DATABASE_CURRENT, 404 NULL, NULL, 405 NULL, NULL, 406 &topID); 407 if(returnValue == ERROR_SUCCESS) 408 { 409 int ret=0; 410 switch(topID) 411 { 412 case DISPLAYCONFIG_TOPOLOGY_CLONE: 413 ret=DISPLAY_TYPE_CLONE; 414 break; 415 case DISPLAYCONFIG_TOPOLOGY_EXTEND: 416 ret=DISPLAY_TYPE_EXTEND; 417 break; 418 default: 419 ret=DISPLAY_TYPE_SINGLE; 420 break; 421 } 422 return ret; 423 } 424 } 425 } 426 return 0; 427 } 428 429 430 //功能: 获得当前多显示器的显示模式. 431 //返回值: DISPLAY_TYPE_SINGLE:单显示器; 432 // DISPLAY_TYPE_CLONE ;复制模式 433 // DISPLAY_TYPE_EXTEND:扩展模式; 434 int CDisplayInfo::GetDisplayMode() 435 { 436 WinVerDef OsVer; 437 int ret=0; 438 if(GetOSVersion(&OsVer)) 439 { 440 switch(OsVer) 441 { 442 case WIN_VER_WIN7: 443 ret=GetDisplayModeWin7(); 444 break; 445 default: 446 ret=GetDisplayModeXP(); 447 break; 448 } 449 } 450 return ret; 451 } 452 453 BOOL CDisplayInfo::GetPrimaryMonitorInfo(DEVMODE *dvm) 454 { 455 if(dvm==NULL) 456 { 457 return FALSE; 458 } 459 dvm->dmSize=sizeof(DEVMODE); 460 461 DISPLAY_DEVICE DispDev; 462 BOOL bRet = TRUE; 463 ZeroMemory(&DispDev, sizeof(DISPLAY_DEVICE)); 464 DispDev.cb = sizeof(DISPLAY_DEVICE); 465 int i=0; 466 DWORD erro=0; 467 while(bRet) 468 { 469 bRet=EnumDisplayDevices(NULL,i,&DispDev,0); 470 if(bRet) 471 { 472 if(DispDev.StateFlags&DISPLAY_DEVICE_PRIMARY_DEVICE) 473 { 474 bRet=EnumDisplaySettings(DispDev.DeviceName,ENUM_CURRENT_SETTINGS,dvm); 475 if(bRet) 476 { 477 return TRUE; 478 } 479 } 480 } 481 i++; 482 } 483 return FALSE; 484 } 485 486 //功能:设置多显示器显示模式 487 //参数:iType;显示模式,可选以下三种: 488 // DISPLAY_TYPE_SINGLE; 单显示器 489 // DISPLAY_TYPE_CLONE; 复制模式 490 // DISPLAY_TYEP_EXTEND; 扩展模式 491 492 BOOL CDisplayInfo::SetDisplayMode(int iType,BOOL ExChange/* =FALSE */) 493 { 494 WinVerDef osVer; 495 if(GetOSVersion(&osVer)) 496 { 497 if(osVer==WIN_VER_WIN7) 498 { 499 OutputDebugString(TEXT("Win7")); 500 SetDisplayModeWin7(iType,ExChange); 501 } 502 else 503 { 504 OutputDebugString(TEXT("Other OS")); 505 SetDisplayModeXP(iType,ExChange); 506 } 507 return TRUE; 508 } 509 return FALSE; 510 } 511 512 //临时测试用 513 void OutPath(DISPLAYCONFIG_PATH_INFO *pPath) 514 { 515 CString Str; 516 Str=TEXT("--------------------PATH----------------------- "); 517 OutputDebugString(Str); 518 Str.Format(TEXT("state:%08X "),pPath->flags); 519 OutputDebugString(Str); 520 Str.Format(TEXT("source: adapterID_H:%08X L:%08X ID:%08X ModeIndex:%08X Flag:%08X"), 521 pPath->sourceInfo.adapterId.HighPart,pPath->sourceInfo.adapterId.LowPart, 522 pPath->sourceInfo.id,pPath->sourceInfo.modeInfoIdx,pPath->sourceInfo.statusFlags); 523 OutputDebugString(Str); 524 Str.Format(TEXT("target: adapterId H: %08X L:%08X ID:%08X Index:%08X"), 525 pPath->targetInfo.adapterId.HighPart,pPath->targetInfo.adapterId.LowPart, 526 pPath->targetInfo.id,pPath->targetInfo.modeInfoIdx); 527 OutputDebugString(Str); 528 529 switch(pPath->targetInfo.outputTechnology)//输出类型. 530 { 531 case DISPLAYCONFIG_OUTPUT_TECHNOLOGY_OTHER: 532 OutputDebugStringA("outputTechnology: DISPLAYCONFIG_OUTPUT_TECHNOLOGY_OTHER "); 533 break; 534 case DISPLAYCONFIG_OUTPUT_TECHNOLOGY_HD15: 535 OutputDebugStringA("outputTechnology: DISPLAYCONFIG_OUTPUT_TECHNOLOGY_HD15 "); 536 break; 537 case DISPLAYCONFIG_OUTPUT_TECHNOLOGY_SVIDEO: 538 OutputDebugStringA("outputTechnology: DISPLAYCONFIG_OUTPUT_TECHNOLOGY_SVIDEO "); 539 break; 540 case DISPLAYCONFIG_OUTPUT_TECHNOLOGY_COMPOSITE_VIDEO : 541 OutputDebugStringA("outputTechnology: DISPLAYCONFIG_OUTPUT_TECHNOLOGY_COMPOSITE_VIDEO "); 542 break; 543 case DISPLAYCONFIG_OUTPUT_TECHNOLOGY_COMPONENT_VIDEO : 544 OutputDebugStringA("outputTechnology: DISPLAYCONFIG_OUTPUT_TECHNOLOGY_COMPONENT_VIDEO "); 545 break; 546 case DISPLAYCONFIG_OUTPUT_TECHNOLOGY_DVI: 547 OutputDebugStringA("outputTechnology: DISPLAYCONFIG_OUTPUT_TECHNOLOGY_DVI "); 548 break; 549 case DISPLAYCONFIG_OUTPUT_TECHNOLOGY_HDMI: 550 OutputDebugStringA("outputTechnology: DISPLAYCONFIG_OUTPUT_TECHNOLOGY_HDMI "); 551 break; 552 case DISPLAYCONFIG_OUTPUT_TECHNOLOGY_LVDS: 553 OutputDebugStringA("outputTechnology: DISPLAYCONFIG_OUTPUT_TECHNOLOGY_LVDS "); 554 break; 555 case DISPLAYCONFIG_OUTPUT_TECHNOLOGY_D_JPN: 556 OutputDebugStringA("outputTechnology: outputTechnology: DISPLAYCONFIG_OUTPUT_TECHNOLOGY_D_JPN "); 557 break; 558 case DISPLAYCONFIG_OUTPUT_TECHNOLOGY_SDI: 559 OutputDebugStringA("outputTechnology: DISPLAYCONFIG_OUTPUT_TECHNOLOGY_SDI "); 560 break; 561 case DISPLAYCONFIG_OUTPUT_TECHNOLOGY_DISPLAYPORT_EXTERNAL: 562 OutputDebugStringA("outputTechnology: DISPLAYCONFIG_OUTPUT_TECHNOLOGY_DISPLAYPORT_EXTERNAL "); 563 break; 564 case DISPLAYCONFIG_OUTPUT_TECHNOLOGY_DISPLAYPORT_EMBEDDED: 565 OutputDebugStringA("outputTechnology: DISPLAYCONFIG_OUTPUT_TECHNOLOGY_DISPLAYPORT_EMBEDDED "); 566 break; 567 case DISPLAYCONFIG_OUTPUT_TECHNOLOGY_UDI_EXTERNAL: 568 OutputDebugStringA("outputTechnology: DISPLAYCONFIG_OUTPUT_TECHNOLOGY_UDI_EXTERNAL "); 569 break; 570 case DISPLAYCONFIG_OUTPUT_TECHNOLOGY_UDI_EMBEDDED: 571 OutputDebugStringA("outputTechnology: DISPLAYCONFIG_OUTPUT_TECHNOLOGY_UDI_EMBEDDED "); 572 break; 573 case DISPLAYCONFIG_OUTPUT_TECHNOLOGY_SDTVDONGLE: 574 OutputDebugStringA("outputTechnology: DISPLAYCONFIG_OUTPUT_TECHNOLOGY_SDTVDONGLE "); 575 break; 576 case DISPLAYCONFIG_OUTPUT_TECHNOLOGY_INTERNAL : 577 OutputDebugStringA("outputTechnology: DISPLAYCONFIG_OUTPUT_TECHNOLOGY_INTERNAL "); 578 break; 579 /*case DISPLAYCONFIG_OUTPUT_TECHNOLOGY_FORCE_UINT32: 580 OutputDebugStringA("outputTechnology: DISPLAYCONFIG_OUTPUT_TECHNOLOGY_FORCE_UINT32 "); 581 break;*/ 582 } 583 584 Str.Format(TEXT("refreshRate--Numerator:%-08X Denominator:%08X"), 585 pPath->targetInfo.refreshRate.Numerator,pPath->targetInfo.refreshRate.Denominator); 586 587 switch(pPath->targetInfo.rotation) 588 { 589 case DISPLAYCONFIG_ROTATION_IDENTITY: 590 OutputDebugString(TEXT("rotation: DISPLAYCONFIG_ROTATION_IDENTITY ")); 591 break; 592 case DISPLAYCONFIG_ROTATION_ROTATE90: 593 OutputDebugString(TEXT("rotation: DISPLAYCONFIG_ROTATION_ROTATE90 ")); 594 break; 595 case DISPLAYCONFIG_ROTATION_ROTATE180: 596 OutputDebugString(TEXT("rotation: DISPLAYCONFIG_ROTATION_ROTATE180 ")); 597 break; 598 case DISPLAYCONFIG_ROTATION_ROTATE270: 599 OutputDebugString(TEXT("rotation: DISPLAYCONFIG_ROTATION_ROTATE270 ")); 600 break; 601 case DISPLAYCONFIG_ROTATION_FORCE_UINT32: 602 OutputDebugString(TEXT("rotation: DISPLAYCONFIG_ROTATION_FORCE_UINT32 ")); 603 break; 604 } 605 606 switch(pPath->targetInfo.scaling) 607 { 608 case DISPLAYCONFIG_SCALING_IDENTITY: 609 OutputDebugString(TEXT("scaling: DISPLAYCONFIG_SCALING_IDENTITY ")); 610 break; 611 case DISPLAYCONFIG_SCALING_CENTERED: 612 OutputDebugString(TEXT("scaling: DISPLAYCONFIG_SCALING_CENTERED ")); 613 break; 614 case DISPLAYCONFIG_SCALING_STRETCHED: 615 OutputDebugString(TEXT("scaling: DISPLAYCONFIG_SCALING_STRETCHED ")); 616 break; 617 case DISPLAYCONFIG_SCALING_ASPECTRATIOCENTEREDMAX: 618 OutputDebugString(TEXT("scaling: DISPLAYCONFIG_SCALING_ASPECTRATIOCENTEREDMAX ")); 619 break; 620 case DISPLAYCONFIG_SCALING_CUSTOM: 621 OutputDebugString(TEXT("scaling: DISPLAYCONFIG_SCALING_CUSTOM ")); 622 break; 623 case DISPLAYCONFIG_SCALING_PREFERRED: 624 OutputDebugString(TEXT("scaling: DISPLAYCONFIG_SCALING_PREFERRED ")); 625 break; 626 case DISPLAYCONFIG_SCALING_FORCE_UINT32: 627 OutputDebugString(TEXT("scaling: DISPLAYCONFIG_SCALING_FORCE_UINT32 ")); 628 break; 629 } 630 switch(pPath->targetInfo.scanLineOrdering) 631 { 632 case DISPLAYCONFIG_SCANLINE_ORDERING_UNSPECIFIED: 633 OutputDebugString(TEXT("scanLineOrdering: DISPLAYCONFIG_SCANLINE_ORDERING_UNSPECIFIED ")); 634 break; 635 case DISPLAYCONFIG_SCANLINE_ORDERING_PROGRESSIVE: 636 OutputDebugString(TEXT("scanLineOrdering: ")); 637 break; 638 case DISPLAYCONFIG_SCANLINE_ORDERING_INTERLACED: 639 OutputDebugString(TEXT("scanLineOrdering: DISPLAYCONFIG_SCANLINE_ORDERING_PROGRESSIVE ")); 640 break; 641 /*case DISPLAYCONFIG_SCANLINE_ORDERING_INTERLACED_UPPERFIELDFIRST: 642 OutputDebugString(TEXT("scanLineOrdering: DISPLAYCONFIG_SCANLINE_ORDERING_INTERLACED_UPPERFIELDFIRST ")); 643 break;*/ 644 case DISPLAYCONFIG_SCANLINE_ORDERING_INTERLACED_LOWERFIELDFIRST: 645 OutputDebugString(TEXT("scanLineOrdering: DISPLAYCONFIG_SCANLINE_ORDERING_INTERLACED_LOWERFIELDFIRST ")); 646 break; 647 case DISPLAYCONFIG_SCANLINE_ORDERING_FORCE_UINT32: 648 OutputDebugString(TEXT("scanLineOrdering: DISPLAYCONFIG_SCANLINE_ORDERING_FORCE_UINT32 ")); 649 break; 650 } 651 Str.Format(TEXT("state:%08X valiable:%08X"), 652 pPath->targetInfo.statusFlags,pPath->targetInfo.targetAvailable); 653 OutputDebugString(Str); 654 655 } 656 //临时测试用 657 void OutMode(DISPLAYCONFIG_MODE_INFO *pMode) 658 { 659 CString Str; 660 OutputDebugString(TEXT("-----------------------MODE------------------------ ")); 661 Str.Format(TEXT("adpterID H:%08X L:%98X ID:%08X"), 662 pMode->adapterId.HighPart,pMode->adapterId.LowPart,pMode->id); 663 OutputDebugString(Str); 664 switch(pMode->infoType) 665 { 666 case DISPLAYCONFIG_MODE_INFO_TYPE_SOURCE: 667 OutputDebugString(TEXT("infotype DISPLAYCONFIG_MODE_INFO_TYPE_SOURCE ")); 668 break; 669 case DISPLAYCONFIG_MODE_INFO_TYPE_TARGET: 670 OutputDebugString(TEXT("infotype DISPLAYCONFIG_MODE_INFO_TYPE_TARGET ")); 671 break; 672 case DISPLAYCONFIG_MODE_INFO_TYPE_FORCE_UINT32: 673 OutputDebugString(TEXT("infotype DISPLAYCONFIG_MODE_INFO_TYPE_FORCE_UINT32 ")); 674 break; 675 } 676 switch(pMode->sourceMode.pixelFormat) 677 { 678 case DISPLAYCONFIG_PIXELFORMAT_16BPP: 679 OutputDebugString(TEXT("source pixelFormat: DISPLAYCONFIG_PIXELFORMAT_16BPP ")); 680 break; 681 case DISPLAYCONFIG_PIXELFORMAT_8BPP: 682 OutputDebugString(TEXT("source pixelFormat: DISPLAYCONFIG_PIXELFORMAT_8BPP ")); 683 break; 684 case DISPLAYCONFIG_PIXELFORMAT_32BPP: 685 OutputDebugString(TEXT("source pixelFormat: DISPLAYCONFIG_PIXELFORMAT_32BPP ")); 686 break; 687 case DISPLAYCONFIG_PIXELFORMAT_24BPP: 688 OutputDebugString(TEXT("source pixelFormat: DISPLAYCONFIG_PIXELFORMAT_24BPP ")); 689 break; 690 case DISPLAYCONFIG_PIXELFORMAT_NONGDI: 691 OutputDebugString(TEXT("source pixelFormat: DISPLAYCONFIG_PIXELFORMAT_NONGDI ")); 692 break; 693 case DISPLAYCONFIG_PIXELFORMAT_FORCE_UINT32: 694 OutputDebugString(TEXT("source pixelFormat: DISPLAYCONFIG_PIXELFORMAT_FORCE_UINT32 ")); 695 break; 696 } 697 Str.Format(TEXT("source pos(%d,%d) Width:%d Height:%d "), 698 pMode->sourceMode.position.x,pMode->sourceMode.position.y, 699 pMode->sourceMode.width,pMode->sourceMode.height); 700 OutputDebugString(Str); 701 702 Str.Format(TEXT("target activeSize(CX=%d,cy=%d) "), 703 pMode->targetMode.targetVideoSignalInfo.activeSize.cx,pMode->targetMode.targetVideoSignalInfo.activeSize.cy); 704 OutputDebugString(Str); 705 Str.Format(TEXT("target totalsize(cx=%d,cy=%d)"), 706 pMode->targetMode.targetVideoSignalInfo.totalSize.cx, 707 pMode->targetMode.targetVideoSignalInfo.totalSize.cy); 708 OutputDebugString(Str); 709 Str.Format(TEXT("target pixelRate:%016X videoStander:%08X "), 710 pMode->targetMode.targetVideoSignalInfo.pixelRate, 711 pMode->targetMode.targetVideoSignalInfo.videoStandard); 712 OutputDebugString(Str); 713 714 Str.Format(TEXT("target Hfreq Numerator:%08X Denominator:%08X "), 715 pMode->targetMode.targetVideoSignalInfo.hSyncFreq.Numerator, 716 pMode->targetMode.targetVideoSignalInfo.hSyncFreq.Denominator); 717 OutputDebugString(Str); 718 Str.Format(TEXT("target Vfreq Numerator:%08X Denominator:%08X "), 719 pMode->targetMode.targetVideoSignalInfo.vSyncFreq.Numerator, 720 pMode->targetMode.targetVideoSignalInfo.vSyncFreq.Denominator); 721 OutputDebugString(Str); 722 switch(pMode->targetMode.targetVideoSignalInfo.scanLineOrdering) 723 { 724 case DISPLAYCONFIG_SCANLINE_ORDERING_UNSPECIFIED: 725 OutputDebugString(TEXT("scanLineOrdering: DISPLAYCONFIG_SCANLINE_ORDERING_UNSPECIFIED ")); 726 break; 727 case DISPLAYCONFIG_SCANLINE_ORDERING_PROGRESSIVE: 728 OutputDebugString(TEXT("scanLineOrdering: ")); 729 break; 730 case DISPLAYCONFIG_SCANLINE_ORDERING_INTERLACED: 731 OutputDebugString(TEXT("scanLineOrdering: DISPLAYCONFIG_SCANLINE_ORDERING_PROGRESSIVE ")); 732 break; 733 /*case DISPLAYCONFIG_SCANLINE_ORDERING_INTERLACED_UPPERFIELDFIRST: 734 OutputDebugString(TEXT("scanLineOrdering: DISPLAYCONFIG_SCANLINE_ORDERING_INTERLACED_UPPERFIELDFIRST ")); 735 break;*/ 736 case DISPLAYCONFIG_SCANLINE_ORDERING_INTERLACED_LOWERFIELDFIRST: 737 OutputDebugString(TEXT("scanLineOrdering: DISPLAYCONFIG_SCANLINE_ORDERING_INTERLACED_LOWERFIELDFIRST ")); 738 break; 739 case DISPLAYCONFIG_SCANLINE_ORDERING_FORCE_UINT32: 740 OutputDebugString(TEXT("scanLineOrdering: DISPLAYCONFIG_SCANLINE_ORDERING_FORCE_UINT32 ")); 741 break; 742 } 743 744 745 } 746 747 BOOL CDisplayInfo::SetDisplayModeWin7(int iType,BOOL ExChange/* =FALSE */) 748 { 749 SETDISPLAYCONFIGFUNC SetDisplayConfig=NULL; 750 GETDISPLAYBUFFERSIZESFUNC GetDisplayConfigBufferSizes=NULL; 751 QUERYDISPLAYCONFIGFUNC QueryDisplayConfig=NULL; 752 HMODULE hMod=LoadLibrary(TEXT("user32.dll")); 753 if(hMod) 754 { 755 OutputDebugString(TEXT("Load OK")); 756 SetDisplayConfig=(SETDISPLAYCONFIGFUNC)GetProcAddress(hMod,"SetDisplayConfig"); 757 GetDisplayConfigBufferSizes=(GETDISPLAYBUFFERSIZESFUNC)(GetProcAddress(hMod,"GetDisplayConfigBufferSizes")); 758 QueryDisplayConfig=(QUERYDISPLAYCONFIGFUNC)GetProcAddress(hMod,"QueryDisplayConfig"); 759 if(SetDisplayConfig && GetDisplayConfigBufferSizes && QueryDisplayConfig) 760 { 761 UINT32 NumPathArrayElements = 0; 762 UINT32 NumModeInfoArrayElements = 0; 763 LONG returnValue; 764 returnValue = GetDisplayConfigBufferSizes( 765 QDC_ALL_PATHS, 766 &NumPathArrayElements, 767 &NumModeInfoArrayElements); // Returns Success 768 if (returnValue != ERROR_SUCCESS) 769 { 770 OutputDebugString(TEXT("GetDisplayConfigBufferSizes error")); 771 } 772 DISPLAYCONFIG_PATH_INFO *pPathInfoArray = 773 new DISPLAYCONFIG_PATH_INFO[NumPathArrayElements]; 774 775 DISPLAYCONFIG_MODE_INFO *pModeInfoArray = 776 new DISPLAYCONFIG_MODE_INFO[NumModeInfoArrayElements]; 777 778 DISPLAYCONFIG_TOPOLOGY_ID topID=DISPLAYCONFIG_TOPOLOGY_FORCE_UINT32; 779 returnValue = QueryDisplayConfig( 780 QDC_ALL_PATHS, 781 &NumPathArrayElements, pPathInfoArray, 782 &NumModeInfoArrayElements, pModeInfoArray, 783 NULL); 784 if(returnValue != ERROR_SUCCESS) 785 { 786 OutputDebugString(TEXT(" QueryDisplayConfig erro")); 787 } 788 CString Str; 789 for(int iPath=0;iPath<NumPathArrayElements;++iPath) 790 { 791 OutPath(&pPathInfoArray[iPath]); 792 } 793 for(int iMode=0;iMode<NumModeInfoArrayElements;++iMode) 794 { 795 OutMode(&pModeInfoArray[iMode]); 796 } 797 /*switch(topID) 798 { 799 case DISPLAYCONFIG_TOPOLOGY_CLONE: 800 OutputDebugString(TEXT("DISPLAYCONFIG_TOPOLOGY_CLONE")); 801 break; 802 case DISPLAYCONFIG_TOPOLOGY_INTERNAL: 803 OutputDebugString(TEXT("DISPLAYCONFIG_TOPOLOGY_INTERNAL")); 804 break; 805 case DISPLAYCONFIG_TOPOLOGY_EXTEND: 806 OutputDebugString(TEXT("DISPLAYCONFIG_TOPOLOGY_EXTEND")); 807 break; 808 case DISPLAYCONFIG_TOPOLOGY_EXTERNAL: 809 OutputDebugString(TEXT("DISPLAYCONFIG_TOPOLOGY_EXTERNAL")); 810 break; 811 case DISPLAYCONFIG_TOPOLOGY_FORCE_UINT32: 812 OutputDebugString(TEXT("DISPLAYCONFIG_TOPOLOGY_FORCE_UINT32")); 813 break; 814 default: 815 break; 816 }*/ 817 818 OutputDebugString(TEXT("Get OK")); 819 //returnValue=SetDisplayConfig(NumPathArrayElements,pPathInfoArray,NumModeInfoArrayElements,pModeInfoArray,SDC_TOPOLOGY_CLONE|SDC_APPLY); 820 returnValue=SetDisplayConfig(0,NULL,0,NULL,iType|SDC_APPLY);//设置DC_TOPOLOGY_XXX前面4个参数must be 0; 821 switch(returnValue) 822 { 823 case ERROR_SUCCESS: 824 OutputDebugString(TEXT("ERRO_SUCESS")); 825 break; 826 case ERROR_INVALID_PARAMETER: 827 OutputDebugString(TEXT("ERROR_INVALID_PARAMETER")); 828 break; 829 case ERROR_NOT_SUPPORTED: 830 OutputDebugString(TEXT("ERROR_NOT_SUPPORTED")); 831 break; 832 case ERROR_ACCESS_DENIED: 833 OutputDebugString(TEXT("ERROR_ACCESS_DENIED")); 834 break; 835 case ERROR_GEN_FAILURE: 836 OutputDebugString(TEXT("ERROR_GEN_FAILURE")); 837 break; 838 case ERROR_INSUFFICIENT_BUFFER: 839 OutputDebugString(TEXT("ERROR_INSUFFICIENT_BUFFER")); 840 break; 841 default: 842 OutputDebugString(TEXT("unkonw")); 843 } 844 FreeLibrary(hMod); 845 return TRUE; 846 } 847 FreeLibrary(hMod); 848 } 849 return FALSE; 850 } 851 852 BOOL CDisplayInfo::SetDisplayModeXP(int iType,BOOL ExChange/*=0*/) 853 { 854 DEVMODE dvm1; 855 memset(&dvm1, 0, sizeof(dvm1)); 856 dvm1.dmSize = sizeof(dvm1); 857 DEVMODE dvm2; 858 memset(&dvm2, 0, sizeof(DEVMODE)); 859 dvm2.dmSize = sizeof(DEVMODE); 860 861 TCHAR PriDevName[0x100]={0}; 862 TCHAR AddDevName[0x100]={0}; 863 864 DISPLAY_DEVICE DispDev; 865 BOOL bRet = TRUE; 866 ZeroMemory(&DispDev, sizeof(DISPLAY_DEVICE)); 867 DispDev.cb = sizeof(DISPLAY_DEVICE); 868 int i=0; 869 int nOffset=0; 870 BOOL bAnaly=FALSE; 871 while(bRet) 872 { 873 bRet=EnumDisplayDevices(NULL,i,&DispDev,0); 874 if(bAnaly==FALSE) 875 { 876 bAnaly=TRUE; 877 if(1==AnalyCardType(DispDev.DeviceName))//只能是API显示卡 878 { 879 return FALSE; 880 } 881 bAnaly=TRUE; 882 } 883 if(bRet) 884 { 885 if(DispDev.StateFlags&DISPLAY_DEVICE_PRIMARY_DEVICE) 886 { 887 _tcscpy_s(PriDevName,0x100,DispDev.DeviceName); 888 bRet=EnumDisplaySettingsEx(DispDev.DeviceName,ENUM_CURRENT_SETTINGS,&dvm1,EDS_RAWMODE); 889 } 890 else if((DispDev.StateFlags&0xFFFF)<8) 891 { 892 _tcscpy_s(AddDevName,0x100,DispDev.DeviceName); 893 } 894 } 895 i++; 896 if(i==2) 897 { 898 break; 899 } 900 } 901 if(i==1)//只有一个显示器,不能进行模式设置. 902 { 903 return FALSE; 904 } 905 906 dvm1.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_POSITION ; 907 dvm2.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_POSITION ; 908 //dvm2=dvm1; 909 dvm2.dmPelsHeight=dvm1.dmPelsHeight; 910 dvm2.dmPelsWidth=dvm1.dmPelsWidth; 911 switch(iType) 912 { 913 case DISPLAY_TYPE_SINGLE: 914 dvm2.dmPelsHeight=0;//设置成单显示器. 915 dvm2.dmPelsWidth=0; 916 break; 917 case DISPLAY_TYPE_CLONE://当两个显示器同时有效时,偏移不重叠.克隆模式就不能直接设置. 918 //dvm2.dmPelsHeight=0; 919 //dvm2.dmPelsWidth=0; 920 break; 921 case DISPLAY_TYPE_EXTEND: 922 nOffset=dvm1.dmPelsWidth;//强制横向向右扩展, 923 break; 924 default: 925 break; 926 } 927 dvm1.dmPosition.x=0; 928 dvm1.dmPosition.y=0; 929 dvm2.dmPosition.x=nOffset; 930 dvm2.dmPosition.y=0; 931 932 CString Str; 933 Str.Format(TEXT("Pri: %s (%d,%d,%d,%d) "),PriDevName,dvm1.dmPosition.x,dvm1.dmPosition.y, 934 dvm1.dmPelsWidth,dvm1.dmPelsHeight); 935 OutputDebugString(Str); 936 Str.Format(TEXT("Add: %s (%d,%d,%d,%d) "),AddDevName,dvm2.dmPosition.x,dvm2.dmPosition.y, 937 dvm2.dmPelsWidth,dvm2.dmPelsHeight); 938 OutputDebugString(Str); 939 940 if(ExChange && (iType==DISPLAY_TYPE_EXTEND || iType==DISPLAY_TYPE_SINGLE))//如果需要交换显示,则改变主显示器 941 { 942 ChangeDisplaySettingsEx(PriDevName, &dvm2, NULL, CDS_NORESET | CDS_UPDATEREGISTRY , 0); 943 ChangeDisplaySettingsEx(AddDevName, &dvm1, NULL, CDS_SET_PRIMARY | CDS_NORESET | CDS_UPDATEREGISTRY, 0); 944 } 945 else 946 { 947 ChangeDisplaySettingsEx(PriDevName, &dvm1, NULL, CDS_SET_PRIMARY | CDS_NORESET | CDS_UPDATEREGISTRY, 0); 948 ChangeDisplaySettingsEx(AddDevName, &dvm2, NULL, CDS_NORESET | CDS_UPDATEREGISTRY , 0); 949 } 950 951 ChangeDisplaySettingsEx(NULL, NULL, NULL, 0, 0);//使有效 952 953 return TRUE; 954 } 955 956 int CDisplayInfo::AnalyCardType(LPCTSTR cardStr) 957 { 958 if(cardStr==NULL) 959 { 960 return 0; 961 } 962 OutputDebugString(cardStr); 963 TCHAR buf[0x100]={0}; 964 _tcscpy_s(buf,0x100,cardStr); 965 _tcsupr_s(buf,100); 966 if(_tcsstr(buf,TEXT("ATI"))) 967 { 968 return 1; 969 } 970 else if(_tcsstr(buf,TEXT("NVIDIA"))) 971 { 972 return 2; 973 } 974 else 975 { 976 return 3; 977 } 978 } 979 980 //功能:获得显卡类型,ATI=1,NVDIA.=2... 981 int CDisplayInfo::GetDisplayCardType() 982 { 983 DISPLAY_DEVICE DispDev; 984 TCHAR szSaveDeviceName[0x60]; 985 BOOL bRet = TRUE; 986 987 988 989 ZeroMemory(&DispDev, sizeof(DISPLAY_DEVICE)); 990 DispDev.cb = sizeof(DISPLAY_DEVICE); 991 992 CString Str,outStr; 993 int i=0; 994 BOOL bRet2=TRUE; 995 DWORD erro=0; 996 while(bRet) 997 { 998 bRet=EnumDisplayDevices(NULL,i,&DispDev,0); 999 if(bRet) 1000 { 1001 memset(szSaveDeviceName,0,0x60); 1002 lstrcpy(szSaveDeviceName, DispDev.DeviceName); 1003 bRet2=TRUE; 1004 OutputDebugString(szSaveDeviceName); 1005 OutputDebugString(DispDev.DeviceString); 1006 return AnalyCardType(DispDev.DeviceString); 1007 } 1008 i++; 1009 } 1010 return 0; 1011 } 1012 1013 //功能:返回系统版本号 1014 //参数:winVer返回版本号. 1015 BOOL CDisplayInfo::GetOSVersion( WinVerDef* winVer ) 1016 { 1017 OSVERSIONINFOEX osversionInfo; 1018 ZeroMemory( &osversionInfo, sizeof(osversionInfo) ); 1019 osversionInfo.dwOSVersionInfoSize = sizeof(osversionInfo); 1020 *winVer = WIN_VER_UNKNOWN; 1021 1022 /*BOOL bOsVersionInfoEx=FALSE;//是否是早期版本,FALSE为早期版本,TRUE为校新版本. 1023 if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osversionInfo)) ) 1024 { 1025 osversionInfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);//更早期系统需要从注册表中读取一些版本信息. 1026 if (! GetVersionEx ( (OSVERSIONINFO *) &osversionInfo) ) 1027 return FALSE; 1028 }*/ 1029 1030 if ( NULL == winVer || FALSE == GetVersionEx((LPOSVERSIONINFO)&osversionInfo) ) 1031 { 1032 return FALSE; 1033 } 1034 switch( osversionInfo.dwMajorVersion ) 1035 { 1036 case WIN_MAJOR_NT4_ME_98_95: 1037 switch( osversionInfo.dwMinorVersion ) 1038 { 1039 case WIN_MINOR_2K8_VISTA_2K_NT4_95: 1040 if ( VER_PLATFORM_WIN32_NT == osversionInfo.dwPlatformId ) 1041 { 1042 OutputDebugString(TEXT("WIN_VER_NT4")); 1043 *winVer = WIN_VER_NT4; 1044 } 1045 else 1046 { 1047 OutputDebugString(TEXT("WIN_VER_95")); 1048 *winVer = WIN_VER_95; 1049 } 1050 break; 1051 case WIN_MINOR_98: 1052 OutputDebugString(TEXT("WIN_VER_98")); 1053 *winVer = WIN_VER_98; 1054 break; 1055 case WIN_MINOR_ME: 1056 *winVer = WIN_VER_ME; 1057 OutputDebugString(TEXT("WIN_VER_ME")); 1058 break; 1059 default: 1060 break; 1061 } 1062 break; 1063 case WIN_MAJOR_2K3R2_2K3_XP_2K: 1064 switch( osversionInfo.dwMinorVersion ) 1065 { 1066 case WIN_MINOR_2K8_VISTA_2K_NT4_95: 1067 *winVer = WIN_VER_2K; 1068 OutputDebugString(TEXT("WIN_VER_2K")); 1069 break; 1070 case WIN_MINOR_WIN7_2K8R2_XP: 1071 *winVer = WIN_VER_XP; 1072 OutputDebugString(TEXT("WIN_VER_XP")); 1073 break; 1074 case WIN_MINOR_2K3R2_2K3_XP64: 1075 if ( VER_NT_WORKSTATION == osversionInfo.wProductType ) 1076 { 1077 *winVer = WIN_VER_XP64; 1078 OutputDebugString(TEXT("WIN_VER_XP64")); 1079 break; 1080 } 1081 if ( 0 != GetSystemMetrics( SM_SERVERR2 ) ) 1082 { 1083 *winVer = WIN_VER_2K3R2; 1084 OutputDebugString(TEXT("WIN_VER_2K3_R2")); 1085 } 1086 else 1087 { 1088 *winVer = WIN_VER_2K3; 1089 OutputDebugString(TEXT("WIN_VER_2K3")); 1090 } 1091 break; 1092 default: 1093 break; 1094 } 1095 break; 1096 case WIN_MAJOR_WIN7_2K8R2_2K8_VISTA: 1097 switch( osversionInfo.dwMinorVersion ) 1098 { 1099 case WIN_MINOR_2K8_VISTA_2K_NT4_95: 1100 if ( VER_NT_WORKSTATION == osversionInfo.wProductType ) 1101 { 1102 OutputDebugString(TEXT("WIN_VER_VISTA")); 1103 *winVer = WIN_VER_VISTA; 1104 } 1105 else 1106 { 1107 *winVer = WIN_VER_2K8; 1108 OutputDebugString(TEXT("WIN_VER_2K8")); 1109 } 1110 break; 1111 case WIN_MINOR_WIN7_2K8R2_XP: 1112 if ( VER_NT_WORKSTATION == osversionInfo.wProductType ) 1113 { 1114 *winVer = WIN_VER_WIN7; 1115 OutputDebugString(TEXT("WIN_VER_WIN7")); 1116 } 1117 else 1118 { 1119 *winVer = WIN_VER_2K8R2; 1120 OutputDebugString(TEXT("WIN_VER_2K8_R2")); 1121 } 1122 break; 1123 default: 1124 break; 1125 } 1126 break; 1127 default: 1128 break; 1129 } 1130 return TRUE; 1131 } 1132 1133 //功能:判断当前操作系统是否比sepcVer指定的系统更新 1134 BOOL CDisplayInfo::IsOlderSystem( WinVerDef specVer, INT* iResult ) 1135 { 1136 if ( NULL == iResult ) 1137 { 1138 return FALSE; 1139 } 1140 WinVerDef currVer = WIN_VER_UNKNOWN; 1141 if ( FALSE == GetOSVersion( &currVer ) || WIN_VER_UNKNOWN == currVer ) 1142 { 1143 return FALSE; 1144 } 1145 else 1146 { 1147 *iResult = currVer - specVer; 1148 } 1149 return TRUE; 1150 }
原作者的下载包网址:http://download.csdn.net/detail/phmatthaus/9337081