Jump to content
Sign in to follow this  
theratzenator

Set Screen Resolution to 1280 x 1064 but only if Screen Resolution is LOWER not HIGHER.

Recommended Posts

theratzenator

Hello all you beautiful AutoIt scripting gurus - LOL. I really need some help here because I have not used AutoIt before, but that is definitely going to change because it has come so far and its functionality trumps that of vbs and kixtart I feel. Anyway, here is a script that I found and it works absolutely fantastic, however, I need just a tad little tweak to tell it to check if the current screen resolution is higher than 1280 x 1064, if it is, then exit, but if the screen resolution is lower than 1280 x 1064, then run the script. Here is the code that works great that needs to be modified and thank you all in advance, I really do appreciate you:

==========================================================================================================

; Define screen resolution

$Width = 1280

$Height = 1064

$BitsPerPixel = 32

$RefreshRate = 60

; Define and set resolution

_ChangeScreenRes(1024,768,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_Height > "" Then Exit

ElseIf

$i_Width = "" Or $i_Width = -1 Then $i_Width = @DesktopWidth ; default to current setting

EndIf

ElseIf

$i_Height = "" Or $i_Height = -1 Then $i_Height = @DesktopHeight ; default to current setting

EndIf

ElseIf

$i_BitsPP = "" Or $i_BitsPP = -1 Then $i_BitsPP = @DesktopDepth ; default to current setting

Endif

ElseIf

$i_RefreshRate = "" Or $i_RefreshRate = -1 Then $i_RefreshRate = @DesktopRefresh ; default to current setting

EndIf

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

  • Like 1

Share this post


Link to post
Share on other sites
theratzenator

Yeah, I did that already. Here is what I did to check the current screen resolution:

=============================================================================================

If $i_Width > @DesktopWidth Or $i_Height > @DesktopHeight Then Exit

Elseif

$i_Width = "" Or $i_Width = -1 Then $i_Width = @DesktopWidth ; default to current setting

Else

$i_Height = "" Or $i_Height = -1 Then $i_Height = @DesktopHeight ; default to current setting

Else

$i_BitsPP = "" Or $i_BitsPP = -1 Then $i_BitsPP = @DesktopDepth ; default to current setting

Else

$i_RefreshRate = "" Or $i_RefreshRate = -1 Then $i_RefreshRate = @DesktopRefresh ; default to current setting

EndIf

===========================================================================

However, when I run the script it says: "Else" statmenet with no matching "If" statment. The error begins with the Elseif statement. What am I missing here bro? Thanks again.

Share this post


Link to post
Share on other sites
BrewManNH

You have a single line If Then statement, you can't then add elseifs and elses to that. Plus, you can only have one Else statement in a multi-line If...Endif statement, they all have to be ElseIf before the last Else. Also, your ElseIf statement is poorly formatted, the comparison has to be on the same line as the word ElseIf, and the code you want to run if that comparison is true has to be on the next line below that.

If $i_Width > @DesktopWidth Or $i_Height > @DesktopHeight Then
 Exit
ElseIf $i_Width = "" Or $i_Width = -1 Then
 $i_Width = @DesktopWidth ; default to current setting
ElseIf $i_Height = "" Or $i_Height = -1 Then
 $i_Height = @DesktopHeight ; default to current setting
ElseIf $i_BitsPP = "" Or $i_BitsPP = -1 Then
 $i_BitsPP = @DesktopDepth ; default to current setting
ElseIf $i_RefreshRate = "" Or $i_RefreshRate = -1 Then
 $i_RefreshRate = @DesktopRefresh ; default to current setting
EndIf

That's how it SHOULD be written.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
theratzenator

Great!! Thanks alot BrewManNH. I really appreciate your help. I will test this out first thing and I will let you know how it works out. Also, I like how you instruct as you provide support, it helps people like me who are new to AutoIt to have a better way of conceptualizing the logic behind it. Thanks again.

Share this post


Link to post
Share on other sites
BrewManNH

If you have questions, you know where to find us. :graduated:


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
theratzenator

awesome, you helped alot. I had to change the following line for it to do what I wanted, but i works GREAT!!!!!

If @DesktopWidth > 1280 Or @DesktopHeight > 1024 Then

Exit

Share this post


Link to post
Share on other sites
BrewManNH

You might want to change that to an AND instead of an OR, because there are resolution settings that use a width of 1280 that has a different height setting, and heights of 1024 with different widths. So, if you want to be sure you're setting the resolution correctly, you'd only want to exit if those are both set to the values you have there


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
theratzenator

Damn, you're right about that. I will do that first thing. I should have caught that. Thanks again BrewManNH.

Share this post


Link to post
Share on other sites
theratzenator

Okay, I just checked and the CTO prefers it to be an OR expression because the most important setting for the application that is requiring this configuration is the desktop top height setting of 1280. So we are all good to go. Thank you very very much BrewManNH. Hopefully, I can friend you on here or something for future requests that I may have on other scripts. Have a good one.

Share this post


Link to post
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
Sign in to follow this  

×