How to get the screen auto-rotate's status?

拥有回忆 提交于 2019-11-28 10:33:34

Below maybe helpful if you want to change auto-rotate status:

//C++
typedef BOOL (WINAPI* SETAUTOROTATION)(BOOL bEnable);

SETAUTOROTATION SetAutoRotation = (SETAUTOROTATION)GetProcAddress(GetModuleHandle(TEXT("user32.dll")), (LPCSTR)2507);
if(SetAutoRotation != NULL)
{
  SetAutoRotation(TRUE);
}

or

//C#
[DllImport("user32.dll", EntryPoint = "#2507")]
extern static bool SetAutoRotation(bool bEnable);

SetAutoRotation(true);
翔 羽

I found the answer.

        public enum tagAR_STATE : uint
        {
            AR_ENABLED = 0x0,
            AR_DISABLED = 0x1,
            AR_SUPPRESSED = 0x2,
            AR_REMOTESESSION = 0x4,
            AR_MULTIMON = 0x8,
            AR_NOSENSOR = 0x10,
            AR_NOT_SUPPORTED = 0x20,
            AR_DOCKED = 0x40,
            AR_LAPTOP = 0x80
        }

[DllImport("user32.dll")]
public static extern bool GetAutoRotationState(ref tagAR_STATE input);

Hope that can help the other people.

This MSDN example appears to do the job, using what looks like an 'official' API call, SetDisplayAutoRotationPreferences, which is in User32.dll (not kernel.dll as the example states) and is defined in WinUser.h.

The advantage of this example over the other suggestions is that it first checks whether auto-rotation is supported and enabled first.

The registry and Windows+O hotkey work at the system level, tweaking a user setting. Applications aren't supposed to mess with it. There is an application-level way to set autorotation preferences, and once the user closes your app or switches to a different one, then their existing settings (or the other app's) take over.

MSDN has a good example of using the relevant APIs here: https://code.msdn.microsoft.com/windowsapps/Auto-Rotation-Preferences-87ae2902

If your app has only one autorotation preference that it keeps throughout its lifetime, then it is simplest to just set it in your manifest. There are a few options there that you don't get with the APIs such as supporting both landscape and landscape flipped.

Another alternative, and this is the one that seems to work consistently on my tablet. Check this registry key. You can also change the key and the device will immediately pick up the change:

Key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AutoRotation

Setting: Enable

It's a DWORD, so set to 0 to disable auto-rotate or 1 to enable auto-rotate.

Now, if only I could find a way to force the app to work in Landscape mode only!...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!