Jump to content
Sign in to follow this  
ChrisL

While Button is Pressed

Recommended Posts

ChrisL

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

Share this post


Link to post
Share on other sites
FireFox

#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


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
johnmcloud

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

Share this post


Link to post
Share on other sites
Danyfirex

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

Share this post


Link to post
Share on other sites
ChrisL

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  

×