Compartilhar via


How to: Handle Screen Rotation

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

You can develop Direct3D applications for screen orientations other than portrait. However, device drivers provide varying levels of support for rendering in a non-portrait orientation, so your application should follow recommended practices for handling screen rotation.

Note

Managed Direct3D mobile applications require Windows Mobile version 5.0 software for Pocket PCs and Smartphones. See External Resources for the .NET Compact Framework for information about Windows Mobile software and SDKs.

The following code examples are in the Windows Software Development Kit (SDK).

To detect that a screen is in a rotated state

  1. Add a reference to the Microsoft.WindowsCE.Forms component to your project.

  2. The easiest way to add detection code is to place it in the main form’s Resize event handler. Add a delegate for the event handler in your form’s constructor.

    this.Resize += new System.EventHandler(this.MyForm_Resize);
    
    AddHandler Resize, AddressOf Me.MyForm_Resize
    
  3. Add the Boolean orientationChanged class member variable which will track when the orientation has changed. Use the value of the ScreenOrientation property to determine the current orientation. A value of Angle0 indicates portrait mode. If the current orientation is different from that, take note of it by setting the orientationChanged flag. Normally, no action is taken at this point because the application might be in the background. Do not programmatically change the orientation here because the orientation change that triggered the resize event is probably not complete yet. If you try to change the orientation back now it will fail.

        // Add a class member variable to
        // store that the orientation has changed.
        bool orientationChanged = false;
    
        private void MyForm_Resize(object sender, EventArgs e)
        {
           if (SystemSettings.ScreenOrientation !=
             ScreenOrientation.Angle0)
            orientationChanged = true;
        }
    
        ' Add a class member variable to
        ' store that the orientation has changed.
        Dim OrientationChanged As Boolean = False
    
    Private Sub MyForm_Resize(ByVal sender As Object, ByVal e As EventArgs)
        If (SystemSettings.ScreenOrientation <> ScreenOrientation.Angle0) Then
           orientationChanged = True
        End If
    End Sub
    

How to take action when the screen is rotated

  • You can check each frame to determine whether the orientation has changed. The CheckRotation method in the following code example changes the screen orientation back to portrait mode by setting the ScreenOrientation property. You should consider taking other actions depending on the desired user experience. For example, you may not want the application to change the screen orientation on its own, but instead notify the user that rendering will not continue unless the user changes it back. If you do change the screen orientation, you should also save and restore the initial screen orientation, as shown in the following example. If you do not change the screen orientation but instead take some other action, rendering may continue normally, it may fail silently, or it may fail by returning an exception.

    The following code example attempts to put the device in portrait orientation mode, if it is not already in portrait mode. The CheckOrientation method returns true if the device is in portrait mode.

    private bool CheckOrientation()
    {
        // orientationChanged is set to true in resize if it is
        // detected that the orientation of the device has changed.
        if (orientationChanged)
        {
          // Attempt to change the display back to portrait mode.
          try
          {
             SystemSettings.ScreenOrientation =
              ScreenOrientation.Angle0;
             // Now that the orientation is back to portrait mode
             // Do not attempt to change it again.
             orientationChanged = false;
           }
          catch (Exception)
          {
             // Failed to change the display mode.
             return false;
          }
        }
        return true;
    }
    
    // Use the CheckOrientation() method before rendering occurs.
    // All rendering for each frame occurs here.
    
    private void Render()
    {
        // If the device is not oriented properly, 
        // some display drivers may not work.
        if (!CheckOrientation())
            return;
            // Rendering code omitted here.
    }
    
    Private Function CheckOrientation() As Boolean
        ' orientationChanged is set to true in resize if it is
        ' detected that the orientation of the device has changed.
        If orientationChanged Then
            ' Attempt to change the display back to portrait mode.
            Try 
                SystemSettings.ScreenOrientation = ScreenOrientation.Angle0
                ' Now that the orientation is back to portrait mode
                ' Do not attempt to change it again.
                orientationChanged = false
            Catch  As Exception
                ' Failed to change the display mode.
                Return false
            End Try
        End If
        Return true
    End Function
    
    ' Use the CheckOrientation() method before rendering occurs.
    ' All rendering for each frame occurs here.
    Private Sub Render()
        ' If the device is not oriented properly, 
        ' some display drivers may not work.
        If Not CheckOrientation Then
            Return
        End If
        ' Rendering code omitted here.
    End Sub
    

To restore the orientation when the application exits

  • If you decide to change the screen orientation programmatically, you should also restore the initial application orientation when your application exits. Because an application may attempt to change the orientation while running, you need to save the initial orientation and restore it when the application exits. Add a member variable to store the initial orientation.

    ScreenOrientation initialOrientation = SystemSettings.ScreenOrientation;
    
    ScreenOrientation initialOrientation = SystemSettings.ScreenOrientation;
    

    Add this to the end of the Main method to attempt to restore the orientation when the application exits.

    if (SystemSettings.ScreenOrientation != initialOrientation)
    {
        try
        {
           SystemSettings.ScreenOrientation = initialOrientation;
        }
        catch (Exception)
        {
            // Unable to change the orientation back 
            // to the original configuration. 
    
            MessageBox.Show("This sample was unable to set the " +
                "orientation back to the original state.");
        }
    }
    
    If (SystemSettings.ScreenOrientation <> initialOrientation) Then
        Try 
            SystemSettings.ScreenOrientation = initialOrientation
        Catch  As Exception
            ' Unable to change the orientation back 
            ' to the original configuration. 
            MessageBox.Show(("This sample was unable to set the " & _
                "orientation back to the original state."))
        End Try
    End If
    

See Also

Concepts

.NET Compact Framework How-to Topics

Other Resources

Mobile Direct3D Programming in the .NET Compact Framework