views:

1299

answers:

4

Hi,

How can I know in a C#-Application, in which direction the screen of the mobile device is orientated? (i.e. horizontal or vertical).

A: 

Just guessing but my first attempt would be:

var rect = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
// or var rect = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;

var ratio = rect.Width / rect.Height;

if (ratio == 1.0) // square screen.
if (ratio > 1.0) // landscape.
if (ratio < 1.0) // portrait.
Jason Stangroome
This won't work, but it's a good example of how the new "var" keyword is mainly good at hiding otherwise-obvious problems.
MusiGenesis
+3  A: 

In Microsoft.WindowsMobile.Status there is a class which keeps track of all kinds of properties of your device. Besides the one you need, DisplayRotation, it also contains properties about phone coverage, Nr of missed calls, next appointment and many more. See msdn for more info.

You can also add an event-handler to be notified of changes of these properties.

Ruben
+2  A: 

Add a reference to Microsoft.WindowsCE.Forms to your project. Then you can reference the Microsoft.WindowsCE.Forms.SystemSettings.ScreenOrientation property, which will give you what you need.

Incidentally, you can set this property, so it can be used to set your screen orientation also.

MusiGenesis
+1  A: 

you should add 2 Refrences to the project: Microsoft.WindowsMobile Microsoft.WindowsMobile.Status

then you can use this code for determine orientation:

int orientation=Microsoft.WindowsMobile.Status.SystemState.DisplayRotation;
if(orientation== 90 || orientation==-90 || orientation==270) //Landscape is 90 or -90 or 270
{
    //your code;
}
else
{
    //your code;
}
mSafdel