3

How to get the screen auto-rotate's status (disable or enable) by Regetry or ACPI in windows8?

I need to disable screen auto-rotate, and I will use winkey + O to change the screen auto-rotate control.

Does anyone have similar experiences?

翔 羽
  • 227
  • 5
  • 13

5 Answers5

5

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);
user882729
  • 104
  • 1
  • 3
  • Perfect! It is work! The keyword is "EntryPoint = "#2507"".Thanks for your help! – 翔 羽 May 29 '13 at 08:17
  • 1
    This API is not documented or supported :-( . It will not pass any kind of app certification, Win32 or Store. MSFT reserves the right to remove or change it at any time. – zhuman - MSFT Mar 23 '17 at 21:59
1

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.

Botz3000
  • 39,020
  • 8
  • 103
  • 127
翔 羽
  • 227
  • 5
  • 13
1

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.

Nick Shaw
  • 2,083
  • 19
  • 27
  • SetDisplayAutoRotationPreferences Sets the screen auto-rotation preferences for the current process. That is for "current process", not for system. Thanks for your help!! – 翔 羽 Jan 29 '15 at 02:17
  • 1
    Having tested it out a lot now, it appears this setting doesn't work very well. In my app, it doesn't work at all, to be precise. So I've switched to using the registry key instead, which works every time (but is system-wide) – Nick Shaw Jan 30 '15 at 10:08
1

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.

Katie
  • 1,260
  • 10
  • 20
  • Thanks for the info, Katie. I had problems getting the AutoRotationPreferences API call to actually get/set the correct setting. It worked *most* of the time, but sometimes for no apparent reason it failed. Hence why I've gone for the more reliable, yet more system-wide, registry setting. In my app, the app is the only thing the user should ever be accessing, so it's not a problem being a system-wide change. – Nick Shaw Sep 28 '15 at 08:25
0

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!...

Nick Shaw
  • 2,083
  • 19
  • 27
  • Commenting to ping you - I just posted an answer about how to set your app's preferred orientation without messing with the user's settings. – Katie Sep 25 '15 at 16:13