TreeView Funktionszuweisung (OnEventMode) schlägt fehl

    • Offizieller Beitrag

    Hi,
    hier mal ein Problem von mir. Ich erstelle ein TreeView und füge Einträge mit den UDF-Funktionen _GUICtrlTreeView_Add() und _GUICtrlTreeView_AddChild() hinzu. Da ich Icons zuordne, muß ich zwingend auf die UDF zugreifen.
    Ich arbeite im OnEventMode und möchte bei Klick auf ein Item den Text des Items ausgeben. Bei Verwendung von GUICtrlCreateTreeViewItem() funktioniert das tadellos. Aber mit den UDF wird nicht die ID sondern das Handle zurückgegeben. Vermutlich läßt sich deshalb keine Funktion zuweisen.
    Mein zweiter Versuch dann mit WM_NOTIFY. Das funktioniert zwar - aber mit einem Versatz von einem Klick. D.h. das jetzt angeklickte Item wird erst beim Folgeklick ausgewertet. ?(
    Hier mal das Skript, vielleicht habt ihr ja eine zündende Idee.

    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>
    #include <GuiTreeView.au3>
    #include <TreeViewConstants.au3>
    #include <WindowsConstants.au3>

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

    Opt("MustDeclareVars", 1)
    Opt("GuiOnEventMode", 1)

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

    Global $gui, $hImage, $hTV, $aID_Sub[5], $ID_Table

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

    $gui = GUICreate('Test TreeView')
    GUISetOnEvent($GUI_EVENT_CLOSE, '_end')
    $hTV = GUICtrlCreateTreeView(10,10,300,350,BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, _
    $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)

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

    $hImage = _GUIImageList_Create(16, 16, 5, 3)
    _GUIImageList_AddIcon($hImage, "shell32.dll", 98)
    _GUIImageList_AddIcon($hImage, "shell32.dll", 96)
    _GUIImageList_AddIcon($hImage, "shell32.dll", 36)
    _GUICtrlTreeView_SetNormalImageList($hTV, $hImage)

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

    _GUICtrlTreeView_BeginUpdate($hTV)
    $ID_Table = _GUICtrlTreeView_Add($hTV, 0, 'Table', 0, 0)
    For $i = 1 To 5
    $aID_Sub[$i-1] = _GUICtrlTreeView_AddChild($hTV, $ID_Table, 'Sub' & $i, 1, 1)
    GUICtrlSetOnEvent($aID_Sub[$i-1], 'click_TV') ; does'nt work (because have no ID and handle instead ?)
    For $j = 1 To 3
    _GUICtrlTreeView_AddChild($hTV, $aID_Sub[$i-1], 'Sub_Sub' & $j, 2, 2)
    Next
    Next
    _GUICtrlTreeView_EndUpdate($hTV)
    _GUICtrlTreeView_Expand($hTV, $ID_Table)

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

    GUISetState()
    ;~ GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY') ; by using WM_NOTIFY are strange results, are reading with an offset of 1 click!

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

    While 1
    Sleep(100)
    WEnd

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

    Func _end()
    Exit
    EndFunc

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

    Func click_TV()
    ConsoleWrite(_GUICtrlTreeView_GetText($hTV, _GUICtrlTreeView_GetSelection($hTV)) & @CRLF)
    EndFunc

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

    Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
    $hWndTreeview = $hTV
    If Not IsHWnd($hTV) Then $hWndTreeview = GUICtrlGetHandle($hTV)

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

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
    Case $hWndTreeview
    Switch $iCode
    Case $NM_CLICK
    click_TV()
    EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
    EndFunc ;==>WM_NOTIFY

    [/autoit]

    Edit: Habs jetzt zusätzlich ins EN-Forum gestellt http://www.autoitscript.com/forum/index.php?showtopic=88587&view=findpost&p=636501

    • Offizieller Beitrag

    Ich nehme mal an , das der Fehler kommt weil du in WM_Notify abfragst ob die Mausgedrückt wurde, bevor der Befehl (Message) zum setzen des Selektierten Items ausgeführt wurde .

    • Offizieller Beitrag

    Stimmt, darum hab ich jetzt ein Workaround gemacht unter Nutzung von $GUI_EVENT_PRIMARYUP.
    Ich wäre aber trotzdem interessiert, eine Möglichkeit zu finden den Items direkt eine Funktion zuzuweisen.

    Spoiler anzeigen
    [autoit]

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

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

    Opt("MustDeclareVars", 1)
    Opt("GuiOnEventMode", 1)

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

    Global $gui, $hImage, $hTV, $aID_Sub[5], $ID_Table

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

    $gui = GUICreate('Test TreeView')
    GUISetOnEvent($GUI_EVENT_CLOSE, '_end')
    GUISetOnEvent($GUI_EVENT_PRIMARYUP, '_PrimaryUp')
    $hTV = GUICtrlCreateTreeView(10,10,300,350,BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, _
    $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)

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

    $hImage = _GUIImageList_Create(16, 16, 5, 3)
    _GUIImageList_AddIcon($hImage, "shell32.dll", 98)
    _GUIImageList_AddIcon($hImage, "shell32.dll", 96)
    _GUIImageList_AddIcon($hImage, "shell32.dll", 36)
    _GUICtrlTreeView_SetNormalImageList($hTV, $hImage)

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

    _GUICtrlTreeView_BeginUpdate($hTV)
    $ID_Table = _GUICtrlTreeView_Add($hTV, 0, 'Table', 0, 0)
    For $i = 1 To 5
    $aID_Sub[$i-1] = _GUICtrlTreeView_AddChild($hTV, $ID_Table, 'Sub' & $i, 1, 1)
    For $j = 1 To 3
    _GUICtrlTreeView_AddChild($hTV, $aID_Sub[$i-1], 'Sub_Sub' & $j, 2, 2)
    Next
    Next
    _GUICtrlTreeView_EndUpdate($hTV)
    _GUICtrlTreeView_Expand($hTV, $ID_Table)

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

    GUISetState()

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

    While 1
    Sleep(100)
    WEnd

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

    Func _end()
    Exit
    EndFunc

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

    Func _PrimaryUp()
    Local $hTVItem = _GUICtrlTreeView_GetSelection($hTV)
    If _GUICtrlTreeView_GetParentHandle($hTV, $hTVItem) <> $ID_Table Then Return
    ConsoleWrite(_GUICtrlTreeView_GetText($hTV,$hTVItem) & @CRLF)
    EndFunc

    [/autoit]
  • Ich "entkopple" so was gerne über einen Dummy. Dann sollte es besser gehen :)

    [autoit]

    Global $WM_NOTIFY_DUMMY = GUICtrlCreateDummy()
    GUICtrlSetOnEvent($WM_NOTIFY_DUMMY,"click_TV")
    ...
    Case $NM_CLICK
    GUICtrlSendToDummy($WM_NOTIFY_DUMMY)

    [/autoit]

    //Edit zusätzlich kannst du auch mal TVN_SELCHANGED testen

  • Hi

    Ich steh grad vor dem selben Problem!
    Wie war das jetzt mit dem Dummy? Das kapier ich irgendwie nicht...

    thx
    E

    EDIT:
    Hab grad rausgefunden, daß es perfekt (für mich) funktioniert, wenn ich $NM_CLICK durch $TVN_SELCHANGED ersetzte!
    Also auch für mich : gelöst ;)

  • Die Methode mit $TVN_SELCHANGED funktioniert allerdings nur, wenn man den TreeView mit

    [autoit]

    _GUICtrlTreeView_Create

    [/autoit]


    statt

    [autoit]

    GUICtrlCreateTreeView

    [/autoit]


    erstellt

    Hier mal BugFix´s Beispiel als Beispiel:

    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>
    #include <GuiTreeView.au3>
    #include <TreeViewConstants.au3>
    #include <WindowsConstants.au3>

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

    Opt("MustDeclareVars", 1)
    Opt("GuiOnEventMode", 1)

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

    Global $gui, $hImage, $hTV, $aID_Sub[5], $ID_Table

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

    $gui = GUICreate('Test TreeView')
    GUISetOnEvent($GUI_EVENT_CLOSE, '_end')
    $hTV = _GUICtrlTreeView_Create($gui,10,10,300,350,BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, _
    $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)

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

    $hImage = _GUIImageList_Create(16, 16, 5, 3)
    _GUIImageList_AddIcon($hImage, "shell32.dll", 98)
    _GUIImageList_AddIcon($hImage, "shell32.dll", 96)
    _GUIImageList_AddIcon($hImage, "shell32.dll", 36)
    _GUICtrlTreeView_SetNormalImageList($hTV, $hImage)

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

    _GUICtrlTreeView_BeginUpdate($hTV)
    $ID_Table = _GUICtrlTreeView_Add($hTV, 0, 'Table', 0, 0)
    For $i = 1 To 5
    $aID_Sub[$i-1] = _GUICtrlTreeView_AddChild($hTV, $ID_Table, 'Sub' & $i, 1, 1)
    ;GUICtrlSetOnEvent($aID_Sub[$i-1], 'click_TV') ; does'nt work (because have no ID and handle instead ?)
    For $j = 1 To 3
    _GUICtrlTreeView_AddChild($hTV, $aID_Sub[$i-1], 'Sub_Sub' & $j, 2, 2)
    Next
    Next
    _GUICtrlTreeView_EndUpdate($hTV)
    _GUICtrlTreeView_Expand($hTV, $ID_Table)

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

    GUISetState()
    GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY') ; by using WM_NOTIFY are strange results, are reading with an offset of 1 click!

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

    While 1
    Sleep(100)
    WEnd

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

    Func _end()
    Exit
    EndFunc

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

    Func click_TV()
    ConsoleWrite(_GUICtrlTreeView_GetText($hTV, _GUICtrlTreeView_GetSelection($hTV)) & @CRLF)
    EndFunc

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

    Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
    $hWndTreeview = $hTV
    If Not IsHWnd($hTV) Then $hWndTreeview = GUICtrlGetHandle($hTV)

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

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
    Case $hWndTreeview
    Switch $iCode
    Case $TVN_SELCHANGED
    click_TV()
    EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
    EndFunc ;==>WM_NOTIFY

    [/autoit]

    lgE

    • Offizieller Beitrag

    Wie war das jetzt mit dem Dummy? Das kapier ich irgendwie nicht...


    Hier mal die Lösung mit Dummy:

    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>
    #include <GuiTreeView.au3>
    #include <TreeViewConstants.au3>
    #include <WindowsConstants.au3>

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

    Opt("MustDeclareVars", 1)
    Opt("GuiOnEventMode", 1)

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

    Global $gui, $hImage, $hTV, $aID_Sub[5], $ID_Table

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

    $gui = GUICreate('Test TreeView')
    GUISetOnEvent($GUI_EVENT_CLOSE, '_end')
    $hTV = GUICtrlCreateTreeView(10,10,300,350,BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, _
    $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
    Global $WM_NOTIFY_DUMMY = GUICtrlCreateDummy()
    GUICtrlSetOnEvent($WM_NOTIFY_DUMMY, "TreeViewClick")

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

    $hImage = _GUIImageList_Create(16, 16, 5, 3)
    _GUIImageList_AddIcon($hImage, "shell32.dll", 98)
    _GUIImageList_AddIcon($hImage, "shell32.dll", 96)
    _GUIImageList_AddIcon($hImage, "shell32.dll", 36)
    _GUICtrlTreeView_SetNormalImageList($hTV, $hImage)

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

    _GUICtrlTreeView_BeginUpdate($hTV)
    $ID_Table = _GUICtrlTreeView_Add($hTV, 0, 'Table', 0, 0)
    For $i = 1 To 5
    $aID_Sub[$i-1] = _GUICtrlTreeView_AddChild($hTV, $ID_Table, 'Sub' & $i, 1, 1)
    For $j = 1 To 3
    _GUICtrlTreeView_AddChild($hTV, $aID_Sub[$i-1], 'Sub_Sub' & $j, 2, 2)
    Next
    Next
    _GUICtrlTreeView_EndUpdate($hTV)
    _GUICtrlTreeView_Expand($hTV, $ID_Table)

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

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

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

    While 1
    Sleep(100)
    WEnd

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

    Func _end()
    Exit
    EndFunc

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

    Func TreeViewClick()
    Local $hTVItem = _GUICtrlTreeView_GetSelection($hTV)
    MsgBox(0, 'Item - Text', _GUICtrlTreeView_GetText($hTV, $hTVItem) )
    EndFunc

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

    Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
    $hWndTreeview = $hTV
    If Not IsHWnd($hWndTreeview) Then $hWndTreeview = GUICtrlGetHandle($hTV)

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

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    If ($hWndFrom = $hWndTreeview) And ($iCode = $NM_CLICK) Then GUICtrlSendToDummy($WM_NOTIFY_DUMMY)
    Return $GUI_RUNDEFMSG
    EndFunc ;==>WM_NOTIFY

    [/autoit]