Export (0) Print
Expand All
Expand Minimize

GetDisplayAutoRotationPreferences function

Retrieves the screen auto-rotation preferences for the current process.

Syntax


BOOL WINAPI GetDisplayAutoRotationPreferences(
  _Out_  ORIENTATION_PREFERENCE *pOrientation
);

Parameters

pOrientation [out]

Pointer to a location in memory that will receive the current orientation preference setting for the calling process.

Return value

TRUE if the method succeeds, otherwise FALSE.

Examples

The following example shows how an app (or full-screen game) can lock the screen rotation to landscape mode while it is in focus:

BOOL success = TRUE;

// First, check the current state of the auto-rotation system.
AR_STATE autoRotationState;
ZeroMemory(&autoRotationState, sizeof(AR_STATE));
success = GetAutoRotationState(&autoRotationState);

if ((autoRotationState & AR_NOSENSOR) || (autoRotationState & AR_NOT_SUPPORTED))
{
    // If there's no sensor or rotation is not supported, we don't need 
    // to do anything about auto-rotation.
}
else if (autoRotationState & AR_ENABLED)
{
    // There is a sensor and auto-rotation is enabled. We know that
    // auto-rotation can happen, and potentially interfere with the
    // app (or full-screen game) experience.
            
    // (optional step)
    // Get the current auto-rotation preferences for the process so you 
    // can inspect the current setting.
    ORIENTATION_PREFERENCE prefs;
    success = GetDisplayAutoRotationPreferences(&prefs);

    // Set the rotation modes your app can tolerate.
    // In this example, we lock the rotation to landscape mode.
    prefs = ORIENTATION_PREFERENCE_LANDSCAPE;
    success = SetDisplayAutoRotationPreferences(prefs);

    // (optional step)
    // Retrieve the current setting and observe that your preferences took.
    prefs = ORIENTATION_PREFERENCE_NONE;
    success = GetDisplayAutoRotationPreferences(&prefs);
}

The following example shows how to use the Windows Runtime SimpleOrientationSensor class to detect device orientation in a desktop app. This is used in the case that your full-screen desktop app (or full-screen desktop game) handles rotation on its own:


// make sure to initialize COM first
HRESULT hr = CoInitializeEx(NULL, 0);

// retrieve the factory
Microsoft::WRL::ComPtr<ABI::Windows::Devices::Sensors::ISimpleOrientationSensorStatics> spSimpleOrientationSensorStatics;
hr = ABI::Windows::Foundation::GetActivationFactory(
    Microsoft::WRL::Wrappers::HStringReference(RuntimeClass_Windows_Devices_Sensors_SimpleOrientationSensor).Get(),
    &spSimpleOrientationSensorStatics
    );  // CoInitialize must be called prior to this.

// retrieve the current SimpleOrientationSensor interface
Microsoft::WRL::ComPtr<ABI::Windows::Devices::Sensors::ISimpleOrientationSensor> sensor;
hr = spSimpleOrientationSensorStatics->GetDefault(&sensor);

if (SUCCEEDED(hr))
{
    // inspect the interface to discover changes in orientation
    ABI::Windows::Devices::Sensors::SimpleOrientation orientation;
    hr = sensor->GetCurrentOrientation(&orientation);
}
else
{
    // device has no orientation sensor.
}

Requirements

Header

WinUser.h

DLL

Kernel.dll

 

 

Community Additions

ADD
Show:
© 2014 Microsoft