I recently overclocked my monitor and I am trying to create a script to just click and do the work for me so whenever I want to change my refresh rate I dont have to open windows settings.
I think this works, but I thought I would post this here for any optimizations or any comments you guys that are far more experienced than me probably are going to add.
import ctypes
import time
class DEVMODE(ctypes.Structure):
_fields_ = [
("dmDeviceName", ctypes.c_wchar * 32),
("dmSpecVersion", ctypes.c_ushort),
("dmDriverVersion", ctypes.c_ushort),
("dmSize", ctypes.c_ushort),
("dmDriverExtra", ctypes.c_ushort),
("dmFields", ctypes.c_ulong),
("dmOrientation", ctypes.c_short),
("dmPaperSize", ctypes.c_short),
("dmPaperLength", ctypes.c_short),
("dmPaperWidth", ctypes.c_short),
("dmScale", ctypes.c_short),
("dmCopies", ctypes.c_short),
("dmDefaultSource", ctypes.c_short),
("dmPrintQuality", ctypes.c_short),
("dmColor", ctypes.c_short),
("dmDuplex", ctypes.c_short),
("dmYResolution", ctypes.c_short),
("dmTTOption", ctypes.c_short),
("dmCollate", ctypes.c_short),
("dmFormName", ctypes.c_wchar * 32),
("dmLogPixels", ctypes.c_ushort),
("dmBitsPerPel", ctypes.c_ulong),
("dmPelsWidth", ctypes.c_ulong),
("dmPelsHeight", ctypes.c_ulong),
("dmDisplayFlags", ctypes.c_ulong),
("dmDisplayFrequency", ctypes.c_ulong),
("dmICMMethod", ctypes.c_ulong),
("dmICMIntent", ctypes.c_ulong),
("dmMediaType", ctypes.c_ulong),
("dmDitherType", ctypes.c_ulong),
("dmReserved1", ctypes.c_ulong),
("dmReserved2", ctypes.c_ulong),
("dmPanningWidth", ctypes.c_ulong),
("dmPanningHeight", ctypes.c_ulong)
]
def change_refresh_rate(refresh_rate):
devmode = DEVMODE()
devmode.dmSize = ctypes.sizeof(DEVMODE)
if not ctypes.windll.user32.EnumDisplaySettingsW(None, 0, ctypes.byref(devmode)):
print("Error: Could not retrieve display settings.")
return
devmode.dmDisplayFrequency = refresh_rate
devmode.dmFields = 0x400000 # DM_DISPLAYFREQUENCY
if ctypes.windll.user32.ChangeDisplaySettingsW(ctypes.byref(devmode), 0) != 0:
print(f"Error: Could not change to {refresh_rate} Hz.")
else:
print(f"Changed to {refresh_rate} Hz.")
def main():
change_refresh_rate(170) # Change value to your desired
time.sleep(5)
change_refresh_rate(144) # Change value to your desired
if __name__ == "__main__":
main()