Creating a Project for the Screen Rotation Application

In this procedure, you will create a project in your platform and add sample application source code to the project. The source files must be added to the project after it is created. This enables you to compile, link, and include the application in your platform.

The sample application code in this section uses the ChangeDisplaySettingsEx function. The code also shows how to set the DEVMODE structure to query the driver on the rotation angles it supports, get the current rotation angle, and set a new rotation angle.

Each time you run this application on the CEPC, the screen will be rotated clockwise by 90 degrees.

To create a project for the sample application,

  1. From the File menu, choose New Project or File, and then, on the Projects tab, choose WCE Application.

  2. In the Project name box, type a name for the project. By default, Platform Builder stores the project in a directory immediately subordinate to your platform directory. For example, %_WINCEROOT%\Public\<Platform Name>\<Project Name>. Choose OK.

  3. In the New Project Wizard dialog box, choose An empty project, and then choose Finish. A new project subdirectory is created in the platform directory.

  4. Use Windows Explorer to navigate to the project subdirectory. Using a text editor, create a new file called rotation.cpp. Cut and paste the following code into the file you just created. Save your changes. Make sure that the file you just created has a .cpp extension, because an incorrect extension will cause compilation errors.

    **Note   **To make the following code example easier to read, error checking is not included. This code example should not be used in a release configuration unless it has been modified to include secure error handling.

    /**********************************************************************
    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
    PARTICULAR PURPOSE.
    
    Copyright (c) 1999 Microsoft Corporation. All Rights Reserved.
    
    MODULE: 
      rotation.cpp
    
    ABSTRACT: 
      This application code demonstrates the how you can set up the DEVMODE structure and use the ChangeDisplaySettingsEx function to rotate the screen. 
      Each time you run the executable for this code, the screen will rotate clockwise by 90 degrees.
    
    **********************************************************************/
    
    #include <windows.h>
    
    int
    WINAPI
    WinMain(
       HINSTANCE,
       HINSTANCE,
    #ifdef UNDER_CE
       LPWSTR,
    #else
       LPSTR,
    #endif
       int
       )
    {
       DEVMODE DevMode;
    
       int RotationAngles;
       int CurrentAngle;
       int NewAngle;
    
       //
       // Check for rotation support by getting the rotation angles supported.
       //
    
       memset (&DevMode, 0, sizeof (DevMode));
       DevMode.dmSize   = sizeof (DevMode);
       DevMode.dmFields = DM_DISPLAYQUERYORIENTATION;
    
       if (DISP_CHANGE_SUCCESSFUL == ChangeDisplaySettingsEx(NULL, &DevMode, NULL, CDS_TEST, NULL))
       {
          RotationAngles = DevMode.dmDisplayOrientation;
          RETAILMSG(1, (L"ChangeDisplaySettingsEx supports these rotation angles %d", RotationAngles));
       }
       else
       {
          RETAILMSG(1, (L"ChangeDisplaySettingsEx failed to get the supported rotation angles."));
          RotationAngles = -1;
       }
    
       //
       // Get the current rotation angle.
       //
    
       memset(&DevMode, 0, sizeof (DevMode));
       DevMode.dmSize   = sizeof (DevMode);
       DevMode.dmFields = DM_DISPLAYORIENTATION;
    
       if (DISP_CHANGE_SUCCESSFUL == ChangeDisplaySettingsEx(NULL, &DevMode, NULL, CDS_TEST, NULL))
       {
          CurrentAngle = DevMode.dmDisplayOrientation;
          RETAILMSG(1, (L"ChangeDisplaySettingsEx reports the current rotation as %d", CurrentAngle));
       }
       else
       { 
          RETAILMSG(1, (L"ChangeDisplaySettingsEx failed to get the current rotation angle."));
          CurrentAngle = -1;
       }
    
       //
       // Rotate to the "next" angle.
       //
    
       if (CurrentAngle >= 0 && RotationAngles >= 0)
       {
          NewAngle = CurrentAngle;
    
          do
          {
             NewAngle <<= 1;
    
             if (NewAngle == DMDO_0)
             {
                NewAngle = DMDO_90;
             }
    
             if (NewAngle > DMDO_270)
             {
                NewAngle = DMDO_0;
             }
          } while (!(NewAngle & RotationAngles) && (NewAngle != DMDO_0));
    
          memset(&DevMode, 0, sizeof (DevMode));
          DevMode.dmSize               = sizeof (DevMode);
          DevMode.dmFields             = DM_DISPLAYORIENTATION;
          DevMode.dmDisplayOrientation = NewAngle;
    
          if (DISP_CHANGE_SUCCESSFUL == ChangeDisplaySettingsEx(NULL, &DevMode, NULL, CDS_RESET, NULL))
          {
             RETAILMSG(1, (L"ChangeDisplaySettingsEx changed rotation angle to %d", NewAngle));
          }
          else
          {
             RETAILMSG(1, (L"ChangeDisplaySettingsEx failed to change the rotation angle to %d", NewAngle));
          }
       }
    
       return 0;
    }
    
  5. In the Platform Builder Workspace window, ensure that the FileView tab is active.

  6. Right-click the Source Files folder, and choose Add Files to Folder. Select rotation.cpp and choose OK.

You are now done with configuring the sample application for screen rotation.

See Also

How to Implement Screen Rotation | Rotating the Content of the Screen

Last updated on Wednesday, April 13, 2005

© 2005 Microsoft Corporation. All rights reserved.