PRB: ChangeDisplaySettingsEx Does Not Detach a Monitor from the Desktop (306399)



The information in this article applies to:

  • Microsoft Platform Software Development Kit (SDK) 1.0, when used with:
    • Microsoft Windows 95
    • Microsoft Windows 98
    • Microsoft Windows 98 Second Edition
    • Microsoft Windows Millennium Edition

This article was previously published under Q306399

SYMPTOMS

When you call the ChangeDisplaySettingsEx function on the Microsoft Windows 95, Windows 98, Windows 98 Second Edition, or Windows Millennium Edition (Me) operating system to detach a secondary monitor from the desktop, the monitor will not immediately detach from the desktop. The ChangeDisplaySettingsEx call updates the registry settings so that the update is seen the next time the operating system is started. However, this problem does not occur on Windows NT and Windows 2000.

RESOLUTION

DEVMODE must be set up properly and the property fields in the DEVMODE structure must be set to 0 before you call ChangeDisplaySettingsEx. In addition, in order for the display to be detached immediately without the need to restart the operating system, you must call ChangeDisplaySettingsEx twice.

MORE INFORMATION

In the DEVMODE structure, you need to set the DEVMODE entries of the following flags to zero in the dmFields before calling ChangeDisplaySettingsEx:

DM_PELSWIDTH
DM_PELSHEIGHT
DM_BITSPERPEL
DM_POSITION
DM_DISPLAYFREQUENCY
DM_DISPLAYFLAGS

NOTE: You should save the current DEVMODE by calling EnumDisplaySettings so that you can attach the monitor back to the desktop by using another call to ChangeDisplaySettingsEx.

The following code demonstrates how to detach all secondary display devices from the desktop:
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
}
				

Modification Type:MinorLast Reviewed:4/4/2006
Keywords:kbDSWGDI2003Swept kbGDI kbMultiMon kbprb KB306399