ToolBar Probleme

  • Hallo,

    weiß jemand von euch wie man bei einer Toolbar die Funktionen einsetzen kann?
    Ich hab's mit folgendem Probiert... hat aber leider nicht Funktioniert!

    [autoit]

    _GUICtrlToolbar_AddButton($hToolbar, $idSave, $STD_FILESAVE)
    GUICtrlSetOnEvent(-1, "_Save")

    [/autoit]

    Bitte helft mir
    Danke!

    • Offizieller Beitrag

    GuiCtrlSetOnEvent arbeitet nicht mit _GUICtrlToolbar_AddButton. Du mußt den State mit _GUICtrlToolbar_GetButtonState abfragen .

    Edit habe was im Englischen Forum gefunden . Kannst es eventuell für dich anpassen .

    Spoiler anzeigen
    [autoit]

    #include <GuiToolbar.au3>
    #include <GuiConstantsEx.au3>
    #include <WindowsConstants.au3>
    #include <Constants.au3>
    #include <GuiMenu.au3>
    #Include <GuiRebar.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Opt('MustDeclareVars', 1)

    [/autoit] [autoit][/autoit] [autoit]

    $Debug_TB = False ; Check ClassName being passed to functions, set to True and use a handle to another control to see it work

    [/autoit] [autoit][/autoit] [autoit]

    Global Enum $idNew = 1000, $idOpen, $idSave, $idHelp
    Global $hGUI, $hToolbar, $Tbar, $TbarMenu, $iMemo, $msg
    Global $iItem ; Command identifier of the button associated with the notification.
    Global Enum $idFile = 1100, $idExit

    [/autoit] [autoit][/autoit] [autoit]

    _Main()

    [/autoit] [autoit][/autoit] [autoit]

    Func _Main()
    Local $aSize

    [/autoit] [autoit][/autoit] [autoit]

    ; Create GUI
    $hGUI = GUICreate("Toolbar", 400, 300)
    $hToolbar = _GUICtrlToolbar_Create($hGUI)
    $aSize = _GUICtrlToolbar_GetMaxSize($hToolbar)
    $iMemo = GUICtrlCreateEdit("", 2, $aSize[1] + 20, 596, 396 - ($aSize[1] + 20), $WS_VSCROLL)
    GUICtrlSetFont($iMemo, 9, 400, 0, "Courier New")

    $Tbar = GUICtrlCreateDummy() ; dummy control to receive toolbar events
    $TbarMenu = GUICtrlCreateDummy() ; dummy control to receive toolbar button dropdown menu events

    GUISetState()
    GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

    [/autoit] [autoit][/autoit] [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

    ; Add buttons
    _GUICtrlToolbar_AddButton($hToolbar, $idNew, $STD_FILENEW, 0, BitOR($BTNS_DROPDOWN, $BTNS_WHOLEDROPDOWN))
    _GUICtrlToolbar_AddButton($hToolbar, $idOpen, $STD_FILEOPEN)
    _GUICtrlToolbar_AddButton($hToolbar, $idSave, $STD_FILESAVE)
    _GUICtrlToolbar_AddButtonSep($hToolbar)
    _GUICtrlToolbar_AddButton($hToolbar, $idHelp, $STD_HELP)

    [/autoit] [autoit][/autoit] [autoit]

    ; Loop until user exits

    While 1
    $msg = GUIGetMsg()
    Switch $msg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $Tbar ; toolbar buttons
    _ToolBar_Click(GUICtrlRead($Tbar))
    Case $TbarMenu ; toolbar button dropdown menu
    _ToolBarMenu(GUICtrlRead($TbarMenu))
    EndSwitch
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    EndFunc ;==>_Main

    [/autoit] [autoit][/autoit] [autoit]

    ; Toolbar: call functions for toolbar button dropdown menu
    Func _ToolBarMenu($TbarMenuCmdID)
    Switch $TbarMenuCmdID
    Case $idFile
    MsgBox(4096, "Pop-Up Menu", "File Selected")
    Case $idExit
    MsgBox(4096, "Pop-Up Menu", "Exit Selected")
    Exit
    EndSwitch
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ; Toolbar: call functions for toolbar buttons
    Func _ToolBar_Click($TbarCmdID)
    Switch $TbarCmdID
    ;Case $idNew ; FileNew
    ;Beep(1500, 5)
    Case $idOpen ; FileOpen
    Beep(1000, 5)
    FileOpenDialog("FileOpen",@WorkingDir,"All (*.*)", 1 + 2)
    Case $idSave ; FileSave
    Beep(2000, 5)
    Case $idHelp ; Help
    Beep(2500, 5)
    EndSwitch
    Return
    EndFunc ;==>_ToolBar_Click

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ; Write message to memo
    Func MemoWrite($sMessage = "")
    GUICtrlSetData($iMemo, $sMessage & @CRLF, 1)
    EndFunc ;==>MemoWrite

    [/autoit] [autoit][/autoit] [autoit]

    ; 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, $hMenu, $hSubmenu, $aRet, $iMenuID
    Local Const $TBDDRET_TREATPRESSED = 2

    [/autoit] [autoit][/autoit] [autoit]

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hwndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $idFrom = DllStructGetData($tNMHDR, "IDFrom")
    $code = DllStructGetData($tNMHDR, "Code")
    Switch $hwndFrom
    Case $hToolbar
    Switch $code
    Case $TBN_DROPDOWN
    $hMenu = _GUICtrlMenu_CreatePopup()
    _GUICtrlMenu_AddMenuItem($hMenu, "File")
    _GUICtrlMenu_AddMenuItem($hMenu, "Exit", $idExit)
    $hSubmenu = _GUICtrlMenu_CreateMenu()
    ; set menu item "File" to be submenu instead of selectable item
    _GUICtrlMenu_SetItemSubMenu($hMenu, 0, $hSubmenu) ; menu item position 0 is "File", 1 is "Exit"
    _GUICtrlMenu_AddMenuItem($hSubmenu, "File", $idFile) ; add or insert your items to submenu

    $aRet = _GetToolbarButtonScreenPos($hGUI, $hToolbar, $idNew)
    If Not IsArray($aRet) Then
    Dim $aRet[2] = [-1, -1]
    EndIf
    ; send button dropdown menu item commandID to dummy control for use in GuiGetMsg() or GUICtrlSetOnEvent()
    ; allows quick return from message handler : See warning for GUIRegisterMsg() in helpfile
    $iMenuID = _GUICtrlMenu_TrackPopupMenu($hMenu, $hToolbar, $aRet[0], $aRet[1], 1, 1, 2)
    GUICtrlSendToDummy($TbarMenu, $iMenuID)
    _GUICtrlMenu_DestroyMenu($hMenu)
    If $iMenuID Then Return $TBDDRET_TREATPRESSED
    Case $NM_LDOWN
    Switch _GUICtrlToolbar_IsButtonEnabled($hToolbar, $iItem) ; add check for button state if disabled
    Case True
    ; send toolbar commandID to dummy control for use in GuiGetMsg() or GUICtrlSetOnEvent()
    ; allows quick return from message handler : See warning for GUIRegisterMsg() in helpfile
    GUICtrlSendToDummy($Tbar, $iItem)
    EndSwitch
    ;----------------------------------------------------------------------------------------------
    MemoWrite("$NM_LDOWN: Clicked Item: " & $iItem & " at index: " & _GUICtrlToolbar_CommandToIndex($hToolbar, $iItem))
    ;----------------------------------------------------------------------------------------------
    Case $TBN_HOTITEMCHANGE
    $tNMTBHOTITEM = DllStructCreate($tagNMTBHOTITEM, $lParam)
    $i_idOld = DllStructGetData($tNMTBHOTITEM, "idOld")
    $i_idNew = DllStructGetData($tNMTBHOTITEM, "idNew")
    $iItem = $i_idNew
    $dwFlags = DllStructGetData($tNMTBHOTITEM, "dwFlags")
    If BitAND($dwFlags, $HICF_LEAVING) = $HICF_LEAVING Then
    MemoWrite("$HICF_LEAVING: " & $i_idOld)
    Else
    Switch $i_idNew
    Case $idNew
    ;----------------------------------------------------------------------------------------------
    MemoWrite("$TBN_HOTITEMCHANGE: $idNew")
    ;----------------------------------------------------------------------------------------------
    Case $idOpen
    ;----------------------------------------------------------------------------------------------
    MemoWrite("$TBN_HOTITEMCHANGE: $idOpen")
    ;----------------------------------------------------------------------------------------------
    Case $idSave
    ;----------------------------------------------------------------------------------------------
    MemoWrite("$TBN_HOTITEMCHANGE: $idSave")
    ;----------------------------------------------------------------------------------------------
    Case $idHelp
    ;----------------------------------------------------------------------------------------------
    MemoWrite("$TBN_HOTITEMCHANGE: $idHelp")
    ;----------------------------------------------------------------------------------------------
    EndSwitch
    EndIf
    EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
    EndFunc ;==>_WM_NOTIFY

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _GetToolbarButtonScreenPos($hwnd, $hTbar, $iCmdID, $iIndex = 0, $hRbar = -1) ; $hRbar and $iIndex is for optional Rebar hwnd and band index
    ; Author: rover 04/08/2008
    ; this UDF integrates _WinAPI_ClientToScreen() from WinAPI.au3 include
    ; _GUICtrlMenu_TrackPopupMenu() uses screen coordinates to place dropdown menu
    ; need to convert button client coordinates to screen coordinates
    Local $aBorders, $aBandRect, $aRect, $tpoint, $pPoint, $aRet[2]
    Local $aRect = _GUICtrlToolbar_GetButtonRect($hTbar, $iCmdID) ; 'Options' button with dropdown menu
    If Not IsArray($aRect) Then Return SetError(@error, 0, "")
    $tpoint = DllStructCreate("int X;int Y")
    DllStructSetData($tpoint, "X", $aRect[0])
    DllStructSetData($tpoint, "Y", $aRect[3])
    $pPoint = DllStructGetPtr($tPoint)
    DllCall("User32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", $pPoint)
    If @error Then Return SetError(@error, 0, "")
    If $hRbar <> -1 And IsHWnd($hRbar) And IsNumber($iIndex) Then
    $aBorders = _GUICtrlRebar_GetBandBorders($hRBar, $iIndex)
    If Not IsArray($aBorders) Then Return SetError(@error, 0, "")
    $aBandRect = _GUICtrlRebar_GetBandRect($hRBar, $iIndex)
    If Not IsArray($aBandRect) Then Return SetError(@error, 0, "")
    ; X screen coordinate of dropdown button left corner
    ; subtract 2 pixel border of bounding rectangle for band in rebar control
    $aRet[0] = (DllStructGetData($tpoint, "X") + $aBorders[0]) - $aBandRect[0]
    Else
    ; X screen coordinate of dropdown button left corner
    $aRet[0] = DllStructGetData($tpoint, "X")
    EndIf
    ; Y screen coordinate of dropdown button left corner
    $aRet[1] = DllStructGetData($tpoint, "Y")
    Return $aRet ; return X,Y screen coordinates of toolbar dropdown button lower left corner
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit]

    Schau mal unter WM_NOTIFY nach .

  • mit Case $NM_CLICK
    statt Case $NM_LDOWN
    wird erst auf das Loslaseen der Maus gewartet und nicht beim drücken schon der Klick gesendet.