Jump to content

Unexpect behaviour when click on(select) some treeview item


Recommended Posts

Hi

I'm referencing

of the "Semi-virtual treeview", however, if I add one button in the GUI and run the script, if I click or select the treeview item, like "3" in this example, it will trigger button pressing function, how to fix it?
 

#AutoIt3Wrapper_Au3Check_Parameters=-d -w- 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

#AutoIt3Wrapper_UseX64=Y

Opt( "MustDeclareVars", 1 )

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>

Global $idTreeView, $hTreeView, $aItems

Example()

Func Example()
    ; Create GUI
    Local $hGui = GUICreate( "Create Semi-Virtual TreeView From File", 400, 400, 600, 300, $GUI_SS_DEFAULT_GUI )
    Local $open = GUICtrlCreateButton ("Open", 4, 300)

    ; Create TreeView
    $idTreeView = GUICtrlCreateTreeView( 4, 4, 392, 292, $GUI_SS_DEFAULT_TREEVIEW, $WS_EX_CLIENTEDGE )
    $hTreeView = GUICtrlGetHandle( $idTreeView )
    ; Read level and text of TreeView items
    $aItems = FileReadToArray( "TreeView.txt" )
    ; Create TreeView structure
    CreateTreeView( $aItems )
    ; Expand all child items
    _GUICtrlTreeView_Expand( $hTreeView )

    ; Register WM_NOTIFY message handler
    GUIRegisterMsg( $WM_NOTIFY, "WM_NOTIFY" )

    ; Show GUI
    GUISetState( @SW_SHOW, $hGui )

    ; Loop
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

             Case $open
                msgbox(0,"","HELLO")
        EndSwitch
    WEnd

    ; Cleanup
    GUIDelete( $hGui )
EndFunc

; Create TreeView structure
Func CreateTreeView( $aItems )
    ; TreeView level information
    Local $aLevels[100][2], $iLevel = 0, $iLevelPrev = 0
    ; $aLevels[$iLevel][0]/[1] contains the last item/parent of that level

    ; TreeView insert structure
    Local $tInsert = DllStructCreate( $tagTVINSERTSTRUCT ), $pInsert = DllStructGetPtr( $tInsert )
    DllStructSetData( $tInsert, "InsertAfter", $TVI_LAST )
    DllStructSetData( $tInsert, "Mask", $TVIF_HANDLE+$TVIF_PARAM+$TVIF_TEXT )
    DllStructSetData( $tInsert, "Text", -1 ) ; $LPSTR_TEXTCALLBACK

    ; Add TreeView root
    $aLevels[$iLevel][1] = NULL
    DllStructSetData( $tInsert, "Param", 0 )
    DllStructSetData( $tInsert, "Parent", $aLevels[$iLevel][1] )
    $aLevels[$iLevel][0] = GUICtrlSendMsg( $idTreeView, $TVM_INSERTITEMW, 0, $pInsert )

    ; Add TreeView items
    For $i = 1 To UBound( $aItems ) - 1
        $iLevel = StringSplit( $aItems[$i], "|", 2 )[0]
        If $iLevel <> $iLevelPrev Then
            $aLevels[$iLevel][1] = $iLevel > $iLevelPrev ? $aLevels[$iLevelPrev][0] _ ; A child of the previous level
                                                         : GUICtrlSendMsg( $idTreeView, $TVM_GETNEXTITEM, $TVGN_PARENT, $aLevels[$iLevel][0] ) ; A sibling of the level
            $iLevelPrev = $iLevel
        EndIf
        DllStructSetData( $tInsert, "Param", $i )
        DllStructSetData( $tInsert, "Parent", $aLevels[$iLevel][1] )
        $aLevels[$iLevel][0] = GUICtrlSendMsg( $idTreeView, $TVM_INSERTITEMW, 0, $pInsert )
    Next ; $aLevels[$iLevel][0]/[1] contains the last item/parent of that level
EndFunc

Func WM_NOTIFY( $hWnd, $iMsg, $wParam, $lParam )
    Local $tNMHDR = DllStructCreate( $tagNMHDR, $lParam )
    Switch HWnd( DllStructGetData( $tNMHDR, "hWndFrom" ) )
        Case $hTreeView
            Switch DllStructGetData( $tNMHDR, "Code" )
                Case $TVN_GETDISPINFOW ; Display TreeView item text
                    Local Static $tBuffer = DllStructCreate( "wchar Text[50]" ), $pBuffer = DllStructGetPtr( $tBuffer )
                    Local $tDispInfo = DllStructCreate( $tagNMTVDISPINFO, $lParam ), $sText = StringSplit( $aItems[DllStructGetData($tDispInfo,"Param")], "|", 2 )[1]
                    DllStructSetData( $tBuffer, "Text", $sText )
                    DllStructSetData( $tDispInfo, "Text", $pBuffer )
                    DllStructSetData( $tDispInfo, "TextMax", 2 * StringLen( $sText ) + 2 )
            EndSwitch
    EndSwitch
    #forceref $hWnd, $iMsg, $wParam
EndFunc

the "TreeView.txt" is:

 

0|0
0|1
0|2
1|3
1|This
1|is
2|6
2|a
2|very
2|nice
3|10
3|TreeView
0|, (comma)
1|13
1|indeed.
0|15

Link to post
Share on other sites

It is because you assign control IDs inside the treeview starting at 0 which is the same as the GUI.  You should start like at 1000, so the treeview does not interfere with the GUI.

Link to post
Share on other sites

Hi Nine,

I can see that the button's ID is 3, the treeview's ID is 4, but it seems that the "Func CreateTreeView( $aItems ) returns some control ID when click on their item and conflict with the ID of GUICtrlCreatexxxx.

So how to start the treeview from 1000?

Link to post
Share on other sites

The Control IDs of the treeview are assigned with the param element of the structure $tagTVINSERTSTRUCT.  Just add 1000 to it instead of starting at 0.  Likewise, subtract 1000 in the WM_Notify function.

Link to post
Share on other sites

Here :

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>

Opt("MustDeclareVars", True)

Global Const $TREEVIEW_START = 1000

Global $idTreeView, $hTreeView, $aItems

Example()

Func Example()
  ; Create GUI
  Local $hGui = GUICreate("Create Semi-Virtual TreeView From File", 400, 400, 600, 300, $GUI_SS_DEFAULT_GUI)
  Local $open = GUICtrlCreateButton("Open", 4, 300)

  ; Create TreeView
  $idTreeView = GUICtrlCreateTreeView(4, 4, 392, 292, $GUI_SS_DEFAULT_TREEVIEW, $WS_EX_CLIENTEDGE)
  $hTreeView = GUICtrlGetHandle($idTreeView)
  ; Read level and text of TreeView items
  $aItems = FileReadToArray("TreeView.txt")
  ; Create TreeView structure
  CreateTreeView($aItems)
  ; Expand all child items
  _GUICtrlTreeView_Expand($hTreeView)

  ; Register WM_NOTIFY message handler
  GUIRegisterMsg($WM_NOTIFY, WM_NOTIFY)

  ; Show GUI
  GUISetState(@SW_SHOW, $hGui)

  ; Loop
  While 1
    Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
        ExitLoop

      Case $open
        MsgBox(0, "", "HELLO")
    EndSwitch
  WEnd

  ; Cleanup
  GUIDelete($hGui)
EndFunc   ;==>Example

; Create TreeView structure
Func CreateTreeView($aItems)
  ; TreeView level information
  Local $aLevels[100][2], $iLevel = 0, $iLevelPrev = 0
  ; $aLevels[$iLevel][0]/[1] contains the last item/parent of that level

  ; TreeView insert structure
  Local $tInsert = DllStructCreate($tagTVINSERTSTRUCT), $pInsert = DllStructGetPtr($tInsert)
  DllStructSetData($tInsert, "InsertAfter", $TVI_LAST)
  DllStructSetData($tInsert, "Mask", $TVIF_HANDLE + $TVIF_PARAM + $TVIF_TEXT)
  DllStructSetData($tInsert, "Text", -1)     ; $LPSTR_TEXTCALLBACK

  ; Add TreeView root
  $aLevels[$iLevel][1] = Null
  DllStructSetData($tInsert, "Param", $TREEVIEW_START)
  DllStructSetData($tInsert, "Parent", $aLevels[$iLevel][1])
  $aLevels[$iLevel][0] = GUICtrlSendMsg($idTreeView, $TVM_INSERTITEMW, 0, $pInsert)

  ; Add TreeView items
  For $i = 1 To UBound($aItems) - 1
    $iLevel = StringSplit($aItems[$i], "|", 2)[0]
    If $iLevel <> $iLevelPrev Then
      $aLevels[$iLevel][1] = $iLevel > $iLevelPrev ? $aLevels[$iLevelPrev][0] _       ; A child of the previous level
           : GUICtrlSendMsg($idTreeView, $TVM_GETNEXTITEM, $TVGN_PARENT, $aLevels[$iLevel][0])                                                 ; A sibling of the level
      $iLevelPrev = $iLevel
    EndIf
    DllStructSetData($tInsert, "Param", $TREEVIEW_START + $i)
    DllStructSetData($tInsert, "Parent", $aLevels[$iLevel][1])
    $aLevels[$iLevel][0] = GUICtrlSendMsg($idTreeView, $TVM_INSERTITEMW, 0, $pInsert)
  Next   ; $aLevels[$iLevel][0]/[1] contains the last item/parent of that level
EndFunc   ;==>CreateTreeView

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
  Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
  Switch HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    Case $hTreeView
      Switch DllStructGetData($tNMHDR, "Code")
        Case $TVN_GETDISPINFOW         ; Display TreeView item text
          Local Static $tBuffer = DllStructCreate("wchar Text[50]"), $pBuffer = DllStructGetPtr($tBuffer)
          Local $tDispInfo = DllStructCreate($tagNMTVDISPINFO, $lParam), $sText = StringSplit($aItems[DllStructGetData($tDispInfo, "Param") - $TREEVIEW_START], "|", 2)[1]
          DllStructSetData($tBuffer, "Text", $sText)
          DllStructSetData($tDispInfo, "Text", $pBuffer)
          DllStructSetData($tDispInfo, "TextMax", 2 * StringLen($sText) + 2)
      EndSwitch
  EndSwitch
  Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

 

Edited by Nine
Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...