• Support
  • Articles
  • Resources
  • Products

Windows resizer

Moderator: boris

1 postPage 1 of 1

IshmaelCallMe
Guru

Posts:
670
Joined: Thu Jan 26, 2006 2:28 am
by IshmaelCallMe » Wed Aug 27, 2014 5:00 pm
This is another AutoIt script that I use with TeraTERM all the time. You can store all window positions and sizes into a file and restore them at any time. This can be useful if you want the same windows to be in the same positions and sizes after a reboot, for example.

You can also move all existing windows (even ones off screen) to the main monitor (which can be useful if you undock a laptop for instance).

Code: Select all

  1.  
  2. #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
  3. #AutoIt3Wrapper_Icon=shell32_page_1473.ico
  4. #AutoIt3Wrapper_Outfile=WinPoSize.exe
  5. #AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
  6. #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
  7. #include <ButtonConstants.au3>
  8. #include <GUIConstantsEx.au3>
  9. #include <StaticConstants.au3>
  10. #include <WindowsConstants.au3>
  11. #include <WinAPI.au3>
  12. #include <EditConstants.au3>
  13.  
  14. #Region ### START Koda GUI section ### Form=c:\program files\tt_scripts\programs\winposize\winposize_v2.0.kxf
  15. $Form1_1 = GUICreate("WinPoSize V2.0 by Brian Rose", 683, 170, 192, 124)
  16. $Button1 = GUICtrlCreateButton("Save Windows Sizes and Positions", 8, 56, 209, 73)
  17. $Button2 = GUICtrlCreateButton("Restore Windows Sizes and Positions", 440, 56, 225, 73)
  18. $Checkbox1 = GUICtrlCreateCheckbox("Restore minimized windows", 448, 136, 161, 17)
  19. GUICtrlSetState(-1, $GUI_CHECKED)
  20. $Button3 = GUICtrlCreateButton("Move all windows to Main Monitor", 224, 56, 209, 73)
  21. $Input1 = GUICtrlCreateInput("", 8, 16, 513, 21)
  22. $Browse = GUICtrlCreateButton("Browse", 520, 16, 65, 25)
  23. GUISetState(@SW_SHOW)
  24. #EndRegion ### END Koda GUI section ###
  25.  
  26. Opt("TrayIconDebug", 1) ;0=no info, 1=debug line info
  27. Opt("WinTitleMatchMode", 3) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase
  28. Opt("WinWaitDelay", 10) ;milliseconds
  29.  
  30. If $CmdLine[0] Then
  31.     If $CmdLine[1] == "--uninstall" Then ;uninstall the program
  32.         Local $tmp = MsgBox(1, 'Uninstalling WinPoSize', 'Do you wish to Continue?')
  33.         If $tmp = 2 Then Exit ;cancel was pressed
  34.         FileRecycle(@ProgramsCommonDir & '\WinPoSize')
  35.         FileRecycle(@AppDataDir & '\Microsoft\Internet Explorer\Quick Launch\WinPoSize.lnk')
  36.         FileRecycle(@StartupCommonDir & '\WinPoSize.lnk')
  37.         Exit
  38.     EndIf
  39.     If FileExists($CmdLine[1]) Then
  40.         restore_wins($CmdLine[1])
  41.         Exit
  42.     EndIf
  43. EndIf
  44.  
  45. GUICtrlSetData($Input1, RegRead("HKEY_CURRENT_USER\Software\WinPoSize", "File"))
  46. While 1
  47.     $nMsg = GUIGetMsg()
  48.     Switch $nMsg
  49.         Case $GUI_EVENT_CLOSE
  50.             Exit
  51.         Case $Browse
  52.             $filename = FileSaveDialog("Choose file to Save and Restore windows positions and sizes", @WorkingDir, "Ini files (*.ini)|All (*.*)", '', "winposize.ini")
  53.             GUICtrlSetData($Input1, $filename)
  54.             RegWrite("HKEY_CURRENT_USER\Software\WinPoSize", "File", "REG_SZ", $filename)
  55.         Case $Input1
  56.             RegWrite("HKEY_CURRENT_USER\Software\WinPoSize", "File", "REG_SZ", GUICtrlRead($Input1))
  57.         Case $Button1 ;will store windows in file in Z-order (topmost first) order
  58.             $filename = GUICtrlRead($Input1)
  59.             If $filename = '' Then $filename = FileSaveDialog("Choose file to save windows positions and sizes", @WorkingDir, "Ini files (*.ini)|All (*.*)", '', "winposize.ini")
  60.             If @error Then
  61.                 MsgBox(4096, "", "Error occurred, probably no INI file.")
  62.             Else
  63.                 $list = WinList() ;winlist lists in Z-order, which was NOT in the original documentation!
  64.                 For $win = 1 To $list[0][0]
  65.                     Local $state = WinGetState($list[$win][1], '')
  66.                     If (BitAND(2, $state) And StringLen($list[$win][0]) > 0 And Not StringRegExp($list[$win][0], "^[0-9]+$")) Then ;if visable and has a title thats not just a number
  67.                         Local $class = _WinAPI_GetClassName($list[$win][1])
  68.                         If Not (($class == 'Progman' And $list[$win][0] == 'Program Manager') Or _
  69.                                 ($class == 'Button' And $list[$win][0] == 'Start') Or _
  70.                                 ($class == 'AutoIt v3 GUI' And $list[$win][0] == 'ButtonBar') Or _
  71.                                 ($list[$win][1] = $Form1_1)) Then
  72.                             If (BitAND(32, $state)) Or (BitAND(16, $state)) Then WinSetState($list[$win][1], '', @SW_RESTORE) ;WinActivate($list[$win][1]);WinWaitActive($list[$win][1],'',20);winscp seems to get zero width and height - this dont fix it
  73.                             $pos = WinGetPos($list[$win][1], '') ;If $pos[2] = 0 Or $pos[3] = 0 Then;$client = WinGetClientSize($list[$win][1]);$pos[2] = $client[0];$pos[3] = $client[1];EndIf;winscp seems to get zero width and height - this dont fix it
  74.                             If (BitAND(32, $state)) Then WinSetState($list[$win][1], '', @SW_MAXIMIZE) ;if was maximized, then re-maximize
  75.                             If (BitAND(16, $state)) Then WinSetState($list[$win][1], '', @SW_MINIMIZE) ;if was minimized, then re-minimize
  76.                             IniWriteSection($filename, '"' & $list[$win][0] & '"', "x=" & $pos[0] & @LF & "y=" & $pos[1] & @LF & "w=" & $pos[2] & @LF & "h=" & $pos[3] & @LF & "class=" & $class & @LF & "state=" & $state)
  77.                         EndIf
  78.                     EndIf
  79.                 Next
  80.             EndIf
  81.             Exit
  82.         Case $Button2
  83.             $filename = GUICtrlRead($Input1)
  84.             If $filename = '' Then $filename = FileOpenDialog("Choose file to restore windows positions and sizes", @WorkingDir, "Ini files (*.ini)|All (*.*)", '', "winposize.ini")
  85.             restore_wins($filename)
  86.             Exit
  87.         Case $Button3
  88.             $splashwin = SplashTextOn('', 'Please Wait' & @LF & 'All windows are being resized and moved to the base screen', 600, 80, 16 + 32)
  89.             $list = WinList()
  90.             For $win = 0 To $list[0][0]
  91.                 Local $state = WinGetState($list[$win][1], '')
  92.                 If (BitAND(2, $state)) Then ;if visable ;this is necessary because there are some system windows that are not visible.
  93.                     ControlSetText($splashwin, Default, Default, $list[$win][0])
  94.                     If (BitAND(32, $state)) Then WinSetState($list[$win][1], '', @SW_RESTORE) ;if maximized, then restore
  95.                     If (BitAND(16, $state)) Then WinSetState($list[$win][1], '', @SW_RESTORE) ;if minimized, then restore
  96.                     $pos = WinGetPos($list[$win][1], '')
  97.                     $npos = $pos
  98.                     If $pos[2] > @DesktopWidth Then $npos[2] = @DesktopWidth ;shrink width if needed
  99.                     If $pos[3] > @DesktopHeight Then $npos[3] = @DesktopHeight ;shrink height if needed
  100.                     If $pos[0] + $npos[2] > @DesktopWidth Then $npos[0] = @DesktopWidth - $npos[2] ;move left if needed
  101.                     If $pos[0] < 0 Then $npos[0] = 0 ;move right if needed
  102.                     If $pos[1] + $npos[3] > @DesktopHeight Then $npos[1] = @DesktopHeight - $npos[3] ;move up if needed
  103.                     If $pos[1] < 0 Then $npos[1] = 0 ;move down if needed
  104.                     WinMove($list[$win][1], '', $npos[0], $npos[1], $npos[2], $npos[3])
  105.                     If (BitAND(32, $state)) Then WinSetState($list[$win][1], '', @SW_MAXIMIZE) ;if was maximized, then restore
  106.                     ;If (BitAND(16, $state)) Then WinSetState($list[$win][1], '', @SW_MINIMIZE)     ;if was minimized, then restore
  107.                 EndIf
  108.             Next
  109.             SplashOff()
  110.             Exit
  111.     EndSwitch
  112. WEnd
  113.  
  114. Func restore_wins($filename)
  115.     Local $cur = $Form1_1 ;Local $cur = _WinAPI_GetWindow($Form1_1, $GW_HWNDFIRST )
  116.     $list = IniReadSectionNames($filename)
  117.     If @error Then
  118.         MsgBox(4096, "", "Error occurred, probably no INI file.")
  119.     Else
  120.         For $i = 1 To $list[0]
  121.             $var = IniReadSection($filename, $list[$i])
  122.             If @error Then
  123.                 MsgBox(4096, "", "Error occurred, probably no INI file.")
  124.             Else
  125.                 $list[$i] = StringReplace($list[$i], '"', '') ;get rid of doublequotes in title
  126.                 Local $x = 0, $y = 0, $w = 100, $h = 100, $class = ''
  127.                 For $j = 1 To $var[0][0]
  128.                     Assign($var[$j][0], $var[$j][1], 4)
  129.                 Next
  130.                 If $class == '' Then
  131.                     $list1 = WinList($list[$i])
  132.                 Else ; if class parameter is there, do search on regular exp title plus specified class
  133.                     $list1 = WinList('[CLASS:' & $class & ';REGEXPTITLE:' & $list[$i] & ']')
  134.                 EndIf
  135.                 If @error = 0 Then
  136.                     For $win = 1 To $list1[0][0]
  137.                         If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then
  138.                             Local $state1 = WinGetState($list1[$win][1], '')
  139.                             If (BitAND(16, $state1)) Then WinSetState($list1[$win][1], '', @SW_RESTORE)
  140.                             If (BitAND(32, $state1)) Then WinSetState($list1[$win][1], '', @SW_RESTORE)
  141.                         EndIf
  142.                         _WinAPI_SetWindowPos($list1[$win][1], $cur, $x, $y, $w, $h, $SWP_NOACTIVATE + $SWP_NOCOPYBITS); + $SWP_DRAWFRAME)
  143.                         $cur = $list1[$win][1]
  144.                     Next
  145.                 EndIf
  146.             EndIf
  147.         Next
  148.     EndIf
  149. EndFunc   ;==>restore_wins
  150.  
  151.  

1 postPage 1 of 1

Users browsing this forum: No registered users
cron