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,734 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

The purpose of this article is to identify concerns and considerations of changing or adopting e-mail policies to include the sending of confidential information. Most of the concerns in this article are centered about the human aspect of e-mail …
This comprehensive conference-networking guide will help you prep, practice and pack for success, reach out with purpose and confidence, capitalize on connections, and turn all those new leads into long-term connections.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now