Jump to content

Small sample for getting TreeView-Path


Holger
 Share

Recommended Posts

Hi :)

so here is my first step into "DllCreateStruct" :D

With this you see how to get i.e. the complete-tree-structure or path of the current selected treeview-item in a _GUI-TreeView_.

Feel free to use and modify it :evil:

Newest Autoit3-Beta is needed.

; ----------------------------------------------------------------------------
;
; AutoIt Version: 3.1.1.48
; Author:        Holger Kotsch
;
; Script Function:
;   Gets the treeview-structure-text (i.e.path) for the current selected
;   treeview item
;
; ----------------------------------------------------------------------------

#include <GUIConstants.au3>
Opt("GUIDataSeparatorChar","\")


;---------------------------------------------------------------
; Some additional constants
;---------------------------------------------------------------

Global $TV_FIRST        = 0x1100
Global $TVM_GETITEM     = $TV_FIRST + 12
Global $TVM_GETNEXTITEM = $TV_FIRST + 10
Global $TVGN_CARET      = 0x0009
Global $TVGN_PARENT     = 0x0003
Global $TVIF_TEXT       =      0x0001


;---------------------------------------------------------------
; Main sample program
;---------------------------------------------------------------

$szDataSep  = Opt("GUIDataSeparatorChar")

$hGUI       = GUICreate("TreeTest")

$nTree      = GUICtrlCreateTreeView(10,10,150,150)
$hTree      = ControlGetHandle($hGUI,"",$nTree); Later important for getting item information
$nItem1     = GUICtrlCreateTreeViewItem("Item1",$nTree)
$nItem2     = GUICtrlCreateTreeViewItem("Item2",$nTree)
$nSubItem1  = GUICtrlCreateTreeViewItem("SubItem1",$nItem1)
$nSubItem2  = GUICtrlCreateTreeViewItem("SubItem2",$nItem1)
$nSubItem3  = GUICtrlCreateTreeViewItem("SubItem3",$nSubItem1)

$nButton    = GUICtrlCreateButton("Path?",70,170,70,20)

GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $nMsg = $nButton
            Msgbox(0,"Path",_GUICtrlTreeViewItemGetTree())
    EndSelect
WEnd

Exit



;---------------------------------------------------------------
; Get all items text beginning by the current selected item
;---------------------------------------------------------------

Func _GUICtrlTreeViewItemGetTree()
    $szPath = ""
    $hItem = GUICtrlSendMsg($nTree,$TVM_GETNEXTITEM,$TVGN_CARET,0)
    If $hItem > 0 Then
        $szPath = TreeViewGetItemText($hItem)
        Do; Get now the parent item handle if there is one
            $hParent = GUICtrlSendMsg($nTree,$TVM_GETNEXTITEM,$TVGN_PARENT,$hItem)
            If $hParent > 0 Then $szPath = TreeViewGetItemText($hParent) & $szDataSep & $szPath 
            $hItem = $hParent
        Until $hItem <= 0
    EndIf
    Return $szPath
EndFunc


;---------------------------------------------------------------
; Retrieve the item text by sending a right item handle
;---------------------------------------------------------------

Func TreeViewGetItemText($hItem)
    $szText         = ""
    $TEXT_struct    = DllStructCreate("char[260]"); create a text 'area' for receiving the text
    $TVITEM_struct  = DllStructCreate("uint;int;uint;uint;ptr;int;int;int;int;int")
    
    DllStructSetData($TVITEM_struct,1,$TVIF_TEXT)
    DllStructSetData($TVITEM_struct,2,$hItem)
    DllStructSetData($TVITEM_struct,5,DllStructGetPtr($TEXT_struct))
    DllStructSetData($TVITEM_struct,6,260)
    
    DllCall("user32.dll","int","SendMessage","hwnd",$hTree,"int",$TVM_GETITEM,"int",0,"ptr",DllStructGetPtr($TVITEM_struct))
    $szText = DllStructGetData($TEXT_struct,1)
    DllStructDelete($TEXT_struct)
    DllStructDelete($TVITEM_struct)

    Return $szText
EndFunc

Regards

Holger

Edited by Holger
Link to comment
Share on other sites

Just edited the func's a bit for my personal use, maybe someone can use this:

Test script:

#include <GUIConstants.au3>
#include <GUITreeView.au3>

$hGUI = GUICreate("TreeTest")

$TreeView  = GUICtrlCreateTreeView(10,10,150,150)
$Handle = ControlGetHandle($hGUI,"",$TreeView)
$nItem1 = GUICtrlCreateTreeViewItem("Item1",$TreeView)
$nItem2 = GUICtrlCreateTreeViewItem("Item2",$TreeView)
$nSubItem1 = GUICtrlCreateTreeViewItem("SubItem1",$nItem1)
$nSubItem2 = GUICtrlCreateTreeViewItem("SubItem2",$nItem1)
$nSubItem3 = GUICtrlCreateTreeViewItem("SubItem3",$nSubItem1)

$nButton = GUICtrlCreateButton("Path?",70,170,70,20)

GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $nMsg = $nButton
            Msgbox(0,"Path",_GUICtrlTreeViewItemGetTree($TreeView,$Handle))
    EndSelect
WEnd
Exit

"GUITreeView.au3" Include file

Global $TV_FIRST        = 0x1100
Global $TVM_GETITEM     = $TV_FIRST + 12
Global $TVM_GETNEXTITEM = $TV_FIRST + 10
Global $TVGN_CARET      = 0x0009
Global $TVGN_PARENT     = 0x0003
Global $TVIF_TEXT       =      0x0001


;---------------------------------------------------------------
; Get all items text beginning by the current selected item
;---------------------------------------------------------------

Func _GUICtrlTreeViewItemGetTree($nTree,$hTree)
    $szPath = ""
    $hItem = GUICtrlSendMsg($nTree,$TVM_GETNEXTITEM,$TVGN_CARET,0)
    If $hItem > 0 Then
        $szPath = TreeViewGetItemText($hItem,$hTree)
        Do; Get now the parent item handle if there is one
            $hParent = GUICtrlSendMsg($nTree,$TVM_GETNEXTITEM,$TVGN_PARENT,$hItem)
            If $hParent > 0 Then $szPath = TreeViewGetItemText($hParent,$hTree) & "\" & $szPath 
            $hItem = $hParent
        Until $hItem <= 0
    EndIf
    Return $szPath
EndFunc


;---------------------------------------------------------------
; Retrieve the item text by sending a right item handle
;---------------------------------------------------------------

Func TreeViewGetItemText($hItem,$hTree)
    $szText         = ""
    $TEXT_struct    = DllStructCreate("char[260]"); create a text 'area' for receiving the text
    $TVITEM_struct  = DllStructCreate("uint;int;uint;uint;ptr;int;int;int;int;int")
    
    DllStructSetData($TVITEM_struct,1,$TVIF_TEXT)
    DllStructSetData($TVITEM_struct,2,$hItem)
    DllStructSetData($TVITEM_struct,5,DllStructGetPtr($TEXT_struct))
    DllStructSetData($TVITEM_struct,6,260)
    
    DllCall("user32.dll","int","SendMessage","hwnd",$hTree,"int",$TVM_GETITEM,"int",0,"ptr",DllStructGetPtr($TVITEM_struct))
    $szText = DllStructGetData($TEXT_struct,1)
    DllStructDelete($TEXT_struct)
    DllStructDelete($TVITEM_struct)

    Return $szText
EndFunc
Link to comment
Share on other sites

Hi

Can you help me with manipulating treeview in other application please?

It's the same as your function TreeViewGetItemText

but it doesn't work if I'm trying this on treeview inside other application.

Note that TVM_GETNEXTITEM, TVM_SELECTITEM are working OK

only TVM_GETITEM for obtain item text is not OK.

My first post about it is here:

TreeViewGetItemText from another app

Thank you

Global $TV_FIRST        = 0x1100
Global $TVM_GETITEM     = $TV_FIRST + 12
Global $TVM_GETNEXTITEM = $TV_FIRST + 10
Global $TVGN_CARET      = 0x0009
Global $TVIF_TEXT       =      0x0001

Run("treeview_app.exe");.au3
WinWaitActive("App with treeview")

$hTree = ControlGetHandle("App with treeview", "", "SysTreeView321")
;$hItem = GUICtrlSendMsg($nTree,$TVM_GETNEXTITEM,$TVGN_CARET,0)
$result = dllcall("user32.dll","int","SendMessage","hWnd",$hTree,"int",$TVM_GETNEXTITEM,"int",$TVGN_CARET,"int",0)
;Msgbox(0,"hItem",$result[0]); it's OK

$sItemText = TreeViewGetItemText($result[0]); it's not OK
Msgbox(0,"Item text",$sItemText)

Exit

Func TreeViewGetItemText($hItem)
    $szText         = ""
    $TEXT_struct    = DllStructCreate("char[260]"); create a text 'area' for receiving the text
    $TVITEM_struct  = DllStructCreate("uint;int;uint;uint;ptr;int;int;int;int;int")
    
    DllStructSetData($TVITEM_struct,1,$TVIF_TEXT)
    DllStructSetData($TVITEM_struct,2,$hItem)
    DllStructSetData($TVITEM_struct,5,DllStructGetPtr($TEXT_struct))
    DllStructSetData($TVITEM_struct,6,260)
    
    DllCall("user32.dll","int","SendMessage","hwnd",$hTree,"int",$TVM_GETITEM,"int",0,"ptr",DllStructGetPtr($TVITEM_struct))
    $szText = DllStructGetData($TEXT_struct,1)
    DllStructDelete($TEXT_struct)
    DllStructDelete($TVITEM_struct)

    Return $szText
EndFunc
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...