Jump to content

Recommended Posts

Posted

Good day all, I've been trying to learn AutoIt for a while now, and I'm just struggling, when I click the button here nothing happens?

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

; Create the main GUI window
Global $hGUI = GUICreate("Color Clicker", 300, 150)

; Create a label for the hex color code text field
GUICtrlCreateLabel("Enter a hex color code (e.g. FFFFFF):", 10, 10, 280, 20)

; Create a text field for the hex color code
Global $hTextField = GUICtrlCreateInput("", 10, 30, 280, 20)

; Create a button to start the color clicker
Local $hButton = GUICtrlCreateButton("Click Color", 10, 60, 80, 30)
GUICtrlSetOnEvent(-1, "ButtonClick")

; Show the GUI window
GUISetState(@SW_SHOW)

; GUI event loop
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

; Button click event handler
Func ButtonClick()
    ; Get the hex color code from the text field
    Local $sHexCode = GUICtrlRead($hTextField)

    ; Convert the hex color code to a decimal color value
    Local $iColor = Hex($sHexCode, 6)

    ; Search the screen for pixels of the specified color and click them
    Local $aPos = PixelSearch(0, 0, @DesktopWidth, @DesktopHeight, $iColor, 0, 1)
    While IsArray($aPos)
        MouseClick("left", $aPos[0], $aPos[1])
        Sleep(50)
        $aPos = PixelSearch(0, 0, @DesktopWidth, @DesktopHeight, $iColor, 0, 1)
    WEnd
EndFunc

What am I doing wrong??

I appreciate any help, I'm just stuck!

Posted

Like @Danp2 said, if you want to use GUICtrlSetOnEvent, then you need to also set OnEventMode as they showed. 

Otherwise, you need to in your Switch GUIGetMsg look for the button and call your function, like:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

; Create the main GUI window
Global $hGUI = GUICreate("Color Clicker", 300, 150)

; Create a label for the hex color code text field
GUICtrlCreateLabel("Enter a hex color code (e.g. FFFFFF):", 10, 10, 280, 20)

; Create a text field for the hex color code
Global $hTextField = GUICtrlCreateInput("", 10, 30, 280, 20)

; Create a button to start the color clicker
Local $hButton = GUICtrlCreateButton("Click Color", 10, 60, 80, 30)

; Show the GUI window
GUISetState(@SW_SHOW)

; GUI event loop
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $hButton
            ButtonClick()
    EndSwitch
WEnd

; Button click event handler
Func ButtonClick()
    ; Get the hex color code from the text field
    Local $sHexCode = GUICtrlRead($hTextField)

    ; Convert the hex color code to a decimal color value
    Local $iColor = Hex($sHexCode, 6)

    ; Search the screen for pixels of the specified color and click them
    MsgBox(0, 'ButtonClick', 'Button clicked!')
;~     Local $aPos = PixelSearch(0, 0, @DesktopWidth, @DesktopHeight, $iColor, 0, 1)
;~     While IsArray($aPos)
;~         MouseClick("left", $aPos[0], $aPos[1])
;~         Sleep(50)
;~         $aPos = PixelSearch(0, 0, @DesktopWidth, @DesktopHeight, $iColor, 0, 1)
;~     WEnd
EndFunc   ;==>ButtonClick

Also be careful with your While IsArray...PixelSearch section, as if you're searching for a color that doesn't change when you click on it you'll be stuck in an infinite loop.

We ought not to misbehave, but we should look as though we could.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...