Microsoft Knowledge Base Email Alertz

When you call the

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 306399 - Last Review: April 4, 2006 - Revision: 1.5

PRB: ChangeDisplaySettingsEx Does Not Detach a Monitor from the Desktop

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
}
				

APPLIES TO
  • Microsoft Platform Software Development Kit-January 2000 Edition, when used with:
    • Microsoft Windows 95
    • Microsoft Windows 98 Standard Edition
    • Microsoft Windows 98 Second Edition
    • Microsoft Windows Millennium Edition
Keywords: 
kbmultimon kbgdi kbprb kbdswgdi2003swept KB306399
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate