Jump to content

TreeView: hot put focus when (un)mark a TreeView's box


Luigi
 Share

Go to solution Solved by Danyfirex,

Recommended Posts

Hi forum,

In this little script has a TreeView with checkbox.

If you click in parent item or child item, they put a focus over item (occurs as expected).

But, when you click in checkbox, to mark or unmark a parent or child, the focus is not repositioned to it.

How do this?

I tried, but could not until now, someone can help me?

This is a script...

#AutoIt3Wrapper_AU3Check_Parameters= -q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7
#Tidy_Parameters=/sf

#include-once
#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>

OnAutoItExitRegister("_EXIT_BEFORE")

Opt('GUIOnEventMode', 1)
Opt('GUIEventOptions', 1)
Opt('MustDeclareVars', 1)

Global Const $bDEBUG = True
Global $aGuiSize[2] = [320, 250]
Global $sGuiTitle = "GuiTitle"
Global $hGui
Global $hButton_Read
Global $hTreeView, $hStyle, $hExStyle
Global $oItem = ObjCreate("Scripting.Dictionary")
$oItem.Add("color", ObjCreate("Scripting.Dictionary"))
$oItem.Item("color").Add("red", 0)
$oItem.Item("color").Add("blue", 1)
$oItem.Item("color").Add("green", 2)
$oItem.Item("color").Add("white", 3)
$oItem.Add("computer", ObjCreate("Scripting.Dictionary"))
$oItem.Item("computer").Add("keyboard", 0)
$oItem.Item("computer").Add("mouose", 1)
$oItem.Add("number", ObjCreate("Scripting.Dictionary"))
$oItem.Item("number").Add("one", 1)
$oItem.Item("number").Add("two", 1)
$oItem.Item("number").Add("tree", 1)

$hGui = GUICreate($sGuiTitle, $aGuiSize[0], $aGuiSize[1])
GUISetOnEvent($GUI_EVENT_CLOSE, '_EXIT')
$hButton_Read = GUICtrlCreateButton("read", 10, 220, 80, 20)
GUICtrlSetOnEvent($hButton_Read, "read")

$hStyle = BitOR($TVS_CHECKBOXES, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_SHOWSELALWAYS, $TVS_FULLROWSELECT, $TVS_TRACKSELECT, $TVS_NOTOOLTIPS, $TVS_DISABLEDRAGDROP)
$hExStyle = Default
;~  $WS_EX_CLIENTEDGE - Do not use, add more 2 pixels

$hTreeView = GUICtrlCreateTreeView(10, 10, 300, 200, $hStyle, $hExStyle)

Local $iNext = 1
For $each In $oItem.keys
    Local $parent = GUICtrlCreateTreeViewItem($each, $hTreeView)
    _GUICtrlTreeView_SetItemParam($hTreeView, $parent, $iNext)
    $iNext += 1
    For $subitem In $oItem.Item($each).keys
        Local $child = GUICtrlCreateTreeViewItem($subitem, $parent)
        _GUICtrlTreeView_SetItemParam($hTreeView, $child, $iNext)
        $iNext += 1
    Next
    GUICtrlSetState($parent, BitOR($GUI_EXPAND, $GUI_DEFBUTTON))
Next

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

GUISetState(@SW_SHOW, $hGui)

While Sleep(10)
WEnd

Func __IsParent($hTreeView)
    Return _GUICtrlTreeView_GetParentHandle($hTreeView)
EndFunc   ;==>__IsParent

Func __Parent_Mark($hTreeView, $iItem)
    ConsoleWrite("__Parent_Mark( $hTreeView=" & $hTreeView & ", $iItem=" & $iItem & " )" & @LF)

EndFunc   ;==>__Parent_Mark

Func _DebugPrint($s_Text, $sLine = @ScriptLineNumber)
    ConsoleWrite( _
            "!===========================================================" & @CRLF & _
            "+======================================================" & @CRLF & _
            "-->Line(" & StringFormat("%04d", $sLine) & "):" & @TAB & $s_Text & @CRLF & _
            "+======================================================" & @CRLF)
EndFunc   ;==>_DebugPrint

Func _EXIT()
    Exit
EndFunc   ;==>_EXIT

Func _EXIT_BEFORE($sInput = 0)
    If $bDEBUG Then ConsoleWrite("#Func _exit( $sInput=" & (IsDeclared("sInput") ? $sInput : "Null") & " )" & @LF)
    GUIDelete($hGui)
EndFunc   ;==>_EXIT_BEFORE

Func read()
    ConsoleWrite("read" & @LF)
    Local $parent = _GUICtrlTreeView_GetFirstVisible($hTreeView)
    Do
        ConsoleWrite("+$parent[ " & $parent & " ] mark[" & (_GUICtrlTreeView_GetChecked($hTreeView, $parent) ? "x" : " ") & "] text[ " & _GUICtrlTreeView_GetText($hTreeView, $parent) & " ]" & @LF)
        Local $child = _GUICtrlTreeView_GetFirstChild($hTreeView, $parent)

        Do
            ConsoleWrite("  $child[ " & $child & " ] mark[" & (_GUICtrlTreeView_GetChecked($hTreeView, $child) ? "x" : " ") & "] text[ " & _GUICtrlTreeView_GetText($hTreeView, $child) & " ]" & @LF)
            $child = _GUICtrlTreeView_GetNextSibling($hTreeView, $child)
        Until $child = 0
        $parent = _GUICtrlTreeView_GetNextSibling($hTreeView, $parent)
    Until $parent = 0
EndFunc   ;==>read

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
    $hWndTreeview = $hTreeView
    If Not IsHWnd($hTreeView) Then $hWndTreeview = GUICtrlGetHandle($hTreeView)

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndTreeview
            Local $tInfo = DllStructCreate($tagNMMOUSE, $lParam)

            Local $tPOINT, $iXX, $iYY, $aPos, $iItem
            Local Static $CLICK_TREE_VIEW = False

            Switch $iCode
                Case $NM_CLICK ; The user has clicked the left mouse button within the control
                    $tPOINT = _WinAPI_GetMousePos(True, $hGui)
                    $iXX = DllStructGetData($tPOINT, "X")
                    $iYY = DllStructGetData($tPOINT, "Y")
                    $aPos = ControlGetPos($hGui, "", $hTreeView)
                    $iItem = _GUICtrlTreeView_HitTestItem($hTreeView, $iXX - $aPos[0], $iYY - $aPos[1])
                    ConsoleWrite("normal clique[ " & $iItem & " ]" & @LF)
                    $CLICK_TREE_VIEW = True
                    ; Return 1 ; nonzero to not allow the default processing
                    Return 0 ; zero to allow the default processing
                Case $NM_DBLCLK ; The user has double-clicked the left mouse button within the control
                    $tPOINT = _WinAPI_GetMousePos(True, $hGui)
                    $iXX = DllStructGetData($tPOINT, "X")
                    $iYY = DllStructGetData($tPOINT, "Y")
                    $aPos = ControlGetPos($hGui, "", $hTreeView)
                    $iItem = _GUICtrlTreeView_HitTestItem($hTreeView, $iXX - $aPos[0], $iYY - $aPos[1])
                    _DebugPrint("$NM_DBLCLK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ConsoleWrite("clique duplo [ " & $iItem & " ]" & @LF)
;~                  Return 1 ; nonzero to not allow the default processing
                    Return 0 ; zero to allow the default processing
                Case $NM_SETCURSOR ; control is setting the cursor in response to a WM_SETCURSOR message
;~                  Local $tInfo = DllStructCreate($tagNMMOUSE, $lParam)
                    $hWndFrom = HWnd(DllStructGetData($tInfo, "hWndFrom"))
                    $iIDFrom = DllStructGetData($tInfo, "IDFrom")
                    $iCode = DllStructGetData($tInfo, "Code")

                    If $CLICK_TREE_VIEW Then
                        $iItem = DllStructGetData($tInfo, "ItemSpec")
                                                _GUICtrlTreeView_SelectItem ( $hTreeView, $iItem)
                        __Parent_Mark($hTreeView, $iItem)
                        ConsoleWrite("seleção      [" & (_GUICtrlTreeView_GetChecked($hTreeView, $iItem) ? "x" : " ") & "][ " & _GUICtrlTreeView_GetText($hTreeView, $iItem) & " ] IsParent[ " & __IsParent($hTreeView) & " ]" & @LF)
                        $CLICK_TREE_VIEW = False
                    EndIf
                    Return 0 ; to enable the control to set the cursor
                    ; Return 1 ; nonzero to prevent the control from setting the cursor
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
Edited by Detefon

Visit my repository

Link to comment
Share on other sites

Sorry... solved...

Just put: _GUICtrlTreeView_SelectItem ( $hTreeView, $iItem) at line 172

If $CLICK_TREE_VIEW Then
                        $iItem = DllStructGetData($tInfo, "ItemSpec")
                        $focus = _GUICtrlTreeView_GetFocused($hTreeView, $iItem)
                        _GUICtrlTreeView_SelectItem ( $hTreeView, $iItem)

                        ConsoleWrite("hasFocus[ " & _GUICtrlTreeView_SetFocused($hTreeView, $iItem) & " ]" & @LF)
                        ConsoleWrite("teste normal[" & (_GUICtrlTreeView_GetChecked($hTreeView, $iItem) ? "x" : " ") & "][ " & _GUICtrlTreeView_GetText($hTreeView, $iItem) & " ] focus[ " & $focus & " ]" & @LF)
                        $CLICK_TREE_VIEW = False
                    EndIf
Edited by Detefon

Visit my repository

Link to comment
Share on other sites

  • Solution

 ups late.

_GUICtrlTreeView_SelectItem($hTreeView, $iItem )

#AutoIt3Wrapper_AU3Check_Parameters= -q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7
#Tidy_Parameters=/sf

#include-once
#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>

OnAutoItExitRegister("_EXIT_BEFORE")

Opt('GUIOnEventMode', 1)
Opt('GUIEventOptions', 1)
Opt('MustDeclareVars', 1)

Global Const $bDEBUG = True
Global $aGuiSize[2] = [320, 250]
Global $sGuiTitle = "GuiTitle"
Global $hGui
Global $hButton_Read
Global $hTreeView, $hStyle, $hExStyle
Global $oItem = ObjCreate("Scripting.Dictionary")
$oItem.Add("color", ObjCreate("Scripting.Dictionary"))
$oItem.Item("color").Add("red", 0)
$oItem.Item("color").Add("blue", 1)
$oItem.Item("color").Add("green", 2)
$oItem.Item("color").Add("white", 3)
$oItem.Add("computer", ObjCreate("Scripting.Dictionary"))
$oItem.Item("computer").Add("keyboard", 0)
$oItem.Item("computer").Add("mouose", 1)
$oItem.Add("number", ObjCreate("Scripting.Dictionary"))
$oItem.Item("number").Add("one", 1)
$oItem.Item("number").Add("two", 1)
$oItem.Item("number").Add("tree", 1)

$hGui = GUICreate($sGuiTitle, $aGuiSize[0], $aGuiSize[1])
GUISetOnEvent($GUI_EVENT_CLOSE, '_EXIT')
$hButton_Read = GUICtrlCreateButton("read", 10, 220, 80, 20)
GUICtrlSetOnEvent($hButton_Read, "read")

$hStyle = BitOR($TVS_CHECKBOXES, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_SHOWSELALWAYS, $TVS_FULLROWSELECT, $TVS_TRACKSELECT, $TVS_NOTOOLTIPS, $TVS_DISABLEDRAGDROP)
$hExStyle = Default
;~  $WS_EX_CLIENTEDGE - Do not use, add more 2 pixels

$hTreeView = GUICtrlCreateTreeView(10, 10, 300, 200, $hStyle, $hExStyle)

Local $iNext = 1
For $each In $oItem.keys
    Local $parent = GUICtrlCreateTreeViewItem($each, $hTreeView)
    _GUICtrlTreeView_SetItemParam($hTreeView, $parent, $iNext)
    $iNext += 1
    For $subitem In $oItem.Item($each).keys
        Local $child = GUICtrlCreateTreeViewItem($subitem, $parent)
        _GUICtrlTreeView_SetItemParam($hTreeView, $child, $iNext)
        $iNext += 1
    Next
    GUICtrlSetState($parent, BitOR($GUI_EXPAND, $GUI_DEFBUTTON))
Next

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

GUISetState(@SW_SHOW, $hGui)

While Sleep(10)
WEnd

Func __IsParent($hTreeView)
    Return _GUICtrlTreeView_GetParentHandle($hTreeView)
EndFunc   ;==>__IsParent

Func __Parent_Mark($hTreeView, $iItem)
    ConsoleWrite("__Parent_Mark( $hTreeView=" & $hTreeView & ", $iItem=" & $iItem & " )" & @LF)

EndFunc   ;==>__Parent_Mark

Func _DebugPrint($s_Text, $sLine = @ScriptLineNumber)
    ConsoleWrite( _
            "!===========================================================" & @CRLF & _
            "+======================================================" & @CRLF & _
            "-->Line(" & StringFormat("%04d", $sLine) & "):" & @TAB & $s_Text & @CRLF & _
            "+======================================================" & @CRLF)
EndFunc   ;==>_DebugPrint

Func _EXIT()
    Exit
EndFunc   ;==>_EXIT

Func _EXIT_BEFORE($sInput = 0)
    If $bDEBUG Then ConsoleWrite("#Func _exit( $sInput=" & (IsDeclared("sInput") ? $sInput : "Null") & " )" & @LF)
    GUIDelete($hGui)
EndFunc   ;==>_EXIT_BEFORE

Func read()
    ConsoleWrite("read" & @LF)
    Local $parent = _GUICtrlTreeView_GetFirstVisible($hTreeView)
    Do
        ConsoleWrite("+$parent[ " & $parent & " ] mark[" & (_GUICtrlTreeView_GetChecked($hTreeView, $parent) ? "x" : " ") & "] text[ " & _GUICtrlTreeView_GetText($hTreeView, $parent) & " ]" & @LF)
        Local $child = _GUICtrlTreeView_GetFirstChild($hTreeView, $parent)

        Do
            ConsoleWrite("  $child[ " & $child & " ] mark[" & (_GUICtrlTreeView_GetChecked($hTreeView, $child) ? "x" : " ") & "] text[ " & _GUICtrlTreeView_GetText($hTreeView, $child) & " ]" & @LF)
            $child = _GUICtrlTreeView_GetNextSibling($hTreeView, $child)
        Until $child = 0
        $parent = _GUICtrlTreeView_GetNextSibling($hTreeView, $parent)
    Until $parent = 0
EndFunc   ;==>read

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
    $hWndTreeview = $hTreeView
    If Not IsHWnd($hTreeView) Then $hWndTreeview = GUICtrlGetHandle($hTreeView)

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndTreeview
            Local $tInfo = DllStructCreate($tagNMMOUSE, $lParam)

            Local $tPOINT, $iXX, $iYY, $aPos, $iItem
            Local Static $CLICK_TREE_VIEW = False

            Switch $iCode
                Case $NM_CLICK ; The user has clicked the left mouse button within the control
                    $tPOINT = _WinAPI_GetMousePos(True, $hGui)
                    $iXX = DllStructGetData($tPOINT, "X")
                    $iYY = DllStructGetData($tPOINT, "Y")
                    $aPos = ControlGetPos($hGui, "", $hTreeView)
                    $iItem = _GUICtrlTreeView_HitTestItem($hTreeView, $iXX - $aPos[0], $iYY - $aPos[1])
                    ConsoleWrite("normal clique[ " & $iItem & " ]" & @LF)
                    $CLICK_TREE_VIEW = True
                     _GUICtrlTreeView_SelectItem($hTreeView, $iItem )
                     ConsoleWrite("!test" & @CRLF)
                    ; Return 1 ; nonzero to not allow the default processing
                    Return 0 ; zero to allow the default processing
                Case $NM_DBLCLK ; The user has double-clicked the left mouse button within the control
                    $tPOINT = _WinAPI_GetMousePos(True, $hGui)
                    $iXX = DllStructGetData($tPOINT, "X")
                    $iYY = DllStructGetData($tPOINT, "Y")
                    $aPos = ControlGetPos($hGui, "", $hTreeView)
                    $iItem = _GUICtrlTreeView_HitTestItem($hTreeView, $iXX - $aPos[0], $iYY - $aPos[1])
                    _DebugPrint("$NM_DBLCLK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ConsoleWrite("clique duplo [ " & $iItem & " ]" & @LF)
;~                  Return 1 ; nonzero to not allow the default processing
                    Return 0 ; zero to allow the default processing
                Case $NM_SETCURSOR ; control is setting the cursor in response to a WM_SETCURSOR message
;~                  Local $tInfo = DllStructCreate($tagNMMOUSE, $lParam)
                    $hWndFrom = HWnd(DllStructGetData($tInfo, "hWndFrom"))
                    $iIDFrom = DllStructGetData($tInfo, "IDFrom")
                    $iCode = DllStructGetData($tInfo, "Code")

                    If $CLICK_TREE_VIEW Then
                        $iItem = DllStructGetData($tInfo, "ItemSpec")
                        __Parent_Mark($hTreeView, $iItem)
                        ConsoleWrite("seleção      [" & (_GUICtrlTreeView_GetChecked($hTreeView, $iItem) ? "x" : " ") & "][ " & _GUICtrlTreeView_GetText($hTreeView, $iItem) & " ] IsParent[ " & __IsParent($hTreeView) & " ]" & @LF)
                        $CLICK_TREE_VIEW = False
                    EndIf
                    Return 0 ; to enable the control to set the cursor
                    ; Return 1 ; nonzero to prevent the control from setting the cursor
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Saludos

Edited by Danyfirex
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...