Jump to content

Save Windows Position/Size Of Open Windows


Recommended Posts

I tried this attached script to save/restore all windows on the screen and their size/position...

However, it does work, except for the last window  (say 2 windows are open on the screen), always moves to the upper Left corner of the screen and resizes it very small instead of leaving it in its original position.

I'm running Windows 11 with autoit v3.3.16.1

What I am looking for is this: 

1) a script that has a function you can: 

      a) Be run to Save the Size and Position of all open windows on the desktop screen

      b) Then another function that can be run when required, to reset those windows back to their original location and size if they have moved, been resized or both.

I believe the script was done by User:  TomZ back in 2009

I also attached the windows.ini file the script creates of all open windows on the screen for saving the windows Positions/Sizes (I had only 2 windows open, the last one always moves to the upper left of the screen and resized very small. 

 

Any help on this is greatly appreciated!  Not sure what to fix in the script or if it is because of another issue running windows 11.

Save Windows Locations #2.txt windows.ini

Link to comment
Share on other sites

  • 2 weeks later...

That's how I would do it

; https://www.autoitscript.com/forum/topic/212255-save-windows-positionsize-of-open-windows/?do=findComment&comment=1536691
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <WinAPISysWin.au3>

_CallGUI()

Func _CallGUI() ; Create a GUI with two buttons: Save Setting and Restore Setting.
    GUICreate("Save windows position", 170, 90, -1, -1, -1, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
    Local $ButtonSave = GUICtrlCreateButton("Save Setting", 10, 10, 150, 30)
    GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
    Local $ButtonRestore = GUICtrlCreateButton("Restore Setting", 10, 50, 150, 30)
    GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
    GUISetState(@SW_SHOW)

    Local $nMsg

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop

            Case $ButtonSave
                StoreWins()
                ExitLoop

            Case $ButtonRestore
                RestoreWins()
                ExitLoop

        EndSwitch
    WEnd

    Exit
EndFunc   ;==>_CallGUI

Func StoreWins($file = "windows.ini") ; Store the positions and sizes of all visible windows
    Local $sPosition, $aPos
    Local $var = _WinAPI_EnumWindowsTop()
    If FileExists($file) Then FileDelete($file)
    For $i = 1 To $var[0][0]
        If IsVisible($var[$i][0]) Then
            $aPos = WinGetPos($var[$i][0])
            $sPosition = $aPos[0] & ";" & $aPos[1] & ";" & $aPos[2] & ";" & $aPos[3]
            IniWrite($file, "WindowsPosition", $var[$i][0], $sPosition)
        EndIf
    Next
EndFunc   ;==>StoreWins

Func RestoreWins($file = "windows.ini") ; Restore the positions and sizes of all windows

    Local $aArray = IniReadSection($file, "WindowsPosition")

    ; Check if an error occurred.
    If Not @error Then
        Local $aPos, $hWnd
        ; Read the positions and sizes of all windows from the INI file
        For $i = 1 To $aArray[0][0]
            $hWnd = HWnd($aArray[$i][0])
            If WinExists($hWnd) Then
                $aPos = StringSplit($aArray[$i][1], ";", 2)
                If UBound($aPos) = 4 Then WinMove($hWnd, "", $aPos[0], $aPos[1], $aPos[2], $aPos[3])
            EndIf
        Next
    EndIf
EndFunc   ;==>RestoreWins

Func IsVisible($handle) ; Check if a window is visible
    Return BitAND(WinGetState($handle), 2)
EndFunc   ;==>IsVisible

 

:welcome: to forum
how-to-post-code-on-the-forum

I know that I know nothing

Link to comment
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
 Share

  • Recently Browsing   0 members

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