首页  编辑  

启用/禁用第二显示器/多个显示器

Tags: /超级猛料/Hardware.硬件相关/其它硬件/   Date Created:

启用/禁用第二显示器/多个显示器

下面的代码演示了如何禁用显示器

void DetachDisplay()
{
   BOOL            FoundSecondaryDisp = FALSE;
   DWORD           DispNum = 0;
   DISPLAY_DEVICE  DisplayDevice;
   LONG            Result;
   TCHAR           szTemp[200];
   int             i = 0;
   DEVMODE   defaultMode;
   // initialize DisplayDevice
   ZeroMemory(&DisplayDevice, sizeof(DisplayDevice));
   DisplayDevice.cb = sizeof(DisplayDevice);
   // get all display devices
   while (EnumDisplayDevices(NULL, DispNum, &DisplayDevice, 0))
       {
       ZeroMemory(&defaultMode, sizeof(DEVMODE));
       defaultMode.dmSize = sizeof(DEVMODE);
       if ( !EnumDisplaySettings((LPSTR)DisplayDevice.DeviceName, ENUM_REGISTRY_SETTINGS, &defaultMode) )
                 OutputDebugString("Store default failed\n");
       if ((DisplayDevice.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) &&
           !(DisplayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE))
           {
           DEVMODE    DevMode;
           ZeroMemory(&DevMode, sizeof(DevMode));
           DevMode.dmSize = sizeof(DevMode);
           DevMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_POSITION
                       | DM_DISPLAYFREQUENCY | DM_DISPLAYFLAGS ;
           Result = ChangeDisplaySettingsEx((LPSTR)DisplayDevice.DeviceName, &DevMode, NULL, CDS_UPDATEREGISTRY, NULL);
           Result = ChangeDisplaySettingsEx((LPSTR)DisplayDevice.DeviceName, &DevMode, NULL, CDS_UPDATEREGISTRY, NULL);
           //The code below shows how to re-attach the secondary displays to the desktop
           //ChangeDisplaySettingsEx((LPSTR)DisplayDevice.DeviceName, &defaultMode, NULL, CDS_UPDATEREGISTRY, NULL);
           //ChangeDisplaySettingsEx((LPSTR)DisplayDevice.DeviceName, &defaultMode, NULL, CDS_UPDATEREGISTRY, NULL);
           
           }
       // Reinit DisplayDevice just to be extra clean
       ZeroMemory(&DisplayDevice, sizeof(DisplayDevice));
       DisplayDevice.cb = sizeof(DisplayDevice);
       DispNum++;
       } // end while for all display devices
}