How to change screen resolution using AHK script? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
anil_robo

How to change screen resolution using AHK script?

Post by anil_robo » 05 Jan 2016, 08:28

I have a 4k monitor running Windows 10. I want to write a script that will change resolution from 3840x2160 to 1920x1080 pixels.

What did not work:

Code: Select all

; AutoHotkey Version: 1.x
; Language: English
; Platform: Win9x/NT

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.

^8::
ChangeResolution(1920,1080)
return

^9::
ChangeResolution(1024,768)
return

ChangeResolution(w,h) {
VarSetCapacity(dM,156,0)
NumPut(156,dM,36)
NumPut(0x5c0000,dM,40)
NumPut(w,dM,108)
NumPut(h,dM,112)
DllCall( "ChangeDisplaySettingsA", UInt,&dM, UInt,0 )
}
(Yes, I tried ctrl+8 and ctrl+9)

And this method also didn't work:

Code: Select all

ChangeDisplaySettings( (ClrDep:=32) , (Wid:=1920) , (Hei:=1080) , (Hz:=60) )

ChangeDisplaySettings( cD, sW, sH, rR ) {
  VarSetCapacity(dM,156,0), NumPut(156,2,&dM,36)
  DllCall( "EnumDisplaySettings", UInt,0, UInt,-1, UInt,&dM ), NumPut(0x5c0000,dM,40)
  NumPut(cD,dM,104),  NumPut(sW,dM,108),  NumPut(sH,dM,112),  NumPut(rR,dM,120)
  Return DllCall( "ChangeDisplaySettings", UInt,&dM, UInt,0 )
}

Shadowpheonix
Posts: 1260
Joined: 16 Apr 2015, 09:41

Re: How to change screen resolution using AHK script?

Post by Shadowpheonix » 05 Jan 2016, 09:46

Here is a version of ChangeResolution that works for me on my Windows 10 system...

Code: Select all

^F12::
ChangeResolution(1280, 720)
Return

!F12::
ChangeResolution(1920, 1080)
Return

ChangeResolution(Screen_Width := 1920, Screen_Height := 1080, Color_Depth := 32)
{
	VarSetCapacity(Device_Mode,156,0)
	NumPut(156,Device_Mode,36) 
	DllCall( "EnumDisplaySettingsA", UInt,0, UInt,-1, UInt,&Device_Mode )
	NumPut(0x5c0000,Device_Mode,40) 
	NumPut(Color_Depth,Device_Mode,104)
	NumPut(Screen_Width,Device_Mode,108)
	NumPut(Screen_Height,Device_Mode,112)
	Return DllCall( "ChangeDisplaySettingsA", UInt,&Device_Mode, UInt,0 )
}
Return

anil_robo
Posts: 11
Joined: 05 Jan 2016, 08:30

Re: How to change screen resolution using AHK script?

Post by anil_robo » 09 Jan 2016, 02:21

Wow it works! Thanks a lot!

garry
Posts: 3852
Joined: 22 Dec 2013, 12:50

Re: How to change screen resolution using AHK script?

Post by garry » 09 Jan 2016, 05:41

@ Shadowpheonix , thank you , works also with XP

Shadowpheonix
Posts: 1260
Joined: 16 Apr 2015, 09:41

Re: How to change screen resolution using AHK script?

Post by Shadowpheonix » 09 Jan 2016, 14:08

I should clarify that I did not write that code. I found it on the old forums. Unfortunately, I am unable to find the post now, so I cannot properly credit the original author.

doubleplusthink

Re: How to change screen resolution using AHK script?

Post by doubleplusthink » 17 Nov 2017, 13:59

Does anyone know how to get this to work for two screens?

THX1138
Posts: 63
Joined: 25 Jul 2017, 00:36

Re: How to change screen resolution using AHK script?

Post by THX1138 » 05 Dec 2017, 14:37

I also would like a solution for a multiple-monitor system.

BoBo
Posts: 6563
Joined: 13 May 2014, 17:15

Re: How to change screen resolution using AHK script?  Topic is solved

Post by BoBo » 05 Dec 2017, 15:55

Shadowpheonix wrote:I should clarify that I did not write that code. I found it on the old forums. Unfortunately, I am unable to find the post now, so I cannot properly credit the original author.
https://autohotkey.com/board/topic/2073 ... ion/page-2

psz
Posts: 1
Joined: 16 Mar 2024, 15:10
Contact:

Re: How to change screen resolution using AHK script?

Post by psz » 16 Mar 2024, 15:15

Shadowpheonix wrote:
05 Jan 2016, 09:46

Code: Select all

ChangeResolution(Screen_Width := 1920, Screen_Height := 1080, Color_Depth := 32)
{
	VarSetCapacity(Device_Mode,156,0)
	NumPut(156,Device_Mode,36) 
	DllCall( "EnumDisplaySettingsA", UInt,0, UInt,-1, UInt,&Device_Mode )
	NumPut(0x5c0000,Device_Mode,40) 
	NumPut(Color_Depth,Device_Mode,104)
	NumPut(Screen_Width,Device_Mode,108)
	NumPut(Screen_Height,Device_Mode,112)
	Return DllCall( "ChangeDisplaySettingsA", UInt,&Device_Mode, UInt,0 )
}
Return
This worked great for a long time, however recently, this started showing my screen at the native resolution with a black matte around the smaller resolution that I want. Any ideas why this won't fill the screen?

When I change resolution in Windows display settings, it fills the screen as expected.

Post Reply

Return to “Ask for Help (v1)”