Sign in to follow this  
Followers 0
fireflower

Trouble with starting looped function and usingPixelGetColor

5 posts in this topic

So, I am trying to create a pretty simple script for testing purposes. Basically what I need to do is this: there's a GUI window. When the user presses "Start", the script clicks around randomly. It searches for a particular color of pixel. If that color is found (the color of the desktop background, indicating that the software has crashed) the script stops running. It also stops running if the user hits the "Exit" button.

I have two problems:

1) I can't get the PixelGetColor function to work at all.

2) I can't seem to get my "start clicking" function to be tied to the Start button. It just starts as soon as you open up the script.

If I leave the commented-out stuff below as such, the script runs. Otherwise I get an error about While not having a corresponding "WEnd", even though it does.

Thank you in advance for any thoughts or advice:)!

#include

Opt("GUIOnEventMode", 1)

Global $ExitID

_Main()

Func _Main()
Local $StartID

GUICreate("Testing Tool", 250, 90)
$StartID = GUICtrlCreateButton("Start", 10, 60, 60, 20)
GUICtrlSetOnEvent($StartID, "OnStart")
$ExitID = GUICtrlCreateButton("Exit", 150, 60, 60, 20)
GUICtrlSetOnEvent($ExitID, "OnExit")
GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit")

GUISetState()

; $i = 0
; While $i = 0
; $color = PixelGetColor(437,482)
; If $color = Dec("0xdc4fad") Then
; Exit
; Else
; $i = 1
While 1
Sleep(1000)
OnStart()
WEnd
EndFunc


Func OnStart()
MouseClick("left" , 578, 388, 2, 1)
Sleep(5000)
MouseClick("right" , 635, 457, 1, 1)
Sleep(5000)
MouseClick("left" , 697, 387, 2, 1)
Sleep(5000)
MouseClickDrag("left" , 800, 300, 20, 1)
EndFunc


Func OnExit()
If @GUI_CtrlId = $ExitID Then
MsgBox(0, "Bye!", "Automation will now stop")
Else
MsgBox(0, "Bye!", "Automation will now stop")
EndIf

Exit
EndFunc

Share this post


Link to post
Share on other sites



The problem of auto starting is fairly easy to fix. You are currently trying to utilize the GUICtrlSetOnEvent Function, which has never worked well for me.

Here is my modified version of your code, note that with your onstart command, during the sleeping periods your exit button will not work because it is pausing the GUIGetMsg in order to run the Function.

I'll see if I can find a better way to do that...

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>

GUICreate("Testing Tool", 250, 90)
$StartID = GUICtrlCreateButton("Start", 10, 60, 60, 20)
$ExitID = GUICtrlCreateButton("Exit", 150, 60, 60, 20)
GUISetState(@SW_SHOW)

; $i = 0
; While $i = 0
; $color = PixelGetColor(437,482)
; If $color = Dec("0xdc4fad") Then
; Exit
; Else
; $i = 1
While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $StartID
            OnStart()
        Case $ExitID
            MsgBox(0, "Bye!", "Automation will now stop")
            Exit
    EndSwitch
WEnd



Func OnStart()
    MouseClick("left", 578, 388, 2, 1)
    Sleep(5000)
    MouseClick("right", 635, 457, 1, 1)
    Sleep(5000)
    MouseClick("left", 697, 387, 2, 1)
    Sleep(5000)
    MouseClickDrag("left", 800, 300, 20, 1)
EndFunc   ;==>OnStart

Share this post


Link to post
Share on other sites

Disregard any comments about OnEvent mode not working, because that's just not true, it works just fine.

Your problem is that your function onstart is called inside the While loop, so of course it's going to start itself. It will also restart itself after it's run because it's in there too. You need to remove the onstart function call from inside the While loop, after you do that, this will run as expected.


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

Share this post


Link to post
Share on other sites

Your problem is that your function onstart is called inside the While loop, so of course it's going to start itself. It will also restart itself after it's run because it's in there too. You need to remove the onstart function call from inside the While loop, after you do that, this will run as expected.

That is what I figured; the issue was that I want the mouse clicks to loop (sorry, I think I forgot to mention that in my original post) and I was not successfully getting that to happen without including the onstart function within the While loop. Do you happen to have a recommendation for a method that would allow the function to be called by the Start button and then loop until the Stop button is pressed or the user closes the GUI window?

Share this post


Link to post
Share on other sites

The problem of auto starting is fairly easy to fix. You are currently trying to utilize the GUICtrlSetOnEvent Function, which has never worked well for me.

While 1
$Msg = GUIGetMsg()
Switch $Msg
Case $StartID
OnStart()
Case $ExitID
MsgBox(0, "Bye!", "Automation will now stop")
Exit
EndSwitch
WEnd

Thank you very much for the info! The one thing I neglected to mention in my original post is that I want the onstart function to loop until the user presses the exit button. Is there any way to cause this function to loop while utilizing a Switch statement?

Share this post


Link to post
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
Sign in to follow this  
Followers 0