Jump to content

Check toolbar button programmatically


guiAI
 Share

Recommended Posts

_GUICtrlToolbar_CheckButton() does the job, but:

WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
receives no signal.

Furthermore, using:

_SendMessage($hMain, $WM_COMMAND, $iCmd)
WM_COMMAND receives the signal, but the button remains unchecked.

Is there any other chance besides combining the two commands?

Thanks

guiAI

Link to comment
Share on other sites

_GUICtrlToolbar_CheckButton() does the job, but:

WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
receives no signal.

Furthermore, using:

_SendMessage($hMain, $WM_COMMAND, $iCmd)
WM_COMMAND receives the signal, but the button remains unchecked.

Is there any other chance besides combining the two commands?

Thanks

What event are you looking for? This gets all the messages that seem appropriate (modified version of the help file script):
#include <GuiToolbar.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>

Opt('MustDeclareVars', 1)
Opt('GuiOnEventMode', 1)

Global $hGUI, $hToolbar
Global Enum $idNew = 1000, $idOpen, $idSave, $idHelp
Global $iItem

; Create GUI
$hGUI = GUICreate("Toolbar", 400, 300)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")
$hToolbar = _GUICtrlToolbar_Create($hGUI)
GUISetState()

; Add standard system bitmaps
_GUICtrlToolbar_AddBitmap($hToolbar, 1, -1, $IDB_STD_LARGE_COLOR)

; Add buttons
_GUICtrlToolbar_AddButton($hToolbar, $idNew, $STD_FILENEW)
_GUICtrlToolbar_AddButton($hToolbar, $idOpen, $STD_FILEOPEN)
_GUICtrlToolbar_AddButton($hToolbar, $idSave, $STD_FILESAVE)
_GUICtrlToolbar_AddButtonSep($hToolbar)
_GUICtrlToolbar_AddButton($hToolbar, $idHelp, $STD_HELP)
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

; Loop until user exits
Global $iTimer = TimerInit()
While 1
    Sleep(10)
    ; Click a random button every 5sec
    If TimerDiff($iTimer) >= 5000 Then
        _GUICtrlToolbar_ClickButton($hToolbar, Random(1000, 1003, 1), "left", True)
        $iTimer = TimerInit()
    EndIf
WEnd

; WM_NOTIFY event handler
Func _WM_NOTIFY($hWndGUI, $MsgID, $wParam, $lParam)
    #forceref $hWndGUI, $MsgID, $wParam
    Local $tNMHDR, $event, $hwndFrom, $code, $i_idNew, $dwFlags, $lResult, $idFrom, $i_idOld
    Local $tNMTOOLBAR, $tNMTBHOTITEM
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hwndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $idFrom = DllStructGetData($tNMHDR, "IDFrom")
    $code = DllStructGetData($tNMHDR, "Code")
    Switch $hwndFrom
        Case $hToolbar
            Switch $code
                Case $NM_LDOWN
                    ConsoleWrite("_WM_NOTIFY():  code = $NM_LDOWN; Clicked Item: " & $iItem & " at index: " & _GUICtrlToolbar_CommandToIndex($hToolbar, $iItem) & @LF)
                Case $TBN_HOTITEMCHANGE
                    $tNMTBHOTITEM = DllStructCreate($tagNMTBHOTITEM, $lParam)
                    $i_idOld = DllStructGetData($tNMTBHOTITEM, "idOld")
                    $i_idNew = DllStructGetData($tNMTBHOTITEM, "idNew")
                    $iItem = $i_idNew ; Save new hotitem ID to global
                    $dwFlags = DllStructGetData($tNMTBHOTITEM, "dwFlags")
                    If BitAND($dwFlags, $HICF_LEAVING) = $HICF_LEAVING Then
                        ConsoleWrite("_WM_NOTIFY():  $HICF_LEAVING = " & $i_idOld & @LF)
                    Else
                        Switch $i_idNew
                            Case $idNew
                                ConsoleWrite("_WM_NOTIFY():  $TBN_HOTITEMCHANGE; $idNew = " & $idNew & @LF)
                            Case $idOpen
                                ConsoleWrite("_WM_NOTIFY():  $TBN_HOTITEMCHANGE; $idOpen = " & $idOpen & @LF)
                            Case $idSave
                                ConsoleWrite("_WM_NOTIFY():  $TBN_HOTITEMCHANGE; $idSave = " & $idSave & @LF)
                            Case $idHelp
                                ConsoleWrite("_WM_NOTIFY():  $TBN_HOTITEMCHANGE; $idHelp = " & $idHelp & @LF)
                        EndSwitch
                    EndIf
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY

Func _Close()
    Exit
EndFunc   ;==>_Close

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Hi PsaltyDS,

I'm trying to produce (programmaticaly) a click on a dual state button ($BTNS_CHECK),

get the relative message, then start an appropriate function in response.

Post a short demo script that can be run, or modify the one I posted, to reproduce your conditions.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Here's the modified code:

#include <GuiToolbar.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>

;~ Opt('MustDeclareVars', 1)
Opt('GuiOnEventMode', 1)

Global $hGUI, $hToolbar
Global Enum $idNew = 1000, $idOpen, $idSave, $idHelp
Global $iItem

; Create GUI
$hGUI = GUICreate("Toolbar", 400, 300)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")
$hToolbar = _GUICtrlToolbar_Create($hGUI)

$Click = GUICtrlCreateButton("Click", 100, 150, 75)

GUISetState()

; Add standard system bitmaps
_GUICtrlToolbar_AddBitmap($hToolbar, 1, -1, $IDB_STD_LARGE_COLOR)

; Add buttons
_GUICtrlToolbar_AddButton($hToolbar, $idNew, $STD_FILENEW)
_GUICtrlToolbar_AddButton($hToolbar, $idOpen, $STD_FILEOPEN)
_GUICtrlToolbar_AddButton($hToolbar, $idSave, $STD_FILESAVE)
_GUICtrlToolbar_AddButtonSep($hToolbar)
_GUICtrlToolbar_AddButton($hToolbar, $idHelp, $STD_HELP, 0, $BTNS_CHECK)
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

; Loop until user exits
Global $iTimer = TimerInit()
While 1
    Sleep(10)
    ; Click a random button every 5sec
    If TimerDiff($iTimer) >= 5000 Then
        _GUICtrlToolbar_ClickButton($hToolbar, Random(1000, 1003, 1), "left", True)
        $iTimer = TimerInit()
    EndIf
WEnd

; WM_NOTIFY event handler
Func _WM_NOTIFY($hWndGUI, $MsgID, $wParam, $lParam)
    #forceref $hWndGUI, $MsgID, $wParam
    Local $tNMHDR, $event, $hwndFrom, $code, $i_idNew, $dwFlags, $lResult, $idFrom, $i_idOld
    Local $tNMTOOLBAR, $tNMTBHOTITEM
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hwndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $idFrom = DllStructGetData($tNMHDR, "IDFrom")
    $code = DllStructGetData($tNMHDR, "Code")
    Switch $hwndFrom
        Case $hToolbar
            Switch $code
                Case $NM_LDOWN
                    ConsoleWrite("_WM_NOTIFY():  code = $NM_LDOWN; Clicked Item: " & $iItem & " at index: " & _GUICtrlToolbar_CommandToIndex($hToolbar, $iItem) & @LF)
                Case $TBN_HOTITEMCHANGE
                    $tNMTBHOTITEM = DllStructCreate($tagNMTBHOTITEM, $lParam)
                    $i_idOld = DllStructGetData($tNMTBHOTITEM, "idOld")
                    $i_idNew = DllStructGetData($tNMTBHOTITEM, "idNew")
                    $iItem = $i_idNew ; Save new hotitem ID to global
                    $dwFlags = DllStructGetData($tNMTBHOTITEM, "dwFlags")
                    If BitAND($dwFlags, $HICF_LEAVING) = $HICF_LEAVING Then
                        ConsoleWrite("_WM_NOTIFY():  $HICF_LEAVING = " & $i_idOld & @LF)
                    Else
                        Switch $i_idNew
                            Case $idNew
                                ConsoleWrite("_WM_NOTIFY():  $TBN_HOTITEMCHANGE; $idNew = " & $idNew & @LF)
                            Case $idOpen
                                ConsoleWrite("_WM_NOTIFY():  $TBN_HOTITEMCHANGE; $idOpen = " & $idOpen & @LF)
                            Case $idSave
                                ConsoleWrite("_WM_NOTIFY():  $TBN_HOTITEMCHANGE; $idSave = " & $idSave & @LF)
                            Case $idHelp
                                ConsoleWrite("_WM_NOTIFY():  $TBN_HOTITEMCHANGE; $idHelp = " & $idHelp & @LF)
                        EndSwitch
                    EndIf
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY

Func _Close()
    Exit
EndFunc   ;==>_Close
Now the question is how to change the state of dual state control $idHelp clicking the $Click button.

guiAI

Link to comment
Share on other sites

Here's the modified code:

#include <GuiToolbar.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>

;~ Opt('MustDeclareVars', 1)
Opt('GuiOnEventMode', 1)

Global $hGUI, $hToolbar
Global Enum $idNew = 1000, $idOpen, $idSave, $idHelp
Global $iItem

; Create GUI
$hGUI = GUICreate("Toolbar", 400, 300)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")
$hToolbar = _GUICtrlToolbar_Create($hGUI)

$Click = GUICtrlCreateButton("Click", 100, 150, 75)

GUISetState()

; Add standard system bitmaps
_GUICtrlToolbar_AddBitmap($hToolbar, 1, -1, $IDB_STD_LARGE_COLOR)

; Add buttons
_GUICtrlToolbar_AddButton($hToolbar, $idNew, $STD_FILENEW)
_GUICtrlToolbar_AddButton($hToolbar, $idOpen, $STD_FILEOPEN)
_GUICtrlToolbar_AddButton($hToolbar, $idSave, $STD_FILESAVE)
_GUICtrlToolbar_AddButtonSep($hToolbar)
_GUICtrlToolbar_AddButton($hToolbar, $idHelp, $STD_HELP, 0, $BTNS_CHECK)
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

; Loop until user exits
Global $iTimer = TimerInit()
While 1
    Sleep(10)
    ; Click a random button every 5sec
    If TimerDiff($iTimer) >= 5000 Then
        _GUICtrlToolbar_ClickButton($hToolbar, Random(1000, 1003, 1), "left", True)
        $iTimer = TimerInit()
    EndIf
WEnd

; WM_NOTIFY event handler
Func _WM_NOTIFY($hWndGUI, $MsgID, $wParam, $lParam)
    #forceref $hWndGUI, $MsgID, $wParam
    Local $tNMHDR, $event, $hwndFrom, $code, $i_idNew, $dwFlags, $lResult, $idFrom, $i_idOld
    Local $tNMTOOLBAR, $tNMTBHOTITEM
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hwndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $idFrom = DllStructGetData($tNMHDR, "IDFrom")
    $code = DllStructGetData($tNMHDR, "Code")
    Switch $hwndFrom
        Case $hToolbar
            Switch $code
                Case $NM_LDOWN
                    ConsoleWrite("_WM_NOTIFY():  code = $NM_LDOWN; Clicked Item: " & $iItem & " at index: " & _GUICtrlToolbar_CommandToIndex($hToolbar, $iItem) & @LF)
                Case $TBN_HOTITEMCHANGE
                    $tNMTBHOTITEM = DllStructCreate($tagNMTBHOTITEM, $lParam)
                    $i_idOld = DllStructGetData($tNMTBHOTITEM, "idOld")
                    $i_idNew = DllStructGetData($tNMTBHOTITEM, "idNew")
                    $iItem = $i_idNew ; Save new hotitem ID to global
                    $dwFlags = DllStructGetData($tNMTBHOTITEM, "dwFlags")
                    If BitAND($dwFlags, $HICF_LEAVING) = $HICF_LEAVING Then
                        ConsoleWrite("_WM_NOTIFY():  $HICF_LEAVING = " & $i_idOld & @LF)
                    Else
                        Switch $i_idNew
                            Case $idNew
                                ConsoleWrite("_WM_NOTIFY():  $TBN_HOTITEMCHANGE; $idNew = " & $idNew & @LF)
                            Case $idOpen
                                ConsoleWrite("_WM_NOTIFY():  $TBN_HOTITEMCHANGE; $idOpen = " & $idOpen & @LF)
                            Case $idSave
                                ConsoleWrite("_WM_NOTIFY():  $TBN_HOTITEMCHANGE; $idSave = " & $idSave & @LF)
                            Case $idHelp
                                ConsoleWrite("_WM_NOTIFY():  $TBN_HOTITEMCHANGE; $idHelp = " & $idHelp & @LF)
                        EndSwitch
                    EndIf
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY

Func _Close()
    Exit
EndFunc   ;==>_Close
Now the question is how to change the state of dual state control $idHelp clicking the $Click button.

Here is a version that changes the state of the button:

#include <GuiToolbar.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>

;~ Opt('MustDeclareVars', 1)
Opt('GuiOnEventMode', 1)

Global $hGUI, $hToolbar, $Click
Global Enum $idNew = 1000, $idOpen, $idSave, $idHelp
Global $iItem

; Create GUI
$hGUI = GUICreate("Toolbar", 400, 300)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")
$hToolbar = _GUICtrlToolbar_Create($hGUI)
$Click = GUICtrlCreateButton("Click", 100, 150, 75)
GUICtrlSetOnEvent(-1, "_ButtonHit")

GUISetState()

; Add standard system bitmaps
_GUICtrlToolbar_AddBitmap($hToolbar, 1, -1, $IDB_STD_LARGE_COLOR)

; Add buttons
_GUICtrlToolbar_AddButton($hToolbar, $idNew, $STD_FILENEW)
_GUICtrlToolbar_AddButton($hToolbar, $idOpen, $STD_FILEOPEN)
_GUICtrlToolbar_AddButton($hToolbar, $idSave, $STD_FILESAVE)
_GUICtrlToolbar_AddButtonSep($hToolbar)
_GUICtrlToolbar_AddButton($hToolbar, $idHelp, $STD_HELP, 0, $BTNS_CHECK)
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

; Loop until user exits
While 1
    Sleep(10)
WEnd

; WM_NOTIFY event handler
Func _WM_NOTIFY($hWndGUI, $MsgID, $wParam, $lParam)
    #forceref $hWndGUI, $MsgID, $wParam
    Local $tNMHDR, $event, $hwndFrom, $code, $i_idNew, $dwFlags, $lResult, $idFrom, $i_idOld
    Local $tNMTOOLBAR, $tNMTBHOTITEM
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hwndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $idFrom = DllStructGetData($tNMHDR, "IDFrom")
    $code = DllStructGetData($tNMHDR, "Code")
    Switch $hwndFrom
        Case $hToolbar
            Switch $code
                Case $NM_LDOWN
                    ConsoleWrite("_WM_NOTIFY():  code = $NM_LDOWN; Clicked Item: " & $iItem & " at index: " & _GUICtrlToolbar_CommandToIndex($hToolbar, $iItem) & @LF)
                Case $TBN_HOTITEMCHANGE
                    $tNMTBHOTITEM = DllStructCreate($tagNMTBHOTITEM, $lParam)
                    $i_idOld = DllStructGetData($tNMTBHOTITEM, "idOld")
                    $i_idNew = DllStructGetData($tNMTBHOTITEM, "idNew")
                    $iItem = $i_idNew ; Save new hotitem ID to global
                    $dwFlags = DllStructGetData($tNMTBHOTITEM, "dwFlags")
                    If BitAND($dwFlags, $HICF_LEAVING) = $HICF_LEAVING Then
                        ConsoleWrite("_WM_NOTIFY():  $HICF_LEAVING = " & $i_idOld & @LF)
                    Else
                        Switch $i_idNew
                            Case $idNew
                                ConsoleWrite("_WM_NOTIFY():  $TBN_HOTITEMCHANGE; $idNew = " & $idNew & @LF)
                            Case $idOpen
                                ConsoleWrite("_WM_NOTIFY():  $TBN_HOTITEMCHANGE; $idOpen = " & $idOpen & @LF)
                            Case $idSave
                                ConsoleWrite("_WM_NOTIFY():  $TBN_HOTITEMCHANGE; $idSave = " & $idSave & @LF)
                            Case $idHelp
                                ConsoleWrite("_WM_NOTIFY():  $TBN_HOTITEMCHANGE; $idHelp = " & $idHelp & @LF)
                        EndSwitch
                    EndIf
                Case Else
                    ConsoleWrite("_WM_NOTIFY():  Undefined code received from $hToolbar; code = " & $code & " (0x" & Hex($code, 8) & ")" & @LF)
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY

Func _ButtonHit()
    ConsoleWrite("_ButtonHit():  Button clicked" & @LF)
    Local $iState = _GUICtrlToolbar_IsButtonChecked($hToolbar, $idHelp)
    $iState = Not $iState
    ;_GUICtrlToolbar_CheckButton($hToolbar, $idHelp, $iState)
    _SendMessage($hToolbar, $TB_CHECKBUTTON, $idHelp, $iState)
EndFunc   ;==>_ButtonHit

Func _Close()
    Exit
EndFunc   ;==>_Close

Both methods change the checked state (uncomment either _GuiCtrlToolbar_CheckButton or _SendMessage inside the _ButtonHit function), but neither fires the TBN_HOTITEMCHANGE message. They DO send a WM_NOTIFY with code = -12 (0xFFFFFFF4), so you could detect that something happened. I don't know what the named definition of that code is. It's not included in ToolbarConstants.au3.

:)

P.S. Outside of looking for the WM_NOTIFY message, you can statically check the state any time with _GUICtrlToolbar_IsButtonChecked(). Updated demo to use that method for toggling the state.

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...