Thursday, January 3, 2008

Determine your current Screen Orientation on a Windows Mobile Device

There are many facets of "orientation-aware" apps. There are 2 portrait and 2 landscape modes making 4 possible orientations (0, 90, 180, 270).

I have an application that lets the user set custom keys in an application. At first, I thought I only need to store a landscape set and a portrait set; however, depending on the user, the landscape mode might be in the 90 degree position or the 270 degree position. The keys selected by the user may be very different depending on which one of these orientations gets used.

However, it is very simple to find out what the current screen orientation in on the device:

Imports Microsoft.WindowsCE.Forms
.
.
.
Select Case SystemSettings.ScreenOrientation

Case ScreenOrientation.Angle0
.

.
.
Case ScreenOrientation.Angle90
.

.
.
Case ScreenOrientation.Angle180
.

.
.
Case ScreenOrientation.Angle270
.

.
.
End Select


More Info at:
http://msdn2.microsoft.com/en-us/library/microsoft.windowsce.forms.systemsettings.screenorientation(VS.80).aspx

Detecting Screen Orientation Changes in Windows Mobile Apps

If you are working on a Windows Mobile application, you probably need to be orientation-aware. If the user changes the screen from portrait to landscape or vice versa, you may need to do something based on this change.

The Resize event for the form will tell you when an orientation change has occured. You will probably have to use the me.Width and me.Height to know how it was changed. Look at the If statement in the UpdateButtonLayout sub below.


Private Sub Form_NumberEntry_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize

UpdateButtonLayout()

End Sub

Private Sub UpdateButtonLayout()

Dim ratio As Double = CInt(Me.Width) / CDbl(Me.Height)

If ratio <= 0.9 Then ' portrait .
.
.

Else ' landscape

.
.
.
End If

End Sub


Being orientation away for control may be as simple as using the Anchor and Dock properties on the control, but there are many times, that this is not sufficient or that you need to do something else like auto fit columns in a listview.