2014-05-15 92 views
0

好的,所以我創建了一個AutoIt GUI可執行文件來自動化網站中的一些功能。但是,我無法找到一種方法來打破執行我創建的函數的While循環,該函數使用我創建的「STOP」按鈕創建。因此,它無限期地運行,我不能退出程序。理論上,它應該有效,但我覺得我錯過了一些東西。任何幫助,鄉親們?AutoIt - 用GUICtrlCreateButton打開while循環

#include <GUIConstantsEx.au3> 
#include <WindowsConstants.au3> 
#include <IE.au3> 
Global $msg, $instructions, $AutoLevel, $NumDogsBox, $NumDogs, $IE, $i, $o 

$IE = _IECreateEmbedded() 
GUICreate("Automatic Dog Feeder for Criminal Case", 900, 690) 

$instructions = GUICtrlCreateButton('  Instructions  ', 725, 5) 
GUICtrlCreateLabel("# of Dogs: ",740,40) 
$NumDogsBox = GUICtrlCreateInput("", 815, 37, 25) 
$AutoLevel = GUICtrlCreateButton(' Begin Auto Feed Dog ', 700, 65) 

GUICtrlCreateObj($IE, 1, 100, 915, 610) 

GUISetState(@SW_SHOW) 

_IENavigate($IE, "http://apps.facebook.com/criminalcase") 
$IE.Document.Body.Scroll = "no" 
_IEAction($IE, "stop") 

While 1 
$msg = GUIGetMsg() 
Select 
    Case $msg = $GUI_EVENT_CLOSE 
    ExitLoop 
    Case $msg = $instructions 
    MsgBox(0,"Instructions","1.) Log in to Facebook (if not already) in the window below" & @CRLF & "2.) Clear any notifications and get to the map screen" & @CRLF & "3.) Click on the dog bowl icon on the right of the map" & @CRLF & "4.) Enter the number of dogs you have to level up" & @CRLF & "5.) Click the Begin Auto Feed Dog button at the bottom of the window") 
    Case $msg = $AutoLevel 
     $NumDogs = GUICtrlRead($NumDogsBox) 
     AutoDogLevel() 
EndSelect 
WEnd 

Func AutoDogLevel() 
    While 1 
     $stop = GUICtrlCreateButton('STOP',830,65) 
     $i=0 
     While $i < $NumDogs 
      If $msg = $stop Then 
       ExitLoop 
      Endif 
      MsgBox(0,"","1xp" & $i+1) 
      ControlClick("[CLASS:MacromediaFlashPlayerActiveX]","","","left",1,156,651) 
      Sleep(2000) 
      If $msg = $stop Then 
       ExitLoop 
      Endif 
      ControlClick("[CLASS:MacromediaFlashPlayerActiveX]","","","left",1,723,524) 
      MsgBox(0,"","Next Dog") 
      Sleep(2000) 
      $i = $i+1 
     WEnd 
    If $msg = $stop Then 
     ExitLoop 
    Endif 
    While $i > 0 
     If $msg = $stop Then 
      ExitLoop 
     Endif 
     ControlClick("[CLASS:MacromediaFlashPlayerActiveX]","","","left",1,62,525) 
     MsgBox(0,"","Previous Dog") 
     If $msg = $stop Then 
      ExitLoop 
     Endif 
     Sleep(1000) 
     $i=$i-1 
    WEnd 
    If $msg = $stop Then 
     ExitLoop 
    Endif 
    Sleep(3000) 
WEnd 
EndFunc 

所以,現在我卡住了。任何幫助將不勝感激。

回答

1

對於GUI有超過1個消息循環並不是一個好主意。不要這樣做,我建議爲當前狀態保留一個變量,並且只實現一個循環。消息循環中還有一些非常大的睡眠。

我還沒有測試代碼,但這裏是重寫的消息循環使用狀態變量和定時器而不是多個循環和睡眠。這是一個更好的設計,可以確保您的GUI始終能夠響應所有按鈕,而且我認爲一旦您習慣了結構,您會發現它更容易使用。

Local $iState = 0, $i, $iTimer 
While 1 
    $msg = GUIGetMsg() 
    Select 
     Case $msg = $GUI_EVENT_CLOSE 
      ExitLoop 
     Case $msg = $instructions 
      MsgBox(0, "Instructions", "1.) Log in to Facebook (if not already) in the window below" & @CRLF & "2.) Clear any notifications and get to the map screen" & @CRLF & "3.) Click on the dog bowl icon on the right of the map" & @CRLF & "4.) Enter the number of dogs you have to level up" & @CRLF & "5.) Click the Begin Auto Feed Dog button at the bottom of the window") 
     Case $msg = $AutoLevel 
      $NumDogs = GUICtrlRead($NumDogsBox) 
      $stop = GUICtrlCreateButton('STOP', 830, 65) 

      $iState = 1 
      $i = 0 
     Case Else 
      If $iState = 1 Then 
       If $i >= $NumDogs Then 
        $iState = 3 
        ContinueLoop 
       EndIf 

       MsgBox(0, "", "1xp" & $i + 1) 
       ControlClick("[CLASS:MacromediaFlashPlayerActiveX]", "", "", "left", 1, 156, 651) 
       $iState = 2 
       $iTimer = TimerInit() 
      ElseIf $iState = 2 Then 
       If TimerDiff($iTimer) < 2000 Then ContinueLoop 

       ControlClick("[CLASS:MacromediaFlashPlayerActiveX]", "", "", "left", 1, 723, 524) 
       MsgBox(0, "", "Next Dog") 
       $iTimer = TimerInit() 
       $i = $i + 1 
      ElseIf $iState = 3 Then 
       If TimerDiff($iTimer) < 1000 Then ContinueLoop 

       If $i <= 0 Then 
        $iState = 1 
        ContinueLoop 
       EndIf 

       ControlClick("[CLASS:MacromediaFlashPlayerActiveX]", "", "", "left", 1, 62, 525) 
       MsgBox(0, "", "Previous Dog") 

       $iTimer = TimerInit() 
       $i = $i - 1 
      EndIf 
    EndSelect 
WEnd 
+0

謝謝!唯一缺少的就是將$ iState設置回1: 'ElseIf $ iState = 2 Then'。否則,它會繼續點擊「下一步」而不點擊我需要的內容。 – jparnell8839