如何:处理屏幕旋转
更新:2007 年 11 月
您可以为非纵向的屏幕方向开发 Direct3D 应用程序。不过,设备驱动程序为使用非纵向方向进行呈现提供了各种级别的支持,因此,您的应用程序应遵循推荐的做法来处理屏幕旋转。
说明: |
---|
托管 Direct3D 移动应用程序需要使用适用于 Pocket PC 和 Smartphone 的 Windows Mobile 5.0 版软件。有关 Windows Mobile 软件和 SDK 的信息,请参见 .NET Compact Framework 的外部资源。 |
下面的代码示例包含在 Windows 软件开发工具包 (SDK) 中。
检测屏幕是否处于已旋转状态
在项目中添加对 Microsoft.WindowsCE.Forms 组件的引用。
添加检测代码的最简便方法是将其放置在主窗体的 Resize 事件处理程序中。在窗体的构造函数中添加该事件处理程序的委托。
this.Resize += new System.EventHandler(this.MyForm_Resize);
AddHandler Resize, AddressOf Me.MyForm_Resize
添加布尔型 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
屏幕旋转后如何操作
您可以检查每个帧来确定方向是否已发生更改。下面的代码示例中的 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