Resolution change on specific program start?

Get help with using AutoHotkey and its commands and hotkeys
zariya
Posts: 8
Joined: 23 Feb 2016, 16:20

Resolution change on specific program start?

25 Feb 2016, 16:11

Hello, i would like to ask you guys if its possible to create a script that changes my resolution if i start a specific program and then reset it back to normal if the program closes?

best regards
wizardzedd
Posts: 319
Joined: 23 Jan 2016, 23:03

Re: Resolution change on specific program start?

25 Feb 2016, 16:49

Hello, yes it is possible.
Disclaimer: half of this code was written at https://autohotkey.com/board/topic/7646 ... ode/page-2

Code: Select all

SetTitleMatchMode, 2  ; partial titles
WinWaitActive, Notepad
ChangeDisplaySettings(32, 1024,768, 60)
WinWaitClose, Notepad
ChangeDisplaySettings("Restore", 0, 0, 0)
return


ChangeDisplaySettings( cD, sW, sH, rR ) {
	static settings:={}
  VarSetCapacity(dM,156,0), NumPut(156,2,&dM,36)
  DllCall( "EnumDisplaySettings", UInt,0, UInt,-1, UInt,&dM ) ,  NumPut(0x5c0000,dM,40)
    if(cD = "Restore") { 
    		if(settings["cD"] = "")
 	   		return
		NumPut(settings["cD"],dM,104),  NumPut(settings["sW"],dM,108),  NumPut(settings["sH"],dM,112),  NumPut(settings["rR"],dM,120)		
	} else {
		settings["cD"]:=NumGet(DM,104), settings["sW"]:=NumGet(DM,108), settings["sH"]:=NumGet(DM,112) , settings["rR"]:=NumGet(DM,120) 
		NumPut(cD,dM,104),  NumPut(sW,dM,108),  NumPut(sH,dM,112),  NumPut(rR,dM,120)	
	}
  Return DllCall( "ChangeDisplaySettings", UInt,&dM, UInt,0 )
}
zariya
Posts: 8
Joined: 23 Feb 2016, 16:20

Re: Resolution change on specific program start?

25 Feb 2016, 18:34

wizardzedd wrote:Hello, yes it is possible.
Disclaimer: half of this code was written at https://autohotkey.com/board/topic/7646 ... ode/page-2

Code: Select all

SetTitleMatchMode, 2  ; partial titles
WinWaitActive, Notepad
ChangeDisplaySettings(32, 1024,768, 60)
WinWaitClose, Notepad
ChangeDisplaySettings("Restore", 0, 0, 0)
return


ChangeDisplaySettings( cD, sW, sH, rR ) {
	static settings:={}
  VarSetCapacity(dM,156,0), NumPut(156,2,&dM,36)
  DllCall( "EnumDisplaySettings", UInt,0, UInt,-1, UInt,&dM ) ,  NumPut(0x5c0000,dM,40)
    if(cD = "Restore") { 
    		if(settings["cD"] = "")
 	   		return
		NumPut(settings["cD"],dM,104),  NumPut(settings["sW"],dM,108),  NumPut(settings["sH"],dM,112),  NumPut(settings["rR"],dM,120)		
	} else {
		settings["cD"]:=NumGet(DM,104), settings["sW"]:=NumGet(DM,108), settings["sH"]:=NumGet(DM,112) , settings["rR"]:=NumGet(DM,120) 
		NumPut(cD,dM,104),  NumPut(sW,dM,108),  NumPut(sH,dM,112),  NumPut(rR,dM,120)	
	}
  Return DllCall( "ChangeDisplaySettings", UInt,&dM, UInt,0 )
}
Thanks, but if i start Notepad, it seems like the script crashes or closes itself?
zariya
Posts: 8
Joined: 23 Feb 2016, 16:20

Re: Resolution change on specific program start?

25 Feb 2016, 18:39

It also happens if i use this this one:
Wich is almost the same.

Code: Select all

^space::
  if (x:=!x) 
    ChangeDisplaySettings( (bpp:=32), (breite:=1680), (hoehe:=1050), (hZ:=60) ) 
  else 
    ChangeDisplaySettings( (bpp:=32), (breite:=1024), (hoehe:=768), (hZ:=60) ) 
Return 

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: 1259
Joined: 16 Apr 2015, 09:41

Re: Resolution change on specific program start?

26 Feb 2016, 16:14

I get the same issue with that code. A quick search of the forums reveals that Windows 7 and later work better if you change DllCall( "EnumDisplaySettings" to DllCall ("EnumDisplaySettingsA" and Return DllCall( "ChangeDisplaySettings" to Return DllCall( "ChangeDisplaySettingsA". I made those changes to the code and the script then worked perfectly for me.
zariya
Posts: 8
Joined: 23 Feb 2016, 16:20

Re: Resolution change on specific program start?

26 Feb 2016, 17:10

Shadowpheonix wrote:I get the same issue with that code. A quick search of the forums reveals that Windows 7 and later work better if you change DllCall( "EnumDisplaySettings" to DllCall ("EnumDisplaySettingsA" and Return DllCall( "ChangeDisplaySettings" to Return DllCall( "ChangeDisplaySettingsA". I made those changes to the code and the script then worked perfectly for me.
Thanks for your help, its working fine now.

best regards
zariya
Posts: 8
Joined: 23 Feb 2016, 16:20

Re: Resolution change on specific program start?

26 Feb 2016, 17:52

I got one more question about the WinWaitActive or IfWinActive. Lets say i use WinWaitActive, Notepad the problem is if i open a tab in Firefox where the word Notepad is in, the script activates. Is there a way to prevent this or use the process of the real window that i want?

best regards
wizardzedd
Posts: 319
Joined: 23 Jan 2016, 23:03

Re: Resolution change on specific program start?

26 Feb 2016, 18:02

You can use WinWaitActive, ahk_class Notepad In reality I probably should have used that to begin with. Also thanks ShadowPhoenix for the enlightenment, I was unaware of that bug :)
zariya
Posts: 8
Joined: 23 Feb 2016, 16:20

Re: Resolution change on specific program start?

26 Feb 2016, 18:42

Thank you both for your help.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], ipoyt, omar and 75 guests