Link to home
Start Free TrialLog in
Avatar of the_ratzenator
the_ratzenatorFlag for United States of America

asked on

AutoIt Script to Change Screen Resolution to 1280 x 1064 but not on Systems with Higher Resolution

I have a script that I found online and it works fantastically, but I need it to not change the screen resolution on monitors that have a higher resolution configured because 1280 x 1064 is our new default setting in our enviroment. I know there is a way to do this but I have not used AutoIt in a very very long time and it has really come a long way from what I have seen and I want to know from you gurus out there if you can throw in an if then else statement or something that can tell the script not to run the screen resolution change if any resolution is higher than 1280 x 1064 that would be great. Here is the AutoIt script:
; Define screen resolution
$Width = 1280
$Height = 1024
$BitsPerPixel = 32
$RefreshRate = 60

; Define and set resolution
_ChangeScreenRes(1280,1024,32,60)

Func _ChangeScreenRes($i_Width = @DesktopWidth, $i_Height = @DesktopHeight, $i_BitsPP = @DesktopDepth, $i_RefreshRate = @DesktopRefresh)
Local Const $DM_PELSWIDTH = 0x00080000
Local Const $DM_PELSHEIGHT = 0x00100000
Local Const $DM_BITSPERPEL = 0x00040000
Local Const $DM_DISPLAYFREQUENCY = 0x00400000
Local Const $CDS_TEST = 0x00000002
Local Const $CDS_UPDATEREGISTRY = 0x00000001
Local Const $DISP_CHANGE_RESTART = 1
Local Const $DISP_CHANGE_SUCCESSFUL = 0
Local Const $HWND_BROADCAST = 0xffff
Local Const $WM_DISPLAYCHANGE = 0x007E
If $i_Width = "" Or $i_Width = -1 Then $i_Width = @DesktopWidth ; default to current setting
If $i_Height = "" Or $i_Height = -1 Then $i_Height = @DesktopHeight ; default to current setting
If $i_BitsPP = "" Or $i_BitsPP = -1 Then $i_BitsPP = @DesktopDepth ; default to current setting
If $i_RefreshRate = "" Or $i_RefreshRate = -1 Then $i_RefreshRate = @DesktopRefresh ; default to current setting
Local $DEVMODE = DllStructCreate("byte[32];int[10];byte[32];int[6]")
Local $B = DllCall("user32.dll", "int", "EnumDisplaySettings", "ptr", 0, "long", 0, "ptr", DllStructGetPtr($DEVMODE))
If @error Then
$B = 0
SetError(1)
Return $B
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, $i_Width, 2)
DllStructSetData($DEVMODE, 4, $i_Height, 3)
DllStructSetData($DEVMODE, 4, $i_BitsPP, 1)
DllStructSetData($DEVMODE, 4, $i_RefreshRate, 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", $i_BitsPP, "int", $i_Height * 2 ^ 16 + $i_Width)
$DEVMODE = ""
Return 1
Case Else
$DEVMODE = ""
SetError(1)
Return $B
EndSelect
EndIf
EndFunc ;==> _ChangeScreenRes

Open in new window

Avatar of Timothy Bryant
Timothy Bryant
Flag of United States of America image

It looks like you need to set the Width, Height, Bits in the script.

Look at line 21 - If the width is "1280,1600,1920" Then... and it will leave it alone. Just set the parameters that are ok with you, and it will only change those that are set lower, or higher than what you allow.
Avatar of the_ratzenator

ASKER

I see that, but isn't there a way to just tell it that anything over 1280 x 1064 exit?
I've requested that this question be deleted for the following reason:

No one can answer within a timely manner and therefore leads me to seek alternative avenues for my resolution.
I gave you a timely answer, the reason no one else answered was because it is correct.

Depending upon the version of AutoIt, you might be able to change the following statement:

If $i_Width > "1280" Or $i_Width = -1 Then $i_Width = @DesktopWidth ; default to current setting
If $i_Height > "1064" Or $i_Height = -1 Then $i_Height = @DesktopHeight ; default to current setting
If $i_BitsPP = "???" Or $i_BitsPP = -1 Then $i_BitsPP = @DesktopDepth ; default to current setting
If $i_RefreshRate = "???" Or $i_RefreshRate = -1 Then $i_RefreshRate = @DesktopRefresh ; default to current setting
I gave you a timely answer, the reason no one else answered was because it is correct.

Depending upon the version of AutoIt, you might be able to change the following statement:

If $i_Width > "1280" Or $i_Width = -1 Then $i_Width = @DesktopWidth ; default to current setting
If $i_Height > "1064" Or $i_Height = -1 Then $i_Height = @DesktopHeight ; default to current setting
If $i_BitsPP = "???" Or $i_BitsPP = -1 Then $i_BitsPP = @DesktopDepth ; default to current setting
If $i_RefreshRate = "???" Or $i_RefreshRate = -1 Then $i_RefreshRate = @DesktopRefresh ; default to current setting
Or even easier, replace lines 7 and 8 with this:
If $Width = @DesktopWidth And $Height = @DesktopHeight And $BitsPerPixel = @DesktopDepth And $RefreshRate = @DesktopRefresh Then
    Exit
Else
    ; Define and set resolution
    _ChangeScreenRes(1280,1024,32,60)
EndIf

Open in new window

SOLUTION
Avatar of TheGorby
TheGorby
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Eventhough I did not use your method Gorb, I will give you the points since you were the closest to my resolution.
I accepted my solution because I researched my issue on an extraneous AutoIt forum and I was provided the code that remedied my problem.