在Autoit中刷新并执行程序

时间:2015-09-15 12:33:00

标签: autoit

我想使用GUI创建一个自动程序--2个按钮:第一个按钮必须每3秒刷新一个网页(例如:Google页面),第二个按钮必须停止刷新并在网络上执行一些操作页。我没有第一个按钮,但我无法使用第二个按钮退出循环并执行我想要的操作。有人能帮我吗?非常感谢!这是代码:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>

$Form1 = GUICreate("Example", 190, 133, 192, 124)
GUISetBkColor(0xFFFF00)
$Button1 = GUICtrlCreateButton("UPDATE", 3, 31 ,179, 37, 0)
$Button2 = GUICtrlCreateButton("EXECUTE", 3, 80 ,179, 37, 0)

GUICtrlSetColor(-1, 0x0000FF)
GUICtrlSetBkColor(-1, 0x00FF00)
GUICtrlSetCursor (-1, 0)


GUICtrlSetColor(-1, 0x000000)
GUISetState(@SW_SHOW)


While 1
   $nMsg = GUIGetMsg()
   Switch $nMsg
           Case $GUI_EVENT_CLOSE
                   Exit
           Case $button1

                 HotKeySet("{x}", "_Exit") ; Sets a shortcut to kill the script quickly. ESC
                 Opt("WinTitleMatchMode", 3) ; This is to tell the script to only refresh the EXACT window, incase there are similar windows. Such as: Window 1, Window 2.
                 While 1 ; This begins a While LOOP
                       Sleep(3000) ; Tell the script to pause for 3 seconds
                       $title = WinGetTitle("Google - Mozilla Firefox", "") ; Grabs the exactly title and saves it to a variable to do an If statement against.
                       If WinExists($title) Then ; Check to see if the desired window is running.
                       WinActivate($title) ; If it is running, first activate the window.
                       WinWaitActive($title) ; This tells the script to not send the refresh key (F5) until the window has appeared.
                       Send("{F5}") ; This sends the F5 key to refresh the window.
                       Else ; This is an else statement to tell the script to do a different operation if the window is not running.
                         Sleep(10) ; Just a filler item to wait until the desired window is running.
                       EndIf ; End the If statement to continue the script.
                 WEnd ; Close the While LOOP
                 Func _Exit() ; This is the function that is linked to the kill switch, ESC. It tells the script to perform the following actions.
                 ExitLoop ; Block pressing "x"
              EndFunc ; Close the function up.

           Case $button2
                 ExitLoop ; stop the loop and make operations below
                 Send("{TAB}")
                 Send("^v")

EndSwitch
WEnd

1 个答案:

答案 0 :(得分:1)

这样的东西?

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>

Global $Go2 = True

$Form1 = GUICreate("Example", 190, 133, 192, 124)
GUISetBkColor(0xFFFF00)
$Button1 = GUICtrlCreateButton("UPDATE", 3, 31, 179, 37, 0)
$Button2 = GUICtrlCreateButton("EXECUTE", 3, 80, 179, 37, 0)

GUICtrlSetColor(-1, 0x0000FF)
GUICtrlSetBkColor(-1, 0x00FF00)
GUICtrlSetCursor(-1, 0)


GUICtrlSetColor(-1, 0x000000)
GUISetState(@SW_SHOW)

Local $hTimer = TimerInit()
While True
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $Go2 = True
            HotKeySet("!{e}", "_Exit") ; Sets a shortcut to kill the script quickly. ESC
            HotKeySet("{s}", "_ExitLoop") ; Sets a shortcut to kill the script quickly. ESC
            Opt("WinTitleMatchMode", 3) ; This is to tell the script to only refresh the EXACT window, incase there are similar windows. Such as: Window 1, Window 2.
            While $Go2 ; Avoid getting stuck in the while loop forever
                If TimerDiff($hTimer) > 3000 Then
                    ConsoleWrite("Refreshing Mozilla" & @LF)
                    $title = WinGetTitle("Google - Mozilla Firefox", "") ; Grabs the exactly title and saves it to a variable to do an If statement against.
                    If WinExists($title) Then ; Check to see if the desired window is running.
                        WinActivate($title) ; If it is running, first activate the window.
                        WinWaitActive($title) ; This tells the script to not send the refresh key (F5) until the window has appeared.
                        Send("{F5}") ; This sends the F5 key to refresh the window.
                        $hTimer = TimerInit()
                        HotKeySet("{x}")
                    Else ; This is an else statement to tell the script to do a different operation if the window is not running.
;~                              Sleep(10) ; Just a filler item to wait until the desired window is running.
                    EndIf ; End the If statement to continue the script.
                EndIf
            WEnd
            $hTimer = TimerInit()
            HotKeySet("{x}")        ; Release hotkey
            HotKeySet("!{e}")       ; Release hotkeys
            ConsoleWrite("Exited loop" & @LF)
        Case $Button2
            ;you can't exit the loop from the $button1 here but there is a hotkey set to exit it, ALT + E
            Send("{TAB}")
            Send("^v")

    EndSwitch
WEnd
Exit

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _ExitLoop()
    $Go2 = Not $Go2
EndFunc

P.S。我避免使用睡眠,因为它会暂停脚本。