Matthias Cipold
Matthias Cipold

Reputation: 11

How to set the orientation of a UWP app programmatically

I am developing a UWP Application for a physical device that can be accessed from two sides (display is facing up). The default orientation is Landscape and I would like to be able to flip (rotate by 180°) the orientation of the application programmatically.

I already tried two different things:

1. Change the Windows display orientation using the Win32 API

For this, you need to use API functions contained in user32.lib, which cannot be done / is not allowed from a UWP application. Therefore, I wrote a separate program that could be triggered from the UWP application. Here you can see how I implemented the display orientation based on the code provided with this question:

#include <windows.h>

bool SetDisplayOrientation(bool flip) {
    int index = 0;
    DISPLAY_DEVICE dd;
    dd.cb = sizeof(DISPLAY_DEVICE);
    while (EnumDisplayDevices(NULL, index++, &dd, 0) && !(dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE));

    DEVMODE dm;
    ZeroMemory(&dm, sizeof(dm));
    dm.dmSize = sizeof(DEVMODE);
    if (!EnumDisplaySettings(dd.DeviceName, ENUM_CURRENT_SETTINGS, &dm)) {
        return false;
    }
    dm.dmDisplayOrientation = flip ? DMDO_180 : DMDO_DEFAULT;

    dm.dmFields = (DM_DISPLAYORIENTATION);
    if (ChangeDisplaySettings(&dm, CDS_TEST) != DISP_CHANGE_SUCCESSFUL) {
        return false;
    }

    return (ChangeDisplaySettings(&dm, 0) == DISP_CHANGE_SUCCESSFUL);
}

This works, but when I run the UWP app in Windows 10's Kiosk mode (as we intend to do), it hides the application after the rotation, showing a blank screen if I interacted with the UI before (no clue why that happens either, looks like a bug to me). If I didn't interact with the UI, the rotation works just fine, but that's no solution...

2. Use the DXGI swap chain to rotate the app?

Googling for solutions I quickly came across IDXGISwapChain1::SetRotation and also found this example program. But this all together seems to not be the correct solution for a simple UWP user interface as we would need to implement our UI using DirectX. Please correct me if I am wrong here.

3. Change AutoRotationPreferences

I also tried executing the following command on a button press, but it did not do anything:

DisplayInformation^ displayInformation = DisplayInformation::GetForCurrentView();
displayInformation->AutoRotationPreferences = DisplayOrientations::LandscapeFlipped;

Is there any way to force the orientation of a UWP app without changing the display orientation of windows?

Upvotes: 1

Views: 580

Answers (1)

Faywang - MSFT
Faywang - MSFT

Reputation: 5868

The AutoRotationPreferences api you used is only designed to work when the device is in Tablet mode,so it doesn't work in your device.You could consider setting the screen rotation using SetDisplayConfig from a Win32 app on startup and I will also do some tests for that.

Upvotes: 0

Related Questions