Comment : gérer la rotation de l'écran

Mise à jour : novembre 2007

Vous pouvez développer des applications Direct3D pour des orientations d'écran autres que portrait. Toutefois, les pilotes de périphérique fournissent des niveaux variables de prise en charge pour le rendu dans une orientation qui n'est pas en mode portrait. C'est pourquoi votre application doit suivre des méthodes recommandées pour gérer la rotation d'écran.

Remarque :

Les applications Direct3D mobiles managées requièrent le logiciel Windows Mobile version 5.0 pour les appareils de type Pocket PC et Smartphone. Consultez Ressources externes pour le .NET Compact Framework pour plus d'informations sur le logiciel Windows Mobile et les Kits de développement logiciel.

Les exemples de code suivants figurent dans le Kit de développement logiciel (SDK) Windows.

Pour détecter qu'un écran a pivoté

  1. Ajoutez une référence au composant Microsoft.WindowsCE.Forms dans votre projet.

  2. La façon la plus simple d'ajouter un code de détection consiste à le placer dans le gestionnaire d'événements Resize du formulaire principal. Ajoutez un délégué pour le gestionnaire d'événements dans le constructeur de votre formulaire.

    this.Resize += new System.EventHandler(this.MyForm_Resize);
    
    AddHandler Resize, AddressOf Me.MyForm_Resize
    
  3. Ajoutez la variable du membre de classe orientationChanged de type Boolean qui suivra le changement de l'orientation. Utilisez la valeur de la propriété ScreenOrientation pour déterminer l'orientation actuelle. Une valeur Angle0 indique le mode portrait. Si l'orientation actuelle est différente, notez-la en définissant l'indicateur orientationChanged. Normalement, aucune mesure n'est prise à ce stade, car l'application peut être en arrière-plan. Ne modifiez pas par programme l'orientation ici, car la modification d'orientation qui a déclenché l'événement de redimensionnement n'est probablement pas encore terminée. Si vous essayez de rétablir l'orientation maintenant, cette tentative échouera.

        // 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
    

Mode d'action lorsque l'écran a pivoté

  • Vous pouvez vérifier chaque frame pour déterminer si l'orientation a changé. La méthode CheckRotation dans l'exemple de code suivant rétablit le mode portrait comme orientation de l'écran en définissant la propriété ScreenOrientation. Vous devez envisager de prendre d'autres mesures en fonction de l'expérience utilisateur souhaitée. Par exemple, vous ne voulez pas que l'application modifie l'orientation de l'écran automatiquement, mais vous souhaitez qu'elle avertisse l'utilisateur que le rendu ne continuera pas s'il n'apporte aucune modification. Si vous changez l'orientation de l'écran, vous devez également enregistrer et restaurer l'orientation de l'écran initiale, comme indiqué dans l'exemple suivant. Si vous ne changez pas l'orientation de l'écran, mais prenez une autre mesure à la place, le rendu peut continuer normalement, échouer en silence ou échouer en retournant une exception.

    L'exemple de code suivant essaie de mettre l'appareil en mode d'orientation portrait, s'il n'est pas déjà en mode portrait. La méthode CheckOrientation retourne true si l'appareil est en mode portrait.

    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
    

Pour restaurer l'orientation à la fermeture de l'application

  • Si vous décidez de modifier l'orientation de l'écran par programme, vous devez également restaurer l'orientation de l'application initiale à la fermeture de l'application. Puisqu'une application peut essayer de modifier l'orientation en cours d'exécution, vous devez enregistrer l'orientation initiale et la restaurer à la sortie de l'application. Ajoutez une variable membre pour stocker l'orientation initiale.

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

    Ajoutez ceci à la fin de la méthode Main pour essayer de restaurer l'orientation à la sortie de l'application.

    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
    

Voir aussi

Concepts

.Rubriques Comment relatives au .NET Compact Framework

Autres ressources

Direct3D Mobile Matrices, exemple

Programmation Direct3D Mobile dans le .NET Compact Framework