HOW TO:處理螢幕旋轉

更新:2007 年 11 月

您可以開發非縱向螢幕方向的 Direct3D 應用程式。不過,裝置驅動程式對於非縱向方向中的呈現,提供不同層級的支援,因此您的應用程式應該在處理螢幕旋轉時,遵循其所建議的作法。

注意事項:

Managed Direct3D Mobile 應用程式需要適用於 Pocket PC 和 Smartphone 的 Windows Mobile 5.0 版軟體。如需 Windows Mobile 軟體和 SDK 的詳細資訊,請參閱 .NET Compact Framework 的外部資源

下列程式碼範例出自於 Windows Software Development Kit (SDK)。

若要偵測螢幕是否處於旋轉狀態

  1. 在您的專案中加入 Microsoft.WindowsCE.Forms 元件的參考。

  2. 加入偵測程式碼的最為簡單方式,就是將程式碼放置在主表單的 Resize 事件處理常式中。在表單的建構函式中加入事件處理常式的委派。

    this.Resize += new System.EventHandler(this.MyForm_Resize);
    
    AddHandler Resize, AddressOf Me.MyForm_Resize
    
  3. 加入布林 orientationChanged 類別成員變數,在方向變更時進行追蹤。使用 ScreenOrientation 屬性的值判斷目前的方向。Angle0 的值表示縱向模式。如果目前的方向不同於這個方向,請設定 orientationChanged 旗標以記下這點。通常,由於應用程式可能位於背景,所以此時不會採取任何動作。由於觸發調整大小事件的方向變更可能尚未完成,請勿以程式設計方式在此變更方向。如果現在嘗試將方向變回就會失敗。

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

在螢幕旋轉時如何採取動作

  • 您可以檢查每個框架 (Frame) 以判斷方向是否已經變更。下列程式碼範例中的 CheckRotation 方法,會藉由設定 ScreenOrientation 屬性,將螢幕方向變回縱向模式。您應該根據您所要的使用者經驗,採取其他動作。例如,您可能不希望讓應用程式自行變更螢幕方向,而是讓應用程式通知使用者將方向變回,否則將不再繼續呈現。如果的確變更螢幕方向,您也應該如下列範例所示,儲存並還原初始的螢幕方向。如果沒有變更螢幕方向,而是採取其他一些動作,呈現就有可能繼續正常進行、無訊息地失敗,或是傳回例外狀況而失敗。

    下列的程式碼範例嘗試以縱向模式設定裝置 (如果尚未使用縱向模式)。如果裝置是縱向模式,CheckOrientation 方法會傳回 true。

    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
    

若要在應用程式結束時還原方向

  • 如果決定用程式設計方式變更螢幕方向,在應用程式結束時,您也應該用程式設計方式還原初始的應用程式方向。因為應用程式可能會在執行時嘗試變更方向,所以您要儲存初始的方向,並在應用程式結束時加以還原。加入成員變數以儲存初始的方向。

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

    將這個項目加入至 Main 方法的結尾,嘗試在應用程式結束時還原方向。

    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
    

請參閱

概念

.NET Compact Framework HOW TO 主題

其他資源

Direct3D Mobile 矩陣範例

.NET Compact Framework 中的 Mobile Direct3D 程式設計