Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Pocket PC Screen Rotation

0.00/5 (No votes)
12 Apr 2007 2  
A basic sample on how to rotate the screen of a pocket PC.

Introduction

This week I need to implement a feature to rotate screen of the pocket PC where my Application runs depending on the content of the selected screen. I started some research, but couldn't find anything about it. Actually I couldn't find even an idea where I should start.

I had a thought that such a feature could be implemented by using SystemParametersInfo API, so I went to MSDN and take a deeper look at its parameters. Nothing seemed to safisfy my needs.

So, started browsing the whole Windows CE 3.0 SDK. Once again, I couldn't find anything.

I decided to take a look at ChangeDisplaySettingsEx API, which I had already used several times before but for Win32 APPs, not for pocket PC applications. By looking at DEVMODE structure I saw the member dmDisplayOrientation, and noticed I was close to figuring out how to implement such a feature.

In fact, ChangeDisplaySettingsEx is present in both SDK's (Pocket PC and Win32).

Once I found that structure member there was no tricky to find out how to put it in action.

Using the code

The use of this API in order to flip/rotate the screen of a pocket PC is preety easy, but is very important to mention that it is not present in Windows CE 3.0 or earlier and the capabilty to flip the screen also depends on whether the display device supports it or not.

Before trying to flip the screen, it would be nice to check the device for such a support and, depending on the result warn the user about the lack of support of the device for Screen rotation.

Before going to the code itself, i'd like to list the angle constants below:

DMDO_0

Indicates that the display is on portrait mode.

DMDO_90

Indicates that the display is on landscape mode for people who uses right hand.

DMDO_180

Indicates that the display is upside-down portrait mode.

DMDO_270

Indicates that the display is on landscape mode for people who uses left hand.

Those constants is what we will use whenever we want to put the display at a given position, and they can also be used to determine what positions the device support.

Below I ilustrate how to check the device for what positions it supports:

//
// Check the device for capabilty to rotate the display
// 

DEVMODE DeviceMode;

memset(&DeviceMode, NULL, sizeof(DEVMODE));

DeviceMode.dmSize = sizeof(DeviceMode);
DeviceMode.dmFields = DM_DISPLAYQUERYORIENTATION;

ChangeDisplaySettingsEx(NULL, &DeviceMode, NULL, CDS_TEST, NULL);

Now you can perform a bit-wise operation towards the stuct member dmDisplayOrientation in order to find out what of those constants are supported. Of course if none of those constants are present in dmDisplayOrientation it means that the device does not support screen rotation.

Once you know the device supports screen rotation, you can query the current position of the screen, and it is done this way:

//
// Determine the current position of the screen
// 

DEVMODE DeviceMode;
    
memset(&DeviceMode, NULL, sizeof(DeviceMode));
DeviceMode.dmSize=sizeof(DeviceMode);
DeviceMode.dmFields = DM_DISPLAYORIENTATION;

if ( 
   DISP_CHANGE_SUCCESSFUL == ChangeDisplaySettingsEx(
                            NULL, 
                            &DeviceMode, 
                            NULL, 
                            CDS_TEST, 
                            NULL
                            )
  )
//ChangeDisplaySettingsEx executed successfully
{
    switch (DeviceMode.dmDisplayOrientation)
    {
    case DMDO_0:
    //The display is currently in portrait mode

    case DMDO_90:
    //The display is currently in landscape mode 
    //for people who uses right hand

    case DMDO_180:
    //The display is currently in upside-down portrait mode

    case DMDO_270:
    //The display is currently in landscape mode 
    //for people who uses left hand

    }

}
        return DeviceMode.dmDisplayOrientation;
else
//Error executing ChangeDisplaySettingsEx

Finally, how to set the desired position:

//
// Change the position of the screen
//

DEVMODE DeviceMode;
    
memset(&DeviceMode, NULL, sizeof(DeviceMode));
DeviceMode.dmSize=sizeof(DeviceMode);
DeviceMode.dmFields = DM_DISPLAYORIENTATION;
DeviceMode.dmDisplayOrientation = DMDO_90; //Put your desired 
                       //position right here.

if (DISP_CHANGE_SUCCESSFUL == ChangeDisplaySettingsEx(
                            NULL,
                            &DeviceMode,
                            NULL,
                            CDS_RESET,
                            NULL
                             )
   )
   //Screen was rotated successfully
else
   //Screen could not be rotated

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here