• Support
  • Articles
  • Resources
  • Products

Move Macro windows inside TeraTERM window

Moderator: boris

4 postsPage 1 of 1

IshmaelCallMe
Guru

Posts:
672
Joined: Thu Jan 26, 2006 2:28 am
by IshmaelCallMe » Fri Jul 25, 2014 5:31 pm
This is a AutoIT script that will move macro windows inside and on top of the appropriate TeraTERM window. It will only move a given window once, so you can manually move them out of the way if you wish. The only exception is if you move the TeraTERM window, in which case the macro windows will follow it around. This is useful if you have multiple TeraTERM windows and macros open. All that is needed is to exec the script as one of the first things you do in the macro.

Please post here if you find this useful.

For example:

Code: Select all

  1.  
  2.     exec '"C:\Program Files (x86)\AutoIt3\AutoIt3.exe" "C:\Program Files\TT_Scripts\Programs\tt_window_pos\tt_window_pos_V6.au3"'
  3.  


Code: Select all

  1.  
  2.  
  3. #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
  4. #AutoIt3Wrapper_Outfile=tt_window_pos.exe
  5. #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
  6. #include <Process.au3>      ;tt_window_pos by Brian Rose
  7. #include <Array.au3>
  8. #include <MsgBoxConstants.au3>
  9. #include <File.au3>
  10. #include <WinAPIProc.au3>
  11.  
  12. Opt("WinWaitDelay", 100) ;milliseconds
  13. Opt("TrayIconHide", 1) ;0=show, 1=hide tray icon
  14.  
  15. Local $tt_hwin = '', $nwins = 0, $wins[10], $cwins[10], $ncwins = 0, $hTimer = 0, $paPos[4] = [0, 0, 0, 0], $fDiff = 0, $reset_time = 3000
  16. $pid = _WinAPI_GetParentProcess(@AutoItPID)
  17. If _ProcessGetName($pid) <> 'ttpmacro.exe' Then Exit
  18. While ProcessExists($pid) ;as long as the macro process exists
  19.     Sleep(200) ;put sleep at top of loop so continueloop statement doesn't eat too much CPU time
  20.  
  21.     $ncwins = 0 ;Make sure all saved windows still exist
  22.     For $j = 1 To $nwins
  23.         If WinExists($wins[$j]) Then
  24.             $ncwins += 1
  25.             $cwins[$ncwins] = $wins[$j]
  26.         EndIf
  27.     Next
  28.     $wins = $cwins
  29.     $nwins = $ncwins
  30.  
  31.     Local $aList = WinList('[CLASS:#32770]'), $found_hwin = 0, $moved = 0
  32.     For $i = 1 To $aList[0][0]
  33.         If $aList[$i][0] <> "" And BitAND(WinGetState($aList[$i][1]), 2) And $pid = WinGetProcess($aList[$i][1]) Then ;for all macro windows - visable windows with a title.
  34.             If BitAND(WinGetState($aList[$i][1]), 16) Then ContinueLoop ;if macro window minimized
  35.             If $found_hwin = 0 Then $tt_hwin = get_tterm_hwin($pid) ;only do this once since tterm will not change for a loop
  36.             If $tt_hwin Then
  37.                 If BitAND(WinGetState($tt_hwin), 16) Then ;if teraterm window minimized
  38.                     $nwins = 0
  39.                     ContinueLoop 2
  40.                 EndIf
  41.                 $found_hwin += 1
  42.                 Local $aPos = WinGetPos($tt_hwin)
  43.                 If @error Then ExitLoop
  44.                 If _ArrayToString($aPos, @TAB, 0, 3) <> _ArrayToString($paPos, @TAB, 0, 3) Then Local $moved = 1, $nwins = 0 ;if tterm window moved, then allow macro wins to be moved again
  45.                 Local $paPos = $aPos, $mPos = WinGetPos($aList[$i][1])
  46.                 If @error Then ExitLoop
  47.                 If $mPos[0] < $aPos[0] Or $mPos[0] + $mPos[2] > $aPos[0] + $aPos[2] Or $mPos[1] < $aPos[1] Or $mPos[1] + $mPos[3] > $aPos[1] + $aPos[3] Or $moved Then
  48.                     Local $found = 0 ;only move a given window once
  49.                     For $j = 1 To $nwins
  50.                         If $aList[$i][1] = $wins[$j] Then $found += 1
  51.                     Next
  52.                     If Not $found Then
  53.                         $xp = $aPos[0] + ($aPos[2] / 2) - ($mPos[2] / 2) ;center in x
  54.                         Local $pyoffset = 0, $yoffset = $aPos[1] + $aPos[3] / 4 ;start 1/4 way down base window in y
  55.                         While $yoffset <> $pyoffset ;Find out if saved wins are on top of each other and move them if so
  56.                             $pyoffset = $yoffset
  57.                             For $j = 1 To $nwins ;look for already moved windows so don't put them on top of each other
  58.                                 $oPos = WinGetPos($wins[$j])
  59.                                 If @error Then ExitLoop 2
  60.                                 Local $try[4] = [$xp, $yoffset, $mPos[2], $mPos[3]]
  61.                                 If overlapp($oPos, $try) Then $yoffset = ($oPos[3] + $oPos[1]) ;try putting underneath existing win
  62.                             Next
  63.                         WEnd
  64.                         If WinMove($aList[$i][1], '', $xp, $yoffset) = 0 Then ExitLoop
  65.                         WinSetOnTop($aList[$i][1], '', 1)
  66.                         $nwins += 1
  67.                         $wins[$nwins] = $aList[$i][1] ;save as already moved
  68.                         WinSetOnTop($aList[$i][1], '', 0)
  69.                     EndIf
  70.                 EndIf
  71.             EndIf
  72.         EndIf
  73.     Next
  74. WEnd
  75. SplashOff()
  76. Exit
  77.  
  78. Func overlapp($a, $b) ;see if two squares (windows) overlap by testing if difference between centers in x and y is greater than average of width and height
  79.     Local $aw = ($a[2] + $b[2]) / 2, $ah = ($a[3] + $b[3]) / 2
  80.     Local $cax = $a[0] + ($a[2] / 2), $cbx = $b[0] + ($b[2] / 2)
  81.     Local $cay = $a[1] + ($a[3] / 2), $cby = $b[1] + ($b[3] / 2)
  82.     If Abs($cax - $cbx) > $aw Or Abs($cay - $cby) > $ah Then Return 0 ;if disjoint in x or y, they don't overlap
  83.     Return 1
  84. EndFunc   ;==>overlapp
  85.  
  86. Func get_tterm_hwin($pid) ;attempt to find ttermpro window as either child or parent of ttmacro
  87.     Local $tt_hwin = '', $k, $j, $ppid = _WinAPI_GetParentProcess($pid)
  88.     Local $a_children = _WinAPI_EnumChildProcess($pid) ;Ttermpro could also be a child of ttpmacro
  89.     For $k = 1 To $a_children[0][0] ;child takes precedence
  90.         If $a_children[$k][1] = 'ttermpro.exe' Then
  91.             Local $a_grandchildren = _WinAPI_EnumChildProcess($a_children[$k][0])
  92.             If IsArray($a_grandchildren) Then
  93.                 For $i = 1 To $a_grandchildren[0][0]
  94.                     If $a_grandchildren[$i][1] = 'ttpmacro.exe' Then ContinueLoop 2 ;if a grandchild is macro, this is not a valid child
  95.                 Next
  96.             EndIf
  97.             $ppid = $a_children[$k][0]
  98.             ExitLoop
  99.         EndIf
  100.     Next
  101.     If _ProcessGetName($ppid) = 'ttermpro.exe' Then
  102.         Local $ttList = WinList('[CLASS:VTWin32]') ;find TeraTERM window corresponding to $ppid
  103.         For $j = 1 To $ttList[0][0] ; Loop through the array displaying only visable windows with a title.
  104.             If $ttList[$j][0] <> "" And BitAND(WinGetState($ttList[$j][1]), 2) And $ppid = WinGetProcess($ttList[$j][1]) Then
  105.                 $tt_hwin = $ttList[$j][1]
  106.                 ExitLoop
  107.             EndIf
  108.         Next
  109.     EndIf
  110.     Return $tt_hwin
  111. EndFunc   ;==>get_tterm_hwin
  112.  
  113.  
Last edited by IshmaelCallMe on Fri Oct 10, 2014 8:43 pm, edited 3 times in total.
IshmaelCallMe
Guru

Posts:
672
Joined: Thu Jan 26, 2006 2:28 am
by IshmaelCallMe » Fri Aug 08, 2014 5:00 pm
Fixed minor bug where it would sometimes attach to wrong window. Updated first post.
IshmaelCallMe
Guru

Posts:
672
Joined: Thu Jan 26, 2006 2:28 am
by IshmaelCallMe » Fri Sep 19, 2014 3:31 pm
Updated first post again, using winapi.
IshmaelCallMe
Guru

Posts:
672
Joined: Thu Jan 26, 2006 2:28 am
by IshmaelCallMe » Fri Oct 10, 2014 8:44 pm
Updated first post again, better non-overlap handling
Display posts from previous:
Sort by:

4 postsPage 1 of 1

Users browsing this forum: No registered users