Jump to content

recursive function not returning


Recommended Posts

I recently got a second monitor for my computer and wrote this program to move windows to one side or the other if they are split across two screens. It uses a recursive function to move the window either to the right or the left.

The problem is when a window exists that apparently has no title, text, class, process, or state, but still manages to get past line 27 AND past lines 201 and 122, where the function should return if there is any error. When I run this script, I get this output:

Error: could not complete WinGetPos(0x000503B2)

Extended information: $hWnd = 0x000503B2

Could not complete WinGetTitle

Could not complete WinGetClassList

Could not complete WinGetProcess

Could not complete WinGetState

Could not complete WinGetText

Press {esc} to continue...

D:\Program Files\AutoIt3\source\windowautoresize_forum.au3 (209) : ==> Subscript used with non-Array variable.:

If ($iCoords[0] + $iCoords[2]) > @DesktopWidth Then

If ($iCoords^ ERROR

->14:30:03 AutoIT3.exe ended.rc:1

This error pops up when you drag a file between windows; GameRanger also randomly causes it to crash.

My question is this: why is the function not returning on line 201 or line 122?

Source:

Global $iDiff, $iCenter = @DesktopWidth / 2, $bSwitchView = False, $bIconSwitch = False
Global $bContinue = False
Opt("GUIOnEventMode", 1)
Opt("WinDetectHiddenText", 1)
TraySetPauseIcon(@WindowsDir & '\explorer.exe', -10)
If Not @Compiled Then
HotKeySet('{esc}', '_exit')
AutoItSetOption('TrayIconDebug', 1)
EndIf
AdlibRegister('_SwitchIcon', 1000)
TraySetIcon(@WindowsDir & '\explorer.exe', -12)
While True
$aList = WinList()
If @error Or Not IsArray($aList) Then
  ConsoleWrite('Error: could not complete WinList()' & @crlf)
  ConsoleWrite('Press {esc} to continue...' & @crlf)
  HotKeySet('{esc}', '_continue')
  Do
   Sleep(1000)
  Until $bContinue
  $bContinue = False
  ContinueLoop
EndIf
For $i = 1 To $aList[0][0]
  If $aList[$i][0] = "Program Manager" Then ContinueLoop
  If $aList[$i][0] <> "" And _IsVisible($aList[$i][1]) And Not _IsMaximized($aList[$i][1]) Then
   $iCoords = WinGetPos($aList[$i][1])
   If $iCoords[0] + $iCoords[2] > $iCenter And $iCoords[0] < $iCenter Then
    $iDiff = @DesktopWidth - ($iCoords[0] + $iCoords[2])
    If $iDiff < $iCoords[0] Then
     _MoveWindowLeft($aList[$i][1])
    Else
     _MoveWindowRight($aList[$i][1])
    EndIf
   EndIf
  ElseIf $aList[$i][0] <> "" And _IsMaximized($aList[$i][1]) And _IsVisible($aList[$i][1]) Then
   $bSwitchView = Not $bSwitchView
   If $bSwitchView Then
    WinSetState($aList[$i][1], '', @SW_RESTORE)
    WinMove($aList[$i][1], '', 0, 10, $iCenter, @DesktopHeight - 50)
   Else
    WinSetState($aList[$i][1], '', @SW_RESTORE)
    WinMove($aList[$i][1], '', $iCenter + 5, 10, $iCenter, @DesktopHeight - 50)
   EndIf
  EndIf
Next
Sleep(5)
WEnd
Func _continue()
HotKeySet('{esc}')
$bContinue = True
Return $bContinue
EndFunc   ;==>_continue
Func _exit()
Exit 1
EndFunc   ;==>_exit
Func _MoveWindowLeft($hWnd)
Local $iCoords, $sClassList, $sWindowTitle, $iPID, $iState, $sText, $sMsg, $sRet
$iCoords = WinGetPos($hWnd)
If @error Or Not IsArray($iCoords) Then
  ConsoleWrite('Error: could not complete WinGetPos(' & $hWnd & ')' & @crlf)
  ConsoleWrite('Extended information: $hWnd = ' & $hWnd & @crlf)
  $sWindowTitle = WinGetTitle($hWnd)
  If $sWindowTitle <> 0 Then
   ConsoleWrite('WinGetTitle(' & $hWnd & ') = ' & $sWindowTitle & @crlf)
  Else
   ConsoleWrite('Could not complete WinGetTitle' & @crlf)
  EndIf
  $sClassList = WinGetClassList($hWnd)
  If Not @error Then
   ConsoleWrite('WinGetClassList(' & $hWnd & ') = ' & $sClassList & @crlf)
  Else
   ConsoleWrite('Could not complete WinGetClassList' & @crlf)
  EndIf
  $iPID = WinGetProcess($hWnd)
  If $iPID <> -1 Then
   ConsoleWrite('WinGetProcess(' & $hWnd & ') = ' & $iPID & @crlf)
  Else
   ConsoleWrite('Could not complete WinGetProcess' & @crlf)
  EndIf
  $iState = WinGetState($hWnd)
  If Not @error Then
   $sMsg = 'States of window ' & $hWnd & ':'
   Switch True
    Case BitAND($iState, 1)
     $sMsg &= 'window exists, '
     ContinueCase
    Case BitAND($iState, 2)
     $sMsg &= 'window is visible, '
     ContinueCase
    Case BitAND($iState, 4)
     $sMsg &= 'window is enabled, '
     ContinueCase
    Case BitAND($iState, 8)
     $sMsg &= 'window is active, '
     ContinueCase
    Case BitAND($iState, 16)
     $sMsg &= 'window is minimized, '
    Case BitAND($iState, 32)
     $sMsg &= 'window is maximized, '
   EndSwitch
   If not ($sMsg = 'States of window ' & $hWnd & ':') Then
    $sMsg = StringTrimRight($sMsg, 2)
   EndIf
   ConsoleWrite($sMsg & @crlf)
  Else
   ConsoleWrite('Could not complete WinGetState' & @crlf)
  EndIf
  $sText = WinGetText($hWnd)
  If $sText <> 0 Then
   ConsoleWrite('WinGetText(' & $hWnd & ') = ' & $sText & @crlf)
  Else
   ConsoleWrite('Could not complete WinGetText' & @crlf)
  EndIf
  ConsoleWrite('Press {esc} to continue...' & @crlf)
  HotKeySet('{esc}', '_continue')
  Do
   Sleep(1000)
  Until $bContinue
  $bContinue = False
  Run(@ScriptFullPath)
  Return False ;<<<<<<<<<<<<<<<<<<<<<<<<<SHOULD RETURN HERE>>>>>>>>>>>>>>>>>>>>>>>>>
Else
  WinMove($hWnd, '', $iCoords[0] - 25, $iCoords[1])
  If $iCoords[0] + $iCoords[2] > $iCenter And $iCoords[0] < $iCenter Then
   Sleep(5)
   _MoveWindowLeft($hWnd)
  EndIf
  $iCoords = WinGetPos($hWnd)
  If $iCoords[0] < 0 Then
   WinMove($hWnd, '', 5, $iCoords[1], $iCenter - 10)
  EndIf
EndIf
Return True
EndFunc   ;==>_MoveWindowLeft
Func _MoveWindowRight($hWnd)
Local $sClassList, $sWindowTitle, $iPID, $iState, $sText, $sMsg, $sRet
$iCoords = WinGetPos($hWnd)
If @error Or Not IsArray($iCoords) Then
  ConsoleWrite('Error: could not complete WinGetPos(' & $hWnd & ')' & @crlf)
  ConsoleWrite('Extended information: $hWnd = ' & $hWnd & @crlf)
  $sWindowTitle = WinGetTitle($hWnd)
  If $sWindowTitle <> 0 Then
   ConsoleWrite('WinGetTitle(' & $hWnd & ') = ' & $sWindowTitle & @crlf)
  Else
   ConsoleWrite('Could not complete WinGetTitle' & @crlf)
  EndIf
  $sClassList = WinGetClassList($hWnd)
  If Not @error Then
   ConsoleWrite('WinGetClassList(' & $hWnd & ') = ' & $sClassList & @crlf)
  Else
   ConsoleWrite('Could not complete WinGetClassList' & @crlf)
  EndIf
  $iPID = WinGetProcess($hWnd)
  If $iPID <> -1 Then
   ConsoleWrite('WinGetProcess(' & $hWnd & ') = ' & $iPID & @crlf)
  Else
   ConsoleWrite('Could not complete WinGetProcess' & @crlf)
  EndIf
  $iState = WinGetState($hWnd)
  If Not @error Then
   $sMsg = 'States of window ' & $hWnd & ':'
   Switch True
    Case BitAND($iState, 1)
     $sMsg &= 'window exists, '
     ContinueCase
    Case BitAND($iState, 2)
     $sMsg &= 'window is visible, '
     ContinueCase
    Case BitAND($iState, 4)
     $sMsg &= 'window is enabled, '
     ContinueCase
    Case BitAND($iState, 8)
     $sMsg &= 'window is active, '
     ContinueCase
    Case BitAND($iState, 16)
     $sMsg &= 'window is minimized, '
    Case BitAND($iState, 32)
     $sMsg &= 'window is maximized, '
   EndSwitch
   If not ($sMsg = 'States of window ' & $hWnd & ':') Then
    $sMsg = StringTrimRight($sMsg, 2)
   EndIf
   ConsoleWrite($sMsg & @crlf)
  Else
   ConsoleWrite('Could not complete WinGetState' & @crlf)
  EndIf
  $sText = WinGetText($hWnd)
  If $sText <> 0 Then
   ConsoleWrite('WinGetText(' & $hWnd & ') = ' & $sText & @crlf)
  Else
   ConsoleWrite('Could not complete WinGetText' & @crlf)
  EndIf
  ConsoleWrite('Press {esc} to continue...' & @crlf)
  HotKeySet('{esc}', '_continue')
  Do
   Sleep(1000)
  Until $bContinue
  $bContinue = False
  Run(@ScriptFullPath)
  Return False ;<<<<<<<<<<<<<<<<<<<<<<<<<SHOULD RETURN HERE>>>>>>>>>>>>>>>>>>>>>>>>>
Else
  WinMove($hWnd, '', $iCoords[0] + 25, $iCoords[1])
  If $iCoords[0] + $iCoords[2] > $iCenter And $iCoords[0] < $iCenter Then
   Sleep(5)
   _MoveWindowRight($hWnd)
  EndIf
  $iCoords = WinGetPos($hWnd)
  If ($iCoords[0] + $iCoords[2]) > @DesktopWidth Then
   WinMove($hWnd, '', $iCenter + 5, $iCoords[1], $iCenter - 10)
  EndIf
EndIf
Return True
EndFunc   ;==>_MoveWindowRight
Func _IsMaximized($handle)
If BitAND(WinGetState($handle), 32) Then
  Return 1
Else
  Return 0
EndIf
EndFunc   ;==>_IsMaximized
Func _IsVisible($handle)
If BitAND(WinGetState($handle), 2) Then
  Return 1
Else
  Return 0
EndIf
EndFunc   ;==>_IsVisible
Func _SwitchIcon()
$bIconSwitch = Not $bIconSwitch
If $bIconSwitch Then
  TraySetIcon(@WindowsDir & '\explorer.exe', -12)
Else
  TraySetIcon(@WindowsDir & '\explorer.exe', -13)
EndIf
EndFunc   ;==>_SwitchIcon

[font=arial, sans-serif]How is education supposed to make me feel smarter? Besides, every time I learn something new, it pushes some old stuff out of my brain. Remember when I took that home winemaking course, and I forgot how to drive?[/font][font=arial, sans-serif]<div>This is how you annoy a web developer.</span>[/font]

Link to comment
Share on other sites

You have a Do Until loop before both lines that can't ever exit because you don't have any way to change $bContinue once it gets into that loop.

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

Link to comment
Share on other sites

actually for all three do--until loops, I have the func _continue() set $bContinue to true. and _continue() is called by the hotkeyset

Thanks for looking, though

[font=arial, sans-serif]How is education supposed to make me feel smarter? Besides, every time I learn something new, it pushes some old stuff out of my brain. Remember when I took that home winemaking course, and I forgot how to drive?[/font][font=arial, sans-serif]<div>This is how you annoy a web developer.</span>[/font]

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...