Jump to content

Setting Desktop Resolutions With Autoit?


 Share

Recommended Posts

Hello, I have just found out about Autoit 3 days ago, and I think it is subperb. My friend recommended it to me.

I've been playing with it and I think I've gotten further then I could in c++, and vb.. Now I'm trying to create a script that will set the screen resolution by pressing a button... But it doesn't workd :think: anyways heres the script.

#include <GUIConstants.au3>
GuiCreate("res", 394, 380,-1, -1)
$supersmall= GUICtrlCreateButton("1218x1024",50,50,200,15)
$small= GUICtrlCreateButton("1280x768",50,80,200,15)
$medium= GUICtrlCreateButton("1152x864",50,100,200,15)
$normal= GUICtrlCreateButton("1024x964",50,120,200,15)
$huge= GUICtrlCreateButton("800x600",50,140,200,15)
GUISetState()

while 1
    sleep(200)
    $msg = GUIGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        exitloop
    case $msg = $supersmall
        _1218()
    case $msg = $small
        _1280()
    case $msg = $medium
        _1152()
    case $msg = $normal
        _1024()
    case $msg = $huge
        _800()
 If $msg = $GUI_EVENT_CLOSE Then ExitLoop
 EndSelect
WEnd

func _1218()
RegRead("HKEY_CURRENT_USER","SessionInformation")
Return @DesktopWidth & @DesktopHeight
;RegWrite("HKEY_CURRENT_USER","SessionInformation")
EndFunc

func _1280()
RegRead("HKEY_CURRENT_USER","SessionInformation")
Return @DesktopWidth & @DesktopHeight
;RegWrite("HKEY_CURRENT_USER","SessionInformation")
EndFunc

func _1152()
RegRead("HKEY_CURRENT_USER","SessionInformation")
Return @DesktopWidth & @DesktopHeight
;RegWrite("HKEY_CURRENT_USER","SessionInformation")
EndFunc

func _1024()
RegRead("HKEY_CURRENT_USER","SessionInformation")
Return @DesktopWidth& @DesktopHeight
;RegWrite("HKEY_CURRENT_USER","SessionInformation")
EndFunc

func _800()
RegRead("HKEY_CURRENT_USER","SessionInformation")
Return @DesktopWidth & @DesktopHeight
;RegWrite("HKEY_CURRENT_USER","SessionInformation")
EndFunc
Link to comment
Share on other sites

Hello, I have just found out about Autoit 3 days ago, and I think it is subperb. My friend recommended it to me.

I've been playing with it and I think I've gotten further then I could in c++, and vb.. Now I'm trying to create a script that will set the screen resolution by pressing a button... But it doesn't workd :think: anyways heres the script.

CODE
#include <GUIConstants.au3>

GuiCreate("res", 394, 380,-1, -1)

$supersmall= GUICtrlCreateButton("1218x1024",50,50,200,15)

$small= GUICtrlCreateButton("1280x768",50,80,200,15)

$medium= GUICtrlCreateButton("1152x864",50,100,200,15)

$normal= GUICtrlCreateButton("1024x964",50,120,200,15)

$huge= GUICtrlCreateButton("800x600",50,140,200,15)

GUISetState()

while 1

sleep(200)

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

exitloop

case $msg = $supersmall

_1218()

case $msg = $small

_1280()

case $msg = $medium

_1152()

case $msg = $normal

_1024()

case $msg = $huge

_800()

If $msg = $GUI_EVENT_CLOSE Then ExitLoop

EndSelect

WEnd

func _1218()

RegRead("HKEY_CURRENT_USER","SessionInformation")

Return @DesktopWidth & @DesktopHeight

;RegWrite("HKEY_CURRENT_USER","SessionInformation")

EndFunc

func _1280()

RegRead("HKEY_CURRENT_USER","SessionInformation")

Return @DesktopWidth & @DesktopHeight

;RegWrite("HKEY_CURRENT_USER","SessionInformation")

EndFunc

func _1152()

RegRead("HKEY_CURRENT_USER","SessionInformation")

Return @DesktopWidth & @DesktopHeight

;RegWrite("HKEY_CURRENT_USER","SessionInformation")

EndFunc

func _1024()

RegRead("HKEY_CURRENT_USER","SessionInformation")

Return @DesktopWidth& @DesktopHeight

;RegWrite("HKEY_CURRENT_USER","SessionInformation")

EndFunc

func _800()

RegRead("HKEY_CURRENT_USER","SessionInformation")

Return @DesktopWidth & @DesktopHeight

;RegWrite("HKEY_CURRENT_USER","SessionInformation")

EndFunc

Check out thread #19324 for how to do it with an object call.

On your script, you have some things that don't look right to me (doesn't mean they're wrong, necesarily):

There's no need for the IF statement for $GUI_EVENT_CLOSE in your while loop. Just make it one more CASE, which was already there, so it's redundant anyway:

while 1
    sleep(200)
    $msg = GUIGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        exitloop
    case $msg = $supersmall
        _1218()
    case $msg = $small
        _1280()
    case $msg = $medium
        _1152()
    case $msg = $normal
        _1024()
    case $msg = $huge
        _800()
 EndSelect
WEnd

You commented out the RegWrite() functions, but the location of your Return statements prevents them from being executed anyway. The function exits at Return:

func _800()
    RegRead("HKEY_CURRENT_USER","SessionInformation")
    RegWrite("HKEY_CURRENT_USER","SessionInformation")
    Return @DesktopWidth & @DesktopHeight
EndFunc

The other problem (as you may have noticed) is that changing those reg keys doesn't change the desktop like you want, hence the link to thread #19324.

Hope that helps! :(

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • Moderators

I've been playing with it and I think I've gotten further then I could in c++, and vb.. Now I'm trying to create a script that will set the screen resolution by pressing a button... But it doesn't workd tongue.gif anyways heres the script.

This has been done before... But foremost, your going to have issues in the future if you call functions that don't do anything. I'll see if I can find the thread.

Edit:

PsaltyDS got me again :think:...

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Thank you PsaltyDS, for the most part I got it working except for the first top three bottoms..

Heres my new code.

#include <GUIConstants.au3>
$res= GuiCreate("res", 394, 380,-1, -1)
$supersmall= GUICtrlCreateButton("1218x1024",50,50,200,15)
$small= GUICtrlCreateButton("1280x768",50,80,200,15)
$medium= GUICtrlCreateButton("1152x864",50,100,200,15)
$normal= GUICtrlCreateButton("1024x964",50,120,200,15)
$huge= GUICtrlCreateButton("800x600",50,140,200,15)
$supersmalltxt= "1218x1024"
$smalltxt= "1280x768"
$mediumtxt= "1152x864"
$normaltxt= "1024x964"
$hugetxt= "800x600"
GUISetState(@SW_SHOW)



while 1
    $msg = GUIGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        exitloop
    case $msg = $supersmall
        _1218()
    case $msg = $small
        _1280()
    case $msg = $medium
        _1152()
    case $msg = $normal
        _1024()
    case $msg = $huge
        _800()
EndSelect
WEnd

func _1218()
msgbox(0,"res","setting desktop resolution to: " &$supersmalltxt)
DisplayChangeRes(1280, 1024, 32, 85)
EndFunc

func _1280()
msgbox(0,"res","setting desktop resolution to: " &$smalltxt)
DisplayChangeRes(1280, 786, 32, 85)
EndFunc

func _1152()
msgbox(0,"res","setting desktop resolution to: " &$mediumtxt)
DisplayChangeRes(1152, 864, 32, 85)
EndFunc

func _1024()
msgbox(0,"res","setting desktop resolution to: " &$normaltxt)
DisplayChangeRes(1024, 768, 32, 85)
EndFunc

func _800()
msgbox(0,"res","setting desktop resolution to: " &$hugetxt)
DisplayChangeRes(800, 600, 32, 85)
EndFunc



Func DisplayChangeRes($WIDTH, $HEIGHT, $BPP, $FREQ)
$DM_PELSWIDTH = 0x00080000
$DM_PELSHEIGHT = 0x00100000
$DM_BITSPERPEL = 0x00040000
$DM_DISPLAYFREQUENCY = 0x00400000
$CDS_TEST = 0x00000002
$CDS_UPDATEREGISTRY = 0x00000001
$DISP_CHANGE_RESTART = 1
$DISP_CHANGE_SUCCESSFUL = 0
$HWND_BROADCAST = 0xffff
$WM_DISPLAYCHANGE = 0x007E
$DEVMODE = DLLStructCreate ("byte[32];int[10];byte[32];int[6]")
$B = DllCall("user32.dll", "int", "EnumDisplaySettings", "ptr", 0, "long", 0, "ptr", DLLStructGetPtr ($DEVMODE))
If @error Then
$B = 0
Else
$B = $B[0]
EndIf
If $B <> 0 Then
DllStructSetData ($DEVMODE, 2, BitOR($DM_PELSWIDTH, $DM_PELSHEIGHT, $DM_BITSPERPEL, $DM_DISPLAYFREQUENCY), 5)
DllStructSetData ($DEVMODE, 4, $WIDTH, 2)
DllStructSetData ($DEVMODE, 4, $HEIGHT, 3)
DllStructSetData ($DEVMODE, 4, $BPP, 1)
DllStructSetData ($DEVMODE, 4, $FREQ, 5)
$B = DllCall("user32.dll", "int", "ChangeDisplaySettings", "ptr", DLLStructGetPtr ($DEVMODE), "int", $CDS_TEST)
If @error Then
$B = -1
Else
$B = $B[0]
EndIf
Select
Case $B = $DISP_CHANGE_RESTART
$DEVMODE = ""
Return 2
Case $B = $DISP_CHANGE_SUCCESSFUL
DllCall("user32.dll", "int", "ChangeDisplaySettings", "ptr", DLLStructGetPtr ($DEVMODE), "int", $CDS_UPDATEREGISTRY)
DllCall("user32.dll", "int", "SendMessage", "hwnd", $HWND_BROADCAST, "int", $WM_DISPLAYCHANGE, _
"int", $BPP, "int", $HEIGHT * 2 ^ 16 + $WIDTH)
$DEVMODE = ""
Return 1
Case Else
$DEVMODE = ""
Return $B
EndSelect
EndIf
EndFunc;==>DisplayChangeRes
Link to comment
Share on other sites

  • Moderators

They all worked for me Berny... Are you using AutoIt Beta... I tried it with the latest.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

@SmOke_N, I know, I couldn't find the setting in regedit so I just left them, and see if I cant get some help here.

@Valuater do you know why the top 3 buttons don't work?

Are you sure the modes are correct for your Video/Monitor combo? Can you set 1218x1024 at 85Hz manualy? those seem to be unusual aspect ratios. 1280x1024, 1024x768 and 800x600 are more typical, or is that for like a 16:9 wide screen? :think:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • 13 years later...

Seems like your searching for the necromancers crown here☠️👑.

#include <WinAPIGdiDC.au3>

Global Const $_tag_POINTL = "long x;long y"
Global Const $_tag_DEVMODE = "char dmDeviceName[32];ushort dmSpecVersion;ushort dmDriverVersion;short dmSize;" & _
        "ushort dmDriverExtra;dword dmFields;" & $_tag_POINTL & ";dword dmDisplayOrientation;dword dmDisplayFixedOutput;" & _
        "short dmColor;short dmDuplex;short dmYResolution;short dmTTOption;short dmCollate;" & _
        "byte dmFormName[32];ushort LogPixels;dword dmBitsPerPel;int dmPelsWidth;dword dmPelsHeight;" & _
        "dword dmDisplayFlags;dword dmDisplayFrequency"

Local Const $DM_DISPLAYFREQUENCY = 0x00400000
Local Const $DM_PELSWIDTH = 0x00080000
Local Const $DM_PELSHEIGHT = 0x00100000
Local Const $DM_BITSPERPEL = 0x00040000

Local $DEVMODE = DllStructCreate($_tag_DEVMODE)
DllStructSetData($DEVMODE, "dmSize", DllStructGetSize($DEVMODE))
DllStructSetData($DEVMODE, "dmPelsWidth", 1366)
DllStructSetData($DEVMODE, "dmPelsHeight", 768)
DllStructSetData($DEVMODE, "dmDisplayFrequency", 60)
DllStructSetData($DEVMODE, "dmBitsPerPel", 8)
DllStructSetData($DEVMODE, "dmFields", BitOR($DM_PELSWIDTH, $DM_PELSHEIGHT, $DM_DISPLAYFREQUENCY, $DM_BITSPERPEL))

Local Const $CDS_TEST = 0x00000002
Local Const $DISP_CHANGE_SUCCESSFUL = 0

Local $sDisplay = "\\.\DISPLAY1"

Local $i_Res = DllCall("user32.dll", "int", "ChangeDisplaySettingsEx", "str", $sDisplay, "ptr", DllStructGetPtr($DEVMODE), "hwnd", 0, "int", $CDS_TEST, "ptr", 0)
ConsoleWrite($i_Res[0] & @CRLF)
If $i_Res[0] = $DISP_CHANGE_SUCCESSFUL Then
    #cs
        DISP_CHANGE_SUCCESSFUL = 0
        DISP_CHANGE_RESTART = 1
        DISP_CHANGE_FAILED = -1
        DISP_CHANGE_BADMODE = -2
        DISP_CHANGE_NOTUPDATED = -3
        DISP_CHANGE_BADFLAGS = -4
        DISP_CHANGE_BADPARAM = -5
        DISP_CHANGE_BADDUALVIEW = -6
    #ce
    $i_Res = DllCall("user32.dll", "int", "ChangeDisplaySettingsEx", "str", $sDisplay, "ptr", DllStructGetPtr($DEVMODE), "hwnd", 0, "int", 0, "ptr", 0) ; dwflags = 0 > The graphics mode for the current screen will be changed dynamically.
EndIf

 

Link to comment
Share on other sites

Thanks KaFu !

I see it's more complicated than I thought.

If I want to change the resolution back to 1920 x 1080, can I just edit these two lines:

DllStructSetData($DEVMODE, "dmPelsWidth", 1366)
DllStructSetData($DEVMODE, "dmPelsHeight", 768)

or do I also need to edit these two lines:

Local Const $DM_PELSWIDTH = 0x00080000
Local Const $DM_PELSHEIGHT = 0x00100000

Thanks much.

Shmuel

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...