Turn Off the Display While Running Applications

 

Applications like the Microsoft Windows Media Player for Pocket PC allow the user to turn off the display while the application is running in the background. This article shows you how to do this with your own applications.

Applies to:
   Microsoft Windows Powered Pocket PC 2000
   Microsoft eMbedded Visual Tools
   Microsoft eMbedded Visual C++

Languages Supported

All languages.

The Code...

To keep it simple, let's try a short WinMain function that turns the display off, waits five seconds, and then switches the display back on. By following the steps below, you are also probing the display driver to see if it supports the power management escape sequence that enables the feature to work. Probing the display driver gives you all necessary information you need for using the code in your own application.

To create the sample, follow these simple steps:

  1. Create a new Pocket PC "Simple application" using eMbedded Visual C++.

  2. Exchange the code of the WinMain function with the following code:

    int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, 
                         LPWSTR pszCommandLine, int nCommandShow) 
    {
            HDC gdc;
            int iESC=SETPOWERMANAGEMENT;
    
            gdc = ::GetDC(NULL);
            if (ExtEscape(gdc, QUERYESCSUPPORT, sizeof(int), (LPCSTR)&iESC, 
                           0, NULL)==0)           
                     MessageBox(NULL,
                      L"Sorry, your Pocket PC does not support DisplayOff",
                      L"Pocket PC Display Off Feature",
                      MB_OK);
            else
            {
                   VIDEO_POWER_MANAGEMENT vpm;
                   vpm.Length = sizeof(VIDEO_POWER_MANAGEMENT);
                   vpm.DPMSVersion = 0x0001;
                   vpm.PowerState = VideoPowerOff;
    // Power off the display
                   ExtEscape(gdc, SETPOWERMANAGEMENT, vpm.Length, (LPCSTR) &vpm, 
                                 0, NULL);
                   Sleep(5000);
                   vpm.PowerState = VideoPowerOn;
    // Power on the display
                   ExtEscape(gdc, SETPOWERMANAGEMENT, vpm.Length, (LPCSTR) &vpm, 
                                 0, NULL);
                   ::ReleaseDC(NULL, gdc);
            }
            return 0;
    }
    
  3. Add the following declarations right before the WinMain function:

    // GDI Escapes for ExtEscape()
    #define QUERYESCSUPPORT    8
    
    // The following are unique to CE
    #define GETVFRAMEPHYSICAL   6144
    #define GETVFRAMELEN    6145
    #define DBGDRIVERSTAT    6146
    #define SETPOWERMANAGEMENT   6147
    #define GETPOWERMANAGEMENT   6148
    
    
    typedef enum _VIDEO_POWER_STATE {
        VideoPowerOn = 1,
        VideoPowerStandBy,
        VideoPowerSuspend,
        VideoPowerOff
    } VIDEO_POWER_STATE, *PVIDEO_POWER_STATE;
    
    
    typedef struct _VIDEO_POWER_MANAGEMENT {
        ULONG Length;
        ULONG DPMSVersion;
        ULONG PowerState;
    } VIDEO_POWER_MANAGEMENT, *PVIDEO_POWER_MANAGEMENT;
    I extracted these declarations from the include-file PWINGDI.H that ships with the Platform Builder 3.0. 
    
  4. Now compile and download the application to your device.

If you run the application, you will see the display go off for five seconds, and then come back on. In the event your Pocket PC does not support the power-off feature, a message will appear that states, "Sorry, your Pocket PC does not support DisplayOff."

What Applications Can Use This Feature?

As I stated previously, Windows Media Player is a perfect example of an application that could use this feature, because you don't necessarily want to use precious battery power for the display while you are listening to your favorite music. An application that functions like a cellular phone could also turn off the display while a conversation is in progress. Another interesting application is a screen saver that activates while the Pocket PC is in the docking cradle. The application could first show a nice, distracting graphic, and then later, though synchronization and the alarms would still work, switch off the display. This feature does not make sense for a video player, of course, but it could be useful for any application that needs time to render or calculate.

Gotchas

Never use the code above without having a means of turning the display back on. You should always use a toggle key, screen tap, or be able to press any key to turn the display back on. Otherwise, you might end up with a device that's unusable until you switch it off. Despite the logo rule, which states that you always have to save all states of your application in case your app gets closed by the Pocket PC operating system, do not save the state of the display if your application gets suspended. (The logo documentation is only available at VeriTest.) This would require you to turn off the display once your application comes back from suspend. In worst cases, your application would render your Pocket PC useless because it would always switch off the display once it was activated.

Also, not all Pocket PCs support this feature because it depends on the display driver of the OEM. Terry successfully tested the code on his Casio E115, and my test on the Compaq iPAQ was successful, too.

I want to thank Terry Myhrer from Software 309 who sent me the initial code for this article. — C.M.