Solved

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

Posted on 2011-09-27
11
4,890 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
  • 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Email signature management is something that is often overlooked in many organizations or is simply not implemented effectively. Let's take a look at what methods are available for managing this important piece of corporate branding.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 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