Jump to content

Recommended Posts

Posted (edited)

     Hello AutoIt community!  First, let me say that :ILA2:

     So, my monitor easily rotates between portrait and landscape orientation; I move it around regularly.  Opening the screen manager and manually changing it got so tedious I decided to learn to code.  I had never coded anything before trying AutoIT and this is my second version of the script.  I'm actually quite proud of it.  It's versatile, works great as a standalone .exe, and doesn't require any fancy tweaking to use on different computers.   Tell me what you think!

$Desktop_Height = @DesktopHeight
$Desktop_Width = @DesktopWidth
   Run("control desk.cpl")
   WinWaitActive("Screen Resolution")
   ControlSend("Screen Resolution", "", "", "!o")
   If $Desktop_Height < $Desktop_Width Then
      ControlSend("Screen Resolution", "", "", "{Down}")
   ElseIf $Desktop_Height > $Desktop_Width Then
      ControlSend("Screen Resolution", "", "", "{Up}")
   EndIf
   ControlSend("Screen Resolution", "", "", "!a")
   WinWaitActive("Display Settings")
   ControlSend("Screen Resolution", "", "", "!k")
   WinWaitActive("Screen Resolution")
   ControlSend("Screen Resolution", "", "", "!{F4}")
Exit

I attached the .ico I use.

Edit: I replaced script v2 with v3

Landscape (Dell).ico

Edited by Kronitron

[post='1192322']Detect and Switch Monitor Orientation[/post]

Posted

Nice for the ones who can't use Shift Alt Arrow

  Reveal hidden contents

IUIAutomation - Topic with framework and examples

Au3Record.exe

Posted

This is possible without automating any user interface.

If this is only your second project, then you won't have been exposed yet to the monstrosity that is the Windows API (shortened to WinAPI). The WinAPI dlls give you control over nearly everything on the system, but with great power comes great big confusing documentation pages, on a site called MSDN.

With great power also comes great ability to get your computer completely FUBAR. So with that warning in place, here is the code to rotate the screen directly with the winapi. 

Hopefully this shows you just how powerful programming can be, and why it is a great skill to learn :)

#include <APIGdiConstants.au3>
#include <WinAPIGdi.au3>

Local $i = 0, $aData

$aData = _WinAPI_EnumDisplaySettings('', $ENUM_CURRENT_SETTINGS)
ConsoleWrite('Current settings: ' & $aData[0] & ' x ' & $aData[1] & ' x ' & $aData[2] & ' bit, ' & $aData[3] & ' hz' & @CRLF)

; Store the current settings
$tDM = _WinAPI_EnumDisplaySettingsEx("", $ENUM_CURRENT_SETTINGS)


; Get the settings to change
$tDM2 = _WinAPI_EnumDisplaySettingsEx("", $ENUM_CURRENT_SETTINGS)

; Flip orientation, but also switch width/height.
$tDM2.Fields = BitOR($tDM2.Fields, $DM_DISPLAYORIENTATION)
$tDM2.DisplayOrientation = $DMDO_90
$v = $tDM2.PelsWidth
$tDM2.PelsWidth = $tDM2.PelsHeight
$tDM2.PelsHeight = $v

; Change the display settings
$ret = _WinAPI_ChangeDisplaySettings($tDM2, 0)

; Wait for the user to click ok, will also timeout after 10 seconds just in case I've messed the display up.
MsgBox(0, $ret, "H" & @LF & "e" & @LF & "l" & @LF & "l" & @LF & "o", 10)

_WinAPI_ChangeDisplaySettings($tDM, 0)
MsgBox(0, @error, "Back to normal")





Func _WinAPI_ChangeDisplaySettings($tDevMode, $dwFlags)
    Local $aRet = DllCall("user32.dll", "LONG", "ChangeDisplaySettingsW", "ptr", DllStructGetPtr($tDevMode), "DWORD", $dwFlags)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aRet[0]
EndFunc   ;==>_WinAPI_ChangeDisplaySettings


; #FUNCTION# ====================================================================================================================
; Author.........: Yashied
; Modified.......: jpm, Mat
; ===============================================================================================================================
Func _WinAPI_EnumDisplaySettingsEx($sDevice, $iMode)
    Local $sTypeOfDevice = 'wstr'
    If Not StringStripWS($sDevice, $STR_STRIPLEADING + $STR_STRIPTRAILING) Then
        $sTypeOfDevice = 'ptr'
        $sDevice = 0
    EndIf

    Local $tDevMode = DllStructCreate($tagDEVMODE_DISPLAY)
    DllStructSetData($tDevMode, 'Size', DllStructGetSize($tDevMode))
    DllStructSetData($tDevMode, 'DriverExtra', 0)

    Local $aRet = DllCall('user32.dll', 'bool', 'EnumDisplaySettingsW', $sTypeOfDevice, $sDevice, 'dword', $iMode, _
            'struct*', $tDevMode)
    If @error Or Not $aRet[0] Then Return SetError(@error + 10, @extended, 0)

    Return $tDevMode
EndFunc   ;==>_WinAPI_EnumDisplaySettingsEx
Posted
  On 8/19/2014 at 12:46 PM, Mat said:

 

This is possible without automating any user interface.

If this is only your second project, then you won't have been exposed yet to the monstrosity that is the Windows API (shortened to WinAPI). The WinAPI dlls give you control over nearly everything on the system, but with great power comes great big confusing documentation pages, on a site called MSDN.

With great power also comes great ability to get your computer completely FUBAR. So with that warning in place, here is the code to rotate the screen directly with the winapi. 

Hopefully this shows you just how powerful programming can be, and why it is a great skill to learn :)

 

I love the Spiderman references lol.  Thanks for the post, it greatly expanded my view of AutoIT's capabilities. 

[post='1192322']Detect and Switch Monitor Orientation[/post]

Posted

@Mat

Very good motivational speech.

mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

  • 1 year later...
Posted

Sorry to bring this article back from the dead- does anybody know how I could modify this code to change only one monitor in a multi-monitor setup? I'm amateur-at-best at writing script and wouldn't even know where to begin.

 

Thanks for any help!

  • 1 year later...
Posted

Hope no one minds the necro. I found this really useful and decided to rework it to fit my needs. Basically, I made it use CTRL+ALT+<ArrowKey> to rotate the display. Basically exactly the same as Intel's solution, except it works on all graphics card Windows can rotate. For me personally, my laptop uses a G-Sync panel which the monitor must be hard wired to the Nvidia GPU. Because I cannot use the integrated graphics, the hotkeys for Intel's solution do not work.  That's where this script comes in. This rotates only the primary monitor (#1 in window's display settings). Also, to answer @lalder's question, if you happen to still be hanging out here (doubt it), you can swap the primary monitors temporarily to the one you want to rotate, then swap back once you rotated them.  I hope this helps anyone looking for this.

 

#include <APIGdiConstants.au3>
#include <WinAPIGdi.au3>
#include <Misc.au3>
opt("TrayIconHide",1) ;I dont like having unnecessary icons on my tray. Feel free to disable.
$wintitle="Display Rotator"
_Singleton($wintitle)
RegWrite("HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", $wintitle, "REG_SZ", @ScriptFullPath) ;AutoStart with Windows
HotKeySet("^!{up}", "_upsidedown")
HotKeySet("^!{down}", "_Down")
HotKeySet("^!{left}", "_Left")
HotKeySet("^!{right}", "_Right")
While 1
    Sleep(60000)
WEnd
Func _WinAPI_ChangeDisplaySettings($tDevMode, $dwFlags)
    Local $aRet = DllCall("user32.dll", "LONG", "ChangeDisplaySettingsW", "ptr", DllStructGetPtr($tDevMode), "DWORD", $dwFlags)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aRet[0]
EndFunc   ;==>_WinAPI_ChangeDisplaySettings
; #FUNCTION# ====================================================================================================================
; Author.........: Yashied
; Modified.......: jpm, Mat
; ===============================================================================================================================
Func _WinAPI_EnumDisplaySettingsEx($sDevice, $iMode)
    Local $sTypeOfDevice = 'wstr'
    If Not StringStripWS($sDevice, $STR_STRIPLEADING + $STR_STRIPTRAILING) Then
        $sTypeOfDevice = 'ptr'
        $sDevice = 0
    EndIf
    Local $tDevMode = DllStructCreate($tagDEVMODE_DISPLAY)
    DllStructSetData($tDevMode, 'Size', DllStructGetSize($tDevMode))
    DllStructSetData($tDevMode, 'DriverExtra', 0)
    Local $aRet = DllCall('user32.dll', 'bool', 'EnumDisplaySettingsW', $sTypeOfDevice, $sDevice, 'dword', $iMode, _
            'struct*', $tDevMode)
    If @error Or Not $aRet[0] Then Return SetError(@error + 10, @extended, 0)
    Return $tDevMode
EndFunc   ;==>_WinAPI_EnumDisplaySettingsEx
Func _Right()
    _WinAPI_ChangeDisplaySettings(_SetOrientation("Right"), 0)
EndFunc   ;==>_Right
Func _Left()
    _WinAPI_ChangeDisplaySettings(_SetOrientation("Left"), 0)
EndFunc   ;==>_Left
Func _upsidedown()
    _WinAPI_ChangeDisplaySettings(_SetOrientation("Up"), 0)
EndFunc   ;==>_upsidedown
Func _Down()
    _WinAPI_ChangeDisplaySettings(_SetOrientation("Down"), 0)
EndFunc   ;==>_Down
Func _SetOrientation($direction) ;$direction = {0 = Down, 1 = Right, 2 = Up, 3 = Left}
    Switch $direction
        Case "Left"
            $direction = 3
        Case "Right"
            $direction = 1
        Case "Up"
            $direction = 2
        Case "Down"
            $direction = 0
    EndSwitch
    $tDM2 = _WinAPI_EnumDisplaySettingsEx("", $ENUM_CURRENT_SETTINGS)
    $tDM2.Fields = BitOR($tDM2.Fields, $DM_DISPLAYORIENTATION)
    $tDM2.DisplayOrientation = $direction
    Select
        Case ($direction = 1) Or ($direction = 3)
            If $tDM2.PelsWidth > $tDM2.PelsHeight Then
                $v=$tDM2.PelsWidth
                $tDM2.PelsWidth = $tDM2.PelsHeight
                $tDM2.PelsHeight = $v
            Else
                $tDM2.PelsWidth = $tDM2.PelsWidth
                $tDM2.PelsHeight = $tDM2.PelsHeight
            EndIf
        Case ($direction = 0) Or ($direction = 2)
            If $tDM2.PelsWidth > $tDM2.PelsHeight Then
                $tDM2.PelsWidth = $tDM2.PelsWidth
                $tDM2.PelsHeight = $tDM2.PelsHeight
            Else
                $v=$tDM2.PelsWidth
                $tDM2.PelsWidth = $tDM2.PelsHeight
                $tDM2.PelsHeight = $v
            EndIf
    EndSelect
    Return $tDM2
EndFunc   ;==>_SetOrientation

Note: This writes to registry for current user to allow script to auto starts with windows. It also starts without a tray icon. Just personal preference, but you can change it as needed.

P.S. I'm curious if there is an easier or better way to do the select statement in _SetOrientation(). 

 

 

  • 4 years later...
Posted

Hi,

 

Wanted to have it work for my secondary monitor, here the updated version:

#include <WinAPIGdi.au3>
#include <Misc.au3>

opt( "TrayIconHide",1 ) ;I dont like having unnecessary icons on my tray. Feel free to disable.
$wintitle="Display Rotator"
_Singleton( $wintitle )
RegWrite( "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", $wintitle, "REG_SZ", @ScriptFullPath ) ;AutoStart with Windows
HotKeySet( "^!{up}",    "_Landscape" )
HotKeySet( "^!{down}",  "_Landscape_flipped" )
HotKeySet( "^!{left}",  "_Portrait" )
HotKeySet( "^!{right}", "_Portrait_flipped" )
Global Enum $screenOrientationLandscape, $screenOrientationPortrait, $screenOrientationLandscapeFlipped, $screenOrientationPortraitFlipped
Global Const $tagDEVMODE_DISPLAY2 = 'wchar DeviceName[32];ushort SpecVersion;ushort DriverVersion;ushort Size;ushort DriverExtra;dword Fields;' & $tagPOINT & ';dword DisplayOrientation;dword DisplayFixedOutput;short Color;short Duplex;short YResolution;short TTOption;short Collate;wchar Unused2[32];ushort LogPixels;dword BitsPerPel;dword PelsWidth;dword PelsHeight;dword DisplayFlags;dword DisplayFrequency'

While 1
   Sleep( 60000 )
WEnd

Func _Landscape()
   _ChangeDisplaySettings( $screenOrientationLandscape )
EndFunc   ;==>_Landscape

Func _Portrait()
   _ChangeDisplaySettings( $screenOrientationPortrait )
EndFunc   ;==>_Portrait

Func _Landscape_flipped()
   _ChangeDisplaySettings( $screenOrientationLandscapeFlipped )
EndFunc   ;==>_Landscape_flipped

Func _Portrait_flipped()
   _ChangeDisplaySettings( $screenOrientationPortraitFlipped )
EndFunc   ;==>_Portrait_flipped

Func _ChangeDisplaySettings( $orientation )
   Local $mousePos        = _WinAPI_GetMousePos()
   Local $hMonitor        = _WinAPI_MonitorFromPoint( $mousePos )
   Local $monitorInfo     = _WinAPI_GetMonitorInfo( $hMonitor )
   Local $deviceName      = $monitorInfo[3]

   Local $tDEVMODE        = _WinAPI_EnumDisplaySettings_v2( $deviceName, $ENUM_CURRENT_SETTINGS )
   Local $tDEVMODENew     = _SetOrientation( $tDEVMODE, $orientation )
   _WinAPI_ChangeDisplaySettingsEx( $deviceName, $tDEVMODENew )
   _SetMousePos( $hMonitor )
EndFunc   ;==>_ChangeDisplaySettings

Func _SetOrientation( $tDEVMODE, $orientation )
   $tDEVMODE.Fields             = BitOR( $tDEVMODE.Fields, $DM_DISPLAYORIENTATION )
   $tDEVMODE.DisplayOrientation = $orientation
   Select
      Case ( $orientation = 1 ) Or ( $orientation = 3 )
         If $tDEVMODE.PelsWidth  > $tDEVMODE.PelsHeight Then
            $v=$tDEVMODE.PelsWidth
            $tDEVMODE.PelsWidth  = $tDEVMODE.PelsHeight
            $tDEVMODE.PelsHeight = $v
         Else
            $tDEVMODE.PelsWidth  = $tDEVMODE.PelsWidth
            $tDEVMODE.PelsHeight = $tDEVMODE.PelsHeight
         EndIf
      Case ( $orientation = 0 ) Or ( $orientation = 2 )
         If $tDEVMODE.PelsWidth  > $tDEVMODE.PelsHeight Then
            $tDEVMODE.PelsWidth  = $tDEVMODE.PelsWidth
            $tDEVMODE.PelsHeight = $tDEVMODE.PelsHeight
         Else
            $v=$tDEVMODE.PelsWidth
            $tDEVMODE.PelsWidth  = $tDEVMODE.PelsHeight
            $tDEVMODE.PelsHeight = $v
         EndIf
   EndSelect
   Return $tDEVMODE
EndFunc   ;==>_SetOrientation

Func _SetMousePos( $hMonitor )
   Local $mousePos    = DllStructCreate( $tagPOINT )
   Local $monitorInfo = _WinAPI_GetMonitorInfo( $hMonitor )
   Local $Left        = DllStructGetData( $monitorInfo[1], 1 )
   Local $Top         = DllStructGetData( $monitorInfo[1], 2 )
   Local $Right       = DllStructGetData( $monitorInfo[1], 3 )
   Local $Bottom      = DllStructGetData( $monitorInfo[1], 4 )

   DllStructSetData($mousePos, "X", ( $Left + $Right  ) /2 )
   DllStructSetData($mousePos, "Y", ( $Top  + $Bottom ) /2 )
   _WinAPI_SetMousePos( $mousePos )
 EndFunc   ;==>_SetMousePos

Func _WinAPI_EnumDisplaySettings_v2( $sDevice, $iMode )
    Local $sTypeOfDevice = 'wstr'
    If Not StringStripWS( $sDevice, $STR_STRIPLEADING + $STR_STRIPTRAILING ) Then
        $sTypeOfDevice = 'ptr'
        $sDevice = 0
     EndIf

    Local $tDEVMODE = DllStructCreate( $tagDEVMODE_DISPLAY2 )
    DllStructSetData( $tDEVMODE, 'Size',        DllStructGetSize( $tDEVMODE ) )
    DllStructSetData( $tDEVMODE, 'DriverExtra', 0 )

    Local $aRet = DllCall( 'user32.dll', 'bool', 'EnumDisplaySettingsW', $sTypeOfDevice, $sDevice, 'dword', $iMode, 'struct*', $tDEVMODE )
    If @error Or Not $aRet[0] Then Return SetError( @error + 10, @extended, 0 )

    Return $tDEVMODE
 EndFunc   ;==>_WinAPI_EnumDisplaySettings_v2

Func _WinAPI_ChangeDisplaySettingsEx( $deviceName, $tDevMode, $dwFlags = 0 )
   Local $aRet = DllCall( "user32.dll", "LONG", "ChangeDisplaySettingsExW", "wstr", $deviceName, "ptr", DllStructGetPtr( $tDevMode ), "hwnd", 0 , "DWORD", $dwFlags, "lparam", 0 )
   If @error Then Return SetError( @error, @extended, 0 )
   Return $aRet[0]
EndFunc   ;==>_WinAPI_ChangeDisplaySettingsEx

Func _WinAPI_SetMousePos( $mousePos )
   Local $iMode = Opt("MouseCoordMode", 1)
   Local $aRet = DllCall( "user32.dll", "Bool", "SetCursorPos", "int", $mousePos.X, "int", $mousePos.Y )
   Opt("MouseCoordMode", $iMode)
   If @error Then Return SetError( @error, @extended, 0 )
   Return $aRet[0]
EndFunc   ;==>_WinAPI_SetMousePos

 

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...