如何确定移动设备的C#屏幕方向?

如何确定移动设备的C#屏幕方向?,c#,user-interface,windows-mobile,C#,User Interface,Windows Mobile,在C#应用程序中,我如何知道移动设备的屏幕朝向哪个方向?(即水平或垂直)。只是猜测,但我的第一次尝试是: 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) // squ

在C#应用程序中,我如何知道移动设备的屏幕朝向哪个方向?(即水平或垂直)。

只是猜测,但我的第一次尝试是:

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.
var rect=System.Windows.Forms.Screen.PrimaryScreen.Bounds;
//或var rect=System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
变比=矩形宽度/矩形高度;
if(比率==1.0)//方形屏幕。
如果(比率>1.0)//横向。
if(比率<1.0)//纵向。

在Microsoft.WindowsMobile.Status中,有一个类可以跟踪设备的各种属性。 除了您需要的DisplayRotation之外,它还包含有关电话覆盖率、未接来电数、下次约会等属性。有关更多信息,请参阅


您还可以添加事件处理程序,以便在这些属性发生更改时收到通知。

将对Microsoft.WindowsCE.Forms的引用添加到项目中。然后你可以参考 Microsoft.WindowsCE.Forms.SystemSettings.ScreenOrientation属性,该属性将为您提供所需的内容


顺便说一句,您可以设置此属性,以便它也可以用于设置屏幕方向。

您应该向项目添加两个参考: Microsoft.WindowsMobile Microsoft.WindowsMobile.Status

然后可以使用此代码确定方向:

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;
}

这是行不通的,但这是一个很好的例子,说明了新的“var”关键字主要擅长隐藏其他明显的问题。