Jump to content

While Button is Pressed


Recommended Posts

Hello.

I'm trying to make a gui button continually call the function while it is pressed.

In the example below the number only changes once you release the button, I want it to continually count up or down as long as I pressing the button.

I'm sure I've seen this before but can't seem to find it.

Thanks

#include <GUIConstantsEx.au3>

Opt("GuiOnEventMode",1)

Global $label

_Examlple()

While 1
    Sleep(100)
WEnd


Func _Examlple()

    Local $gui
    $gui = GUICreate("Test",500,200)
    GUISetOnEvent($GUI_EVENT_CLOSE,"_Exit")
    GUISetFont(72)

    $label = GUICtrlCreateLabel("0",20,20,100,100)
    $upButton = GUICtrlCreateButton("+",300,20,70,70)
    GUICtrlSetOnEvent($upButton,"_Add")

    $downButton = GUICtrlCreateButton("-",300,110,70,70)
    GUICtrlSetOnEvent($downButton,"_Subtract")

    GUISetState()

EndFunc

Func _Exit()
    Exit
EndFunc

Func _Add()
    GUICtrlSetData($label,Int(GUICtrlRead($label)) +1)
EndFunc

Func _Subtract()
    GUICtrlSetData($label,Int(GUICtrlRead($label)) -1)
EndFunc
Link to comment
Share on other sites

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

Global $_hBtnProc = DllCallbackRegister("_BtnProc", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr")
Global $_pBtnProc = DllCallbackGetPtr($_hBtnProc)

OnAutoItExitRegister("OnAutoItExit")

$hGUI = GUICreate("MyGUI")

$iButton1 = GUICtrlCreateButton("Button1", 10, 10, 80, 22)
_WinAPI_SetWindowSubclass(GUICtrlGetHandle($iButton1), $_pBtnProc, $iButton1, 0)

GUISetState(@SW_SHOW, $hGUI)

While GUIGetMsg() <> $GUI_EVENT_CLOSE

WEnd

Func _BtnProc($hWnd, $iMsg, $wParam, $lParam, $ID, $pData)
    #forceref $pData, $ID

    Switch $iMsg
        Case $WM_LBUTTONDOWN
            While _IsPressed("01")
                ConsoleWrite("pressed" & @MSEC & @LF)
                Sleep(10)
            WEnd
    EndSwitch

    Return _WinAPI_DefSubclassProc($hWnd, $iMsg, $wParam, $lParam)
EndFunc   ;==>_BtnProc

Func OnAutoItExit()
    _WinAPI_RemoveWindowSubclass(GUICtrlGetHandle($iButton1), $_pBtnProc, $iButton1)
    DllCallbackFree($_hBtnProc)
EndFunc   ;==>OnAutoItExit

Link to comment
Share on other sites

Without subclassing and dllcallback

; johnmcloud 2014
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$hGUI = GUICreate("MyGUI")
$iButton1 = GUICtrlCreateButton("Add", 10, 10, 80, 22)
$iLabel1 = GUICtrlCreateLabel("0", 10, 50, 25)
GUISetState(@SW_SHOW, $hGUI)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    If _ButtonClicked($iButton1) = 1 Then
        GUICtrlSetData($iLabel1, Int(GUICtrlRead($iLabel1)) + 1)
        Sleep(100)
    EndIf
WEnd

Func _ButtonClicked($iButton)
    Local $aResult = DllCall("user32.dll", "lresult", "SendMessageW", "hwnd", GUICtrlGetHandle($iButton), "uint", 0xF2, "wparam", 0, "lparam", 0)
    If @error Then Return 0
    If BitAND($aResult[0], 0x4) = 0x4 Then Return 1
EndFunc   ;==>_ButtonClicked
Link to comment
Share on other sites

Another way using GUIGetCursorInfo.

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

$hGUI = GUICreate("MyGUI")
$iButton1 = GUICtrlCreateButton("Add", 10, 10, 80, 22)
$iLabel1 = GUICtrlCreateLabel("0", 10, 50, 25)
GUISetState(@SW_SHOW, $hGUI)

Local $iInfo = 0
Local $i = 1
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_PRIMARYDOWN
            If WinActive($hGUI) Then
                $iInfo = GUIGetCursorInfo()
                If $iInfo[2] And $iInfo[4] = $iButton1 Then
                    While $iInfo[2] and $iInfo[4]=$iButton1
                        $iInfo = GUIGetCursorInfo()
                        GUICtrlSetData($iLabel1, $i)
                        $i += 1
                    WEnd
                EndIf
            EndIf
    EndSwitch
WEnd

Saludos

Edited by Danyfirex
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...