Thursday, July 2, 2009

Change the Screen Rotation in your application

Windows Mobile devices allows user to rotate the screen orientation, on some devices, this rotation is automatically done while sliding keyboard is opened to provide the good orientation to user to get a chance to type text using the hardware keyboard. More advanced devices get an accelerometer and can automatically rotate the screen while the user physically rotate the device from portrait to landscape and vice versa.
Windows Embedded CE devices on their side can also have screen rotation enabled, but as they are usually for industrial purposes, this feature is not necessarily enabled.

On both version, when user have to do screen rotation the ChangeDisplaySettingsEx function is used. The sample code below shows up the steps to perform screen rotation.

 
BOOL RotateScreen(int iRotationAngle)
{
int iAngle;

// Check if rotation angle is a multiple of 90 degres
if ((iRotationAngle % 90) != 0)
{
return FALSE;
}

// Screen rotation is a bit field value
// 0 = 0, 1 = 90, 2 = 180, 4 = 270
iAngle = iRotationAngle / 90;
if( iAngle == 3 )
iAngle = 0x4;

//
DEVMODE devMode;
memset (&devMode, 0, sizeof (devMode));
devMode.dmSize = sizeof (devMode);
devMode.dmFields = DM_DISPLAYQUERYORIENTATION;

// Retrieve the supported screen rotation angles
if (DISP_CHANGE_SUCCESSFUL != ChangeDisplaySettingsEx(NULL, &devMode, NULL, CDS_TEST, NULL))
{
return FALSE;
}

// Check if required screen rotation is supported
if (iAngle == 0 || iAngle & devMode.dmDisplayOrientation)
{
memset(&devMode, 0, sizeof (devMode));
devMode.dmSize = sizeof (devMode);
devMode.dmFields = DM_DISPLAYORIENTATION;
devMode.dmDisplayOrientation = iAngle;

if (DISP_CHANGE_SUCCESSFUL != ChangeDisplaySettingsEx(NULL, &devMode, NULL, CDS_RESET, NULL))
{
return FALSE;
}
else return TRUE;
}

return FALSE;
}


Thanks to Alban for his help.

- Nicolas

No comments: