Solved

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

Posted on 2011-09-27
11
5,151 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:the_ratzenator
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
11 Comments
 
LVL 3

Expert Comment

by:RushB
ID: 36713811
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.
0
 

Author Comment

by:the_ratzenator
ID: 36714044
I see that, but isn't there a way to just tell it that anything over 1280 x 1064 exit?
0
 

Author Comment

by:the_ratzenator
ID: 36739731
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.
0
Webinar: Choosing a MySQL HA Solution

Join Percona’s Principal Technical Services Engineer, Marcos Albe as he presents Choosing a MySQL High Availability Solution on Thursday, June 29, 2017 at 10:00 am PDT / 2:00 pm EDT (UTC-7).

 
LVL 3

Expert Comment

by:RushB
ID: 36739426
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
0
 
LVL 3

Expert Comment

by:RushB
ID: 36739734
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
0
 
LVL 11

Expert Comment

by:TheGorby
ID: 36817857
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

0
 
LVL 11

Assisted Solution

by:TheGorby
TheGorby earned 500 total points
ID: 36896555
Actually now that I look at it, I goofed a bit. It should be 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

0
 

Accepted Solution

by:
the_ratzenator earned 0 total points
ID: 36932284
I just used this:

If @DesktopWidth > 1280 Or @DesktopHeight > 1024 Then
 Exit

And it works. I got my answer from a FREE AutoIt forum. Thank you for your responses, I appreciate it.
0
 

Author Comment

by:the_ratzenator
ID: 36932301
Eventhough I did not use your method Gorb, I will give you the points since you were the closest to my resolution.
0
 

Author Closing Comment

by:the_ratzenator
ID: 36954289
I accepted my solution because I researched my issue on an extraneous AutoIt forum and I was provided the code that remedied my problem.
0

Featured Post

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IT certifications are a concrete representation of continual learning on the part of the candidate.  Continual learning is necessary for the long term success of an IT professional, but are IT certifications the right path for you?
Invest in your employees with these five simple steps to improve employee engagement and retention.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question