Jump to content

If the button is pressed, replace the button image


fs1234
 Share

Recommended Posts

Hi All,

I tried this code, but not working:

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

Global $DLLUser32 = DllOpen("user32.dll"), $fRun

Local $ButtonNo

$gui = GUICreate("Test Button Pressed", 350, 250, -1, -1)


$Button1 = GUICtrlCreateButton("Button1", 20, 40, 80, 80, $BS_BITMAP)
GUICtrlSetImage($Button1, @ScriptDir & "\Button1.bmp")
$Button2 = GUICtrlCreateButton("Button2", 120, 40, 80, 80, $BS_BITMAP)
GUICtrlSetImage($Button2, @ScriptDir & "\Button2.bmp")
$Button3 = GUICtrlCreateButton("Button3", 220, 40, 80, 80, $BS_BITMAP)
GUICtrlSetImage($Button3, @ScriptDir & "\Button3.bmp")

GUISetState()

While 1
    $MSG = GUIGetMsg()
    Select
        Case $MSG = $GUI_EVENT_CLOSE
            Exit
        Case $MSG = $Button1
            $ButtonNo = "1"
            _Display()
            While 1
                If _IsPressed("01", $DLLUser32) Then
                    If $fRun Then
                        GUICtrlSetImage($Button1, @ScriptDir & "\Button1_p.bmp")
                        $fRun = False
                    EndIf
                Else
                    GUICtrlSetImage($Button1, @ScriptDir & "\Button1.bmp")
                    $fRun = True
                EndIf
            WEnd
        Case $MSG = $Button2
            $ButtonNo = "2"
            _Display()
            While 1
                If _IsPressed("01", $DLLUser32) Then
                    If $fRun Then
                        GUICtrlSetImage($Button2, @ScriptDir & "\Button2_p.bmp")
                        $fRun = False
                    EndIf
                Else
                    GUICtrlSetImage($Button2, @ScriptDir & "\Button2.bmp")
                    $fRun = True
                EndIf
            WEnd
        Case $MSG = $Button3
            $ButtonNo = "3"
            _Display()
            While 1
                If _IsPressed("01", $DLLUser32) Then
                    If $fRun Then
                        GUICtrlSetImage($Button3, @ScriptDir & "\Button3_p.bmp")
                        $fRun = False
                    EndIf
                Else
                    GUICtrlSetImage($Button3, @ScriptDir & "\Button3.bmp")
                    $fRun = True
                EndIf
            WEnd

    EndSelect
WEnd

Func _Display()
    MsgBox(0, "", $ButtonNo)
EndFunc

The first pressed button changes continuously.

Help, pls.

Button1.bmp Button1_p.bmp Button2.bmp Button2_p.bmp Button3.bmp Button3_p.bmp

Link to comment
Share on other sites

I think the best way is to use callback, like this :

#include <Constants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WinAPIConstants.au3>
#include <WinAPISys.au3>
#include <WindowsConstants.au3>

Opt("MustDeclareVars", 1)

Local $gui = GUICreate("Test Button Pressed", 350, 250, -1, -1)

Local $Button1 = GUICtrlCreateButton("Button1", 20, 40, 80, 80, $BS_BITMAP)
GUICtrlSetImage($Button1, @ScriptDir & "\Button1.bmp")
Local $Button2 = GUICtrlCreateButton("Button2", 120, 40, 80, 80, $BS_BITMAP)
GUICtrlSetImage($Button2, @ScriptDir & "\Button2.bmp")
Local $Button3 = GUICtrlCreateButton("Button3", 220, 40, 80, 80, $BS_BITMAP)
GUICtrlSetImage($Button3, @ScriptDir & "\Button3.bmp")

GUISetState()

Local $wProcHandle = DllCallbackRegister("_WindowProc", "ptr", "hwnd;uint;wparam;lparam")
Local $wProcOld = _WinAPI_SetWindowLong(GUICtrlGetHandle($Button1), $GWL_WNDPROC, DllCallbackGetPtr($wProcHandle))
_WinAPI_SetWindowLong(GUICtrlGetHandle($Button2), $GWL_WNDPROC, DllCallbackGetPtr($wProcHandle))
_WinAPI_SetWindowLong(GUICtrlGetHandle($Button3), $GWL_WNDPROC, DllCallbackGetPtr($wProcHandle))

While 1
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      ExitLoop
    Case $Button1
      ConsoleWrite("button 1 pushed" & @CRLF)
  EndSwitch
WEnd

GUIDelete($gui)
DllCallbackFree($wProcHandle)

Func _WindowProc($hWnd, $iMsg, $wParam, $lParam)
  Switch $hWnd
    Case GUICtrlGetHandle($Button1)
      Switch $iMsg
        Case $WM_LBUTTONDOWN
          ConsoleWrite("Left mouse down" & @CRLF)
          GUICtrlSetImage($Button1, @ScriptDir & "\Button1_p.bmp")
        Case $WM_LBUTTONUP
          ConsoleWrite("Left mouse up" & @CRLF)
          GUICtrlSetImage($Button1, @ScriptDir & "\Button1.bmp")
        Case $WM_KEYDOWN
          If $wParam = 32 Then
            ConsoleWrite("Key Down " & $wParam & @CRLF)
            GUICtrlSetImage($Button1, @ScriptDir & "\Button1_p.bmp")
          EndIf
        Case $WM_KEYUP
          If $wParam = 32 Then
            ConsoleWrite("Key Up " & $wParam & @CRLF)
            GUICtrlSetImage($Button1, @ScriptDir & "\Button1.bmp")
          EndIf
      EndSwitch
  EndSwitch

  Return _WinAPI_CallWindowProc($wProcOld, $hWnd, $iMsg, $wParam, $lParam)

EndFunc   ;==>_WindowProc

_WindowProc manages only button 1.  But you can complete it by yourself.  I also added the key press :)

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...