當前位置:編程學習大全網 - 編程語言 - 如何:處理屏幕旋轉

如何:處理屏幕旋轉

不過,設備驅動程序為使用非縱向方向進行呈現提供了各種級別的支持,因此,您的應用程序應遵循推薦的做法來處理屏幕旋轉。說明:托管Direct3D 移動應用程序需要使用適用於 Pocket PC 和 Smartphone 的 Windows Mobile 5.0 版軟件。有關 Windows Mobile 軟件和 SDK 的信息,請參見 .NET Compact Framework 的外部資源。下面的代碼示例包含在 Windows 軟件開發工具包 (SDK) 中。檢測屏幕是否處於已旋轉狀態在項目中添加對 Microsoft.WindowsCE.Forms 組件的引用。添加檢測代碼的最簡便方法是將其放置在主窗體的 Resize 事件處理程序中。在窗體的構造函數中添加該事件處理程序的委托。 C# this.Resize += new System.EventHandler(this.MyForm_Resize); Visual Basic AddHandler02Resize,02AddressOf02Me.MyForm_Resize添加布爾型 orientationChanged 類成員變量,該變量將在方向更改時進行跟蹤。使用 ScreenOrientation 屬性的值確定當前方向。值為 Angle0 指示縱向模式。如果當前方向與此不同,請設置 orientationChanged 標誌來將其標記下來。通常,此時不執行任何操作,因為應用程序可能在後臺。不要在此處以編程方式更改方向,因為觸發調整大小事件的方向更改可能尚未完成。如果您現在嘗試改回原來的方向,則會發生失敗。 C# // Add a class member variable to// store that the orientation has changed.bool orientationChanged = false; privatevoid MyForm_Resize(object sender, EventArgs e) { if (SystemSettings.ScreenOrientation != ScreenOrientation.Angle0) orientationChanged = true; } Visual Basic ' Add a class member variable to ' store that the orientation has changed. Dim OrientationChanged AsBoolean = FalsePrivateSub MyForm_Resize(ByVal sender AsObject, ByVal e As EventArgs) If (SystemSettings.ScreenOrientation <> ScreenOrientation.Angle0) Then orientationChanged = TrueEndIfEndSub屏幕旋轉後如何操作您可以檢查每個幀來確定方向是否已發生更改。下面的代碼示例中的 CheckRotation 方法通過設置 ScreenOrientation 屬性將屏幕方向改回了縱向模式。您應考慮根據期望的用戶體驗來進行其他操作。例如,您可能不希望應用程序自己更改屏幕方向,而通知用戶以下情況:除非用戶改回原來的方向,否則將不再繼續呈現。如果您真的更改了屏幕方向,則還應保存並還原初始屏幕方向,如下面的示例所示。如果您沒有更改屏幕方向,而是執行了某種其他操作,則可以正常地繼續進行呈現,它可能悄悄地終止,但也可能會在失敗時返回壹個異常。下面的代碼示例嘗試將設備置於縱向模式(如果它還未處於縱向模式)。如果設備處於縱向模式,則 CheckOrientation 方法返回 true。 C# privatebool 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.returnfalse; } } returntrue; } // Use the CheckOrientation() method before rendering occurs.// All rendering for each frame occurs here.privatevoid Render() { // If the device is not oriented properly, // some display drivers may not work.if (!CheckOrientation()) return; // Rendering code omitted here. } Visual Basic PrivateFunction CheckOrientation() AsBoolean ' orientationChanged issettotruein 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 ' Donot attempt to change it again. orientationChanged = falseCatchAs Exception ' Failed to change the display mode. ReturnfalseEndTryEndIfReturntrueEndFunction ' Use the CheckOrientation() method before rendering occurs. ' All rendering foreach frame occurs here. PrivateSub Render() ' If the device isnot oriented properly, ' some display drivers may not work. IfNot CheckOrientation ThenReturnEndIf ' Rendering code omitted here. EndSub應用程序退出時還原方向如果您決定以編程方式更改屏幕方向,則還應當在應用程序退出時還原初始應用程序方向。由於應用程序可能嘗試在運行時更改方向,因此需要保存初始方向並在應用程序退出時將其還原。添加壹個成員變量以存儲初始方向。

  • 上一篇:計算機考級壹***考幾級啊?
  • 下一篇:c語言常用算法有哪些
  • copyright 2024編程學習大全網