Advertisement
ProtoCortex

AHK - Change Monitor Refreshrate

Mar 14th, 2023
1,277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Autohotkey 3.65 KB | Source Code | 0 0
  1. VK91::SetRefreshrateMainMonitor(120) ; SL
  2. +VK91::SetRefreshrateMainMonitor(165) ; shift+SL
  3. #VK91::CheckMonitors() ; win+SL
  4.  
  5.  
  6. SetRefreshrateMainMonitor(Refreshrate) {
  7.     SysGet, Monitor, MonitorPrimary
  8.     ; Monitor := 2
  9.     Refreshrate := Refreshrate
  10.  
  11.     SysGet, MonitorCount, MonitorCount
  12.     If (Monitor > MonitorCount OR Monitor == 0 OR Monitor < -1) {
  13.         TrayTip, AHK Monitor Change, Failed: Monitor Count incorrect, 5
  14.     }
  15.     EnumDisplayDevices(Monitor, DeviceName, StateFlags)
  16.     ChangeRefreshrate(DeviceName, Refreshrate)
  17.     return
  18. }
  19.  
  20. CheckMonitors() {
  21.     text := ""
  22.     TotalPixels := ""
  23.     PixelsPerSecond := ""
  24.     Loop {
  25.         if ! EnumDisplayDevices(A_Index, DeviceName, StateFlags)
  26.             break
  27.         if (StateFlags & 4) { ; text .= "Primary display device is " DeviceName "`n"
  28.             ReadMonitor(DeviceName, Msg, Pixels, Pps)
  29.             TotalPixels += Pixels
  30.             PixelsPerSecond += Pps
  31.             text .= "Monitor " A_Index " " Msg "`n"
  32.         }            
  33.         else if (StateFlags & 1){ ; text .= "The desktop extends onto " DeviceName "`n"
  34.             ReadMonitor(DeviceName, Msg, Pixels, Pps)
  35.             TotalPixels += Pixels
  36.             PixelsPerSecond += Pps
  37.             text .= "Monitor " A_Index " " Msg "`n"
  38.         }
  39.         else if (StateFlags & 8){ ; text .= DeviceName " is a pseudo-device`n"      
  40.         }            
  41.         else{ ; text .= DeviceName " is disabled`n"
  42.         }
  43.     }
  44.     TotalPixels := ThousandsSep(TotalPixels)
  45.     PixelsPerSecond := ThousandsSep(PixelsPerSecond)
  46.     MsgBox, 0, AHK Read Monitor Settings, %text% `n`n Total Pixels %TotalPixels% `n Pixels per Second %PixelsPerSecond%
  47.     return
  48. }
  49.    
  50. ThousandsSep(x, s=".") {
  51.     return RegExReplace(x, "\G\d+?(?=(\d{3})+(?:\D|$))", "$0" s)
  52. }
  53.  
  54. ChangeRefreshrate(MonitorName, refreshRate) {
  55.     VarSetCapacity(dM,156,0),
  56.     NumPut(156,2,&dM,36)
  57.     DllCall( "EnumDisplaySettingsA", Str, MonitorName, UInt, -1, UInt, &dM),
  58.     NumPut(0x5c0000,dM,40)
  59.     NumPut(refreshRate,dM,120)
  60.     ; Return DllCall( "ChangeDisplaySettingsA", UInt,&dM, UInt,0 )
  61.     Return DllCall( "ChangeDisplaySettingsExA", Str, MonitorName, UInt,&dM, UInt,0, UInt,0 )
  62. }
  63.  
  64. ReadMonitor(MonitorName, ByRef Msg, ByRef Pixels, ByRef Pps) {
  65.     Msg := ""
  66.     Pixels := 0
  67.     Pps := 0
  68.     VarSetCapacity(dM,156,0),
  69.     NumPut(156,2,&dM,36)
  70.     DllCall( "EnumDisplaySettingsA", Str, MonitorName, UInt, -1, UInt, &dM),
  71.     Out_1:=NumGet(&dM,108,"uint4")  ; ScreeWidth
  72.     Out_2:=NumGet(&dM,112,"uint4")  ; ScreenHeight
  73.     ; Out_3:=NumGet(&dM,104,"uint4")    ; ColorDepth
  74.     Out_4:=NumGet(&dM,120,"uint4")  ; Frequency
  75.     ; TrayTip, AHK Read Monitor Settings, %Out_1%x%Out_2% @ %Out_3%bit %Out_4%Hz, 5
  76.     ; Msg := Out_1 "x" Out_2 " @ " Out_3 "bit " Out_4 "Hz"
  77.     Msg := Out_1 "x" Out_2 " @ " Out_4 " Hz"
  78.     Pixels := Out_1 * Out_2
  79.     Pps := Pixels * Out_4
  80.     Return
  81. }
  82.  
  83. EnumDisplayDevices(Index, ByRef DeviceName, ByRef StateFlags:="", ByRef DeviceKey:="") {
  84.     VarSetCapacity(DisplayDevice, 424)
  85.     NumPut(424, DisplayDevice, 0)
  86.     StateFlags := 0     ; For consistency, clear StateFlags in case of failure.
  87.     DeviceName := ""
  88.     DeviceKey := ""
  89.    
  90.     if ! DllCall("EnumDisplayDevicesA"
  91.         , "ptr", 0
  92.         , "uint", Index-1
  93.         , "ptr", &DisplayDevice
  94.         , "uint", 0)
  95.         return false
  96.    
  97.     StateFlags := NumGet(DisplayDevice, 164)
  98.     ; DeviceName := StrGet(&DisplayDevice+4, 32, "cp0")
  99.     DeviceName := StrGet(&DisplayDevice+4, 32)
  100.     DeviceKey := StrGet(&DisplayDevice+296, 128, "cp0")
  101.     if (SubStr(DeviceKey,1,18)="\Registry\Machine\")
  102.         DeviceKey := SubStr(DeviceKey,19)
  103.     return true
  104. }
Tags: ahk
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement