如何在JavaFX8中锁定Microsoft Surface Pro的Windows10屏幕旋转
Windows 10 锁定屏幕旋转为横向的实现方案
以下是不同场景下的可行实现,和你给出的Android端固定横向代码功能完全对应:
PowerShell 脚本方案(无需编译,直接运行)
- 以管理员身份打开PowerShell,执行以下代码即可立即生效:
# 禁用系统自动旋转功能 Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\AutoRotation" -Name "Enable" -Value 0 -Type DWord # 设置显示方向为标准横向 Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\GraphicsDrivers\Configuration" -Name "Rotation" -Value 0 -Type DWord # 刷新显示配置使修改生效 RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters ,1 ,True
- 如需解除锁定,将上述代码中
Enable的值改为1后重新执行即可。
C# 代码方案(适合集成到自有应用中)
- 核心调用Win32系统API实现,参考代码如下:
using System; using System.Runtime.InteropServices; public class OrientationLocker { // 导入Win32显示设置API [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern int ChangeDisplaySettingsEx(string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd, uint dwflags, IntPtr lParam); // 显示设置参数结构体定义 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] private struct DEVMODE { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string dmDeviceName; public short dmSpecVersion; public short dmDriverVersion; public short dmSize; public short dmDriverExtra; public int dmFields; public int dmPositionX; public int dmPositionY; public int dmDisplayOrientation; // 0=横向,1=纵向,2=横向翻转,3=纵向翻转 public int dmDisplayFixedOutput; public short dmColor; public short dmDuplex; public short dmYResolution; public short dmTTOption; public short dmCollate; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string dmFormName; public short dmLogPixels; public int dmBitsPerPel; public int dmPelsWidth; public int dmPelsHeight; public int dmDisplayFlags; public int dmDisplayFrequency; public int dmICMMethod; public int dmICMIntent; public int dmMediaType; public int dmDitherType; public int dmReserved1; public int dmReserved2; public int dmPanningWidth; public int dmPanningHeight; } /// <summary> /// 锁定屏幕为横向显示,禁用自动旋转 /// </summary> public static void LockToLandscape() { // 设置显示方向为横向 DEVMODE devMode = new DEVMODE(); devMode.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE)); devMode.dmFields = 0x00000080; // 标识仅修改显示方向 devMode.dmDisplayOrientation = 0; ChangeDisplaySettingsEx(null, ref devMode, IntPtr.Zero, 0, IntPtr.Zero); // 写入注册表禁用自动旋转 Microsoft.Win32.Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AutoRotation", "Enable", 0); } }
- 应用内调用
OrientationLocker.LockToLandscape()即可完成固定,注意运行应用时需要获取管理员权限,否则修改系统配置会失败。
内容的提问来源于stack exchange,提问作者pool_dev
相关产品推荐
相关产品推荐

