是否可以在本机 Windows 8.1 桌面应用程序中确定屏幕方向

Is it possible to determine screen orientation in a native Windows 8.1 Desktop App?

本文关键字:应用程序 方向 屏幕 桌面 本机 Windows 是否      更新时间:2023-10-16

我需要考虑设备可以处于的不同方向。又名横向、横向翻转、纵向或纵向翻转。我的应用是用本机C++编写的,并且作为桌面应用在 Windows 8.1 上运行(跨平台不是任何要求)。

我知道我可以使用此处概述的方法确定设备是纵向还是横向Microsoft:http://msdn.microsoft.com/en-us/library/ms812142.aspx

但是,无法区分横向和横向翻转(或纵向和纵向翻转)。

我可以通过检查 DisplayInformation.CurrentOrientation 属性来获得我需要的东西,但它是一个 WinRT API。这意味着如果我想使用它,我的应用程序必须使用 CLR,这是一个非启动器。

此外,我真的很想将我的应用程序保留为单个可执行文件,我认为没有一种干净的方法来做到这一点并同时调用托管 API。但话又说回来,我对本机 + 托管代码集成非常缺乏经验。

那么,有谁知道在Windows中仅使用本机代码来确定显示方向的任何方法?

我想

通了。这实际上比我想象的要容易得多。EnumDisplaySettings() 不会填充 DEVMODE 结构中的 dmDisplayOrientation 字段,但 EnumDisplaySettingsEx() 会填充。所以这实际上很容易:)

这里有一个很好的例子。

检测窗口大小在 XAML 中更改:

<Page x:Class="ClockCpp.MainPage"
      Loaded="Page_Loaded"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:ClockCpp"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      Background="black"
      SizeChanged="Page_SizeChanged">

然后使用是在纵向或横向之间切换:

Boolean t46 = true;
void ClockCpp::MainPage::Page_SizeChanged(Platform::Object^ sender, Windows::UI::Xaml::SizeChangedEventArgs^ e)
{
        if (t46 == true)
        {
            t46 = false; // portrait
            timetext->FontSize = 120; // 2/3 of 180
            Othercontrols->Visibility = Windows::UI::Xaml::Visibility::Visible;
        }
        else
        {
            t46 = true; // landscape
            timetext->FontSize = 180;
            Othercontrols->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        }   
}

您还可以比较屏幕高度和宽度。如果宽度值更高,则它是横向,反之亦然。

#include <windows.h>
int theScreenWidth = GetSystemMetrics(SM_CXFULLSCREEN);
int theScreenHeight = GetSystemMetrics(SM_CYFULLSCREEN);
if (theScreenWidth > theScreenHeight) 
    MessageBox(NULL,"Run in landscape.","Landscape",MB_OK);
else
    MessageBox(NULL,"Run in portrait.","Portrait",MB_OK);