public static void Win32_SetRefreshRate(uint monitorIndex, uint refreshRate) { DISPLAY_DEVICE displayDevice = new DISPLAY_DEVICE(); displayDevice.cb = (ushort)Marshal.SizeOf(displayDevice); DEVMODE_Display devMode = new DEVMODE_Display(); devMode.dmSize = (ushort)Marshal.SizeOf(devMode); ChangeDisplaySettings_Result displayResult = ChangeDisplaySettings_Result.DISP_CHANGE_SUCCESSFUL; int result = EnumDisplayDevices(null, monitorIndex, displayDevice, 0); if (result != 0) { Log.Debug("CycleRefreshRate: Current MonitorIndex : {0} and current deviceName : {1}", (int)monitorIndex, Manager.Adapters[GUIGraphicsContext.DX9Device.DeviceCaps.AdapterOrdinal].Information.DeviceName); if (displayDevice.DeviceName != Manager.Adapters[GUIGraphicsContext.DX9Device.DeviceCaps.AdapterOrdinal].Information.DeviceName) { // Analyse monitorIndex to be sure to get on the good one (some multiscreen setup can failed otherwise) monitorIndex = (uint)FindMonitorIndexForScreen(); // Try to get new displayDevice based on newest detected monitorIndex result = EnumDisplayDevices(null, monitorIndex, displayDevice, 0); Log.Debug("CycleRefreshRate: New MonitorIndex : {0} based on current deviceName : {1}", (int)monitorIndex, Manager.Adapters[GUIGraphicsContext.DX9Device.DeviceCaps.AdapterOrdinal].Information.DeviceName); } if (result != 0) { result = EnumDisplaySettingsEx(displayDevice.DeviceName, 0, devMode, 2); if (result != 0) { result = EnumDisplaySettingsEx(displayDevice.DeviceName, EnumDisplaySettings_EnumMode.ENUM_CURRENT_SETTINGS, devMode, 2); // EDS_RAWMODE = 2 if (result != 0) { // Get current Value uint Width = devMode.dmPelsWidth; uint Height = devMode.dmPelsHeight; //Log.Info("CycleRefreshRate: code result {0} enum devMode", result); devMode.dmFields = (DEVMODE_Fields.DM_BITSPERPEL | DEVMODE_Fields.DM_PELSWIDTH | DEVMODE_Fields.DM_PELSHEIGHT | DEVMODE_Fields.DM_DISPLAYFREQUENCY); devMode.dmBitsPerPel = 32; devMode.dmPelsWidth = Width; devMode.dmPelsHeight = Height; devMode.dmDisplayFrequency = refreshRate; // First set settings ChangeDisplaySettings_Result r = ChangeDisplaySettingsEx(displayDevice.DeviceName, devMode, IntPtr.Zero, (ChangeDisplaySettings_Flags .CDS_NORESET | ChangeDisplaySettings_Flags .CDS_UPDATEREGISTRY), IntPtr.Zero); if (r != displayResult) { Log.Info("CycleRefreshRate: unable to change refresh rate {0}Hz for monitor {1}", refreshRate, monitorIndex); } else { // Apply settings r = ChangeDisplaySettingsEx(null, null, IntPtr.Zero, 0, IntPtr.Zero); Log.Info("CycleRefreshRate: result {0} for refresh rate change {1}Hz", r, refreshRate); FixDwm(); } } } } } else { Log.Info("CycleRefreshRate: unable to change refresh rate {0}Hz for monitor {1}", refreshRate, monitorIndex); } }
public static void Win32_SetRefreshRate(uint monitorIndex, uint refreshRate) { RefreshRateChanger.RefreshRateChangePending = true; DISPLAY_DEVICE displayDevice = new DISPLAY_DEVICE(); displayDevice.cb = (ushort)Marshal.SizeOf(displayDevice); DEVMODE_Display devMode = new DEVMODE_Display(); devMode.dmSize = (ushort)Marshal.SizeOf(devMode); ChangeDisplaySettings_Result displayResult = ChangeDisplaySettings_Result.DISP_CHANGE_SUCCESSFUL; int result = EnumDisplayDevices(null, monitorIndex, displayDevice, 0); if (result != 0) { if (GUIGraphicsContext.DX9Device != null && !GUIGraphicsContext.DX9Device.Disposed) { Log.Debug("CycleRefreshRate: Current MonitorIndex : {0} and current deviceName : {1}", (int)monitorIndex, Manager.Adapters[GUIGraphicsContext.DX9Device.DeviceCaps.AdapterOrdinal].Information.DeviceName); if (displayDevice.DeviceName != Manager.Adapters[GUIGraphicsContext.DX9Device.DeviceCaps.AdapterOrdinal].Information.DeviceName) { // Analyse monitorIndex to be sure to get on the good one (some multiscreen setup can failed otherwise) monitorIndex = (uint)FindMonitorIndexForScreen(); // Try to get new displayDevice based on newest detected monitorIndex result = EnumDisplayDevices(null, monitorIndex, displayDevice, 0); Log.Debug("CycleRefreshRate: New MonitorIndex : {0} based on current deviceName : {1}", (int)monitorIndex, Manager.Adapters[GUIGraphicsContext.DX9Device.DeviceCaps.AdapterOrdinal].Information.DeviceName); } } if (result != 0) { result = EnumDisplaySettingsEx(displayDevice.DeviceName, 0, devMode, 2); if (result != 0) { result = EnumDisplaySettingsEx(displayDevice.DeviceName, EnumDisplaySettings_EnumMode.ENUM_CURRENT_SETTINGS, devMode, 2); // EDS_RAWMODE = 2 if (result != 0) { // Get current Value uint Width = devMode.dmPelsWidth; uint Height = devMode.dmPelsHeight; if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR && GUIGraphicsContext.ForcedRefreshRate3D) { // we are here because it's a 3D movie in SBS or TAB and needed to force a 1080p resolution when we are in 4K config // Get current Value if (GUIGraphicsContext.ForcedRR3DBackDefault) { Width = (uint)GUIGraphicsContext.ForcedRR3DWitdhBackup; Height = (uint)GUIGraphicsContext.ForcedRR3DHeightBackup; refreshRate = (uint)GUIGraphicsContext.ForcedRR3DRate; Log.Debug("CycleRefreshRate: restore backup value {0} x {1}", GUIGraphicsContext.ForcedRR3DWitdhBackup, GUIGraphicsContext.ForcedRR3DHeightBackup); } else { GUIGraphicsContext.ForcedRR3DWitdhBackup = GUIGraphicsContext.form.Width; GUIGraphicsContext.ForcedRR3DHeightBackup = GUIGraphicsContext.form.Height; Log.Debug("CycleRefreshRate: backup current value {0} x {1}", GUIGraphicsContext.ForcedRR3DWitdhBackup, GUIGraphicsContext.ForcedRR3DHeightBackup); Width = 1920; Height = 1080; Log.Debug("CycleRefreshRate: 3D used forced value {0} x {1}", Width, Height); } // needed to resize screen and GUI after resolution change GUIGraphicsContext.ForceMadVRRefresh3D = true; // needed to avoid multiple refresh rate GUIGraphicsContext.ForcedRefreshRate3DDone = true; } //Log.Info("CycleRefreshRate: code result {0} enum devMode", result); devMode.dmFields = (DEVMODE_Fields.DM_BITSPERPEL | DEVMODE_Fields.DM_PELSWIDTH | DEVMODE_Fields.DM_PELSHEIGHT | DEVMODE_Fields.DM_DISPLAYFREQUENCY); devMode.dmBitsPerPel = 32; devMode.dmPelsWidth = Width; devMode.dmPelsHeight = Height; devMode.dmDisplayFrequency = refreshRate; // First set settings ChangeDisplaySettings_Result r = ChangeDisplaySettingsEx(displayDevice.DeviceName, devMode, IntPtr.Zero, (ChangeDisplaySettings_Flags .CDS_NORESET | ChangeDisplaySettings_Flags .CDS_UPDATEREGISTRY), IntPtr.Zero); if (r != displayResult) { Log.Info("CycleRefreshRate: unable to change refresh rate {0}Hz for monitor {1} for resolution {2} x {3}", refreshRate, monitorIndex, devMode.dmPelsWidth, devMode.dmPelsHeight); } else { // Apply settings r = ChangeDisplaySettingsEx(null, null, IntPtr.Zero, 0, IntPtr.Zero); Log.Info("CycleRefreshRate: result {0} for refresh rate change {1}Hz for resolution {2} x {3}", r, refreshRate, devMode.dmPelsWidth, devMode.dmPelsHeight); FixDwm(); } } } } } else { Log.Info("CycleRefreshRate: unable to change refresh rate {0}Hz for monitor {1}", refreshRate, monitorIndex); } // Refresh rate done RefreshRateChanger.RefreshRateChangePending = false; }