treeview how to use it . Need help

Windows specific questions.
Post Reply
aloberoger
Posts: 507
Joined: Jan 13, 2009 19:23

treeview how to use it . Need help

Post by aloberoger »

since Iam Not familiar With Treeview
I want To know how To retrieve:
- the current selected item
- edit the current selected item
- Swap two items of a treeview
- drag item of a treeview

heare is an example
tvex.bi

Code: Select all

FUNCTION MainWnd_InitDialog(BYVAL hDlg AS HWND) AS LRESULT
    'ghImgList=ImageList_Create(ghInst,16,16,ILC_COLOR8,10,10)  ' create imagelist

    '  ImageList_Add(ghImgList,bmpAPI16)  ' add icons
    '  ImageList_Add(ghImgList,bmpTvwOpen16)
    '  TreeView_SetImageList(GetDlgItem(hDlg,trvMain),ghImgList,TVSIL_NORMAL) ' attach image list to treeview control
  Return 0
END FUNCTION
 
FUNCTION MainWnd_trvMain_NmClick(BYVAL hDlg AS HWND, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
 ' TreeView_EditLabel(GetDlgItem(hDlg,trvMain),hitem)
  
   messagebox hDlg,"","ok",0
 Return 0
END FUNCTION

 
FUNCTION MainWnd_trvMain_NmDblclk(BYVAL hDlg AS HWND, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

  Return 0
END FUNCTION

 
FUNCTION MainWnd_trvMain_NmSetfocus(BYVAL hDlg AS HWND, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

  Return 0
END FUNCTION

 
FUNCTION MainWnd_trvMain_TvnSelchanged(BYVAL hDlg AS HWND, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

  Return 0
END FUNCTION

 
FUNCTION MainWnd_trvMain_TvnSelchanging(BYVAL hDlg AS HWND, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

  Return 0
END FUNCTION

 
FUNCTION MainWnd_spnIconID_UdnDeltapos(BYVAL hDlg AS HWND, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

  Return 0
END FUNCTION

 
FUNCTION MainWnd_txtIconID_EnChange(BYVAL hDlg AS HWND, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

  Return 0
END FUNCTION

 
FUNCTION MainWnd_txtIconID_EnUpdate(BYVAL hDlg AS HWND, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

  Return 0
END FUNCTION

 
FUNCTION MainWnd_cmdAddItem_BnClicked(BYVAL hDlg AS HWND, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
   Dim  tInsert AS TV_INSERTSTRUCT    ' structure needed to insert a new item
    Dim tItem AS TV_ITEM              ' treeview item

  Dim As HWND  hTrv=GetDlgItem(hDlg,trvMain)
  Dim As HTREEITEM  hti=TreeView_GetSelection(hTrv)   ' get current selection = parent item
    IF hti THEN                        ' make current item parent item
        ' the mask item specifies which properties / members in the UDT should be set
        tItem.mask=TVIF_CHILDREN OR TVIF_HANDLE   ' only set cchildren member
        tItem.hItem=hti
        tItem.cchildren=1               ' means that item will contain child items
          TreeView_SetItem(hTrv,@tItem)
    END IF

    tInsert.hParent=hti                ' fill "insert item" structure
    tInsert.hInsertAfter=TVI_SORT
    ' the "item" member is the item which should be inserted (type = TV_ITEM)
    tInsert.Item.mask=TVIF_CHILDREN OR TVIF_HANDLE OR TVIF_IMAGE OR _
                      TVIF_SELECTEDIMAGE OR TVIF_STATE OR TVIF_TEXT
 Dim As ZString*255 a: getwindowtext(GetDlgItem(hDlg,txtItemText),StrPtr(a),255)
    tInsert.Item.pszText=STRPTR(a)     ' item text
    tInsert.Item.cchTextMax=LEN(a)
    Dim As ZString*255 b : getwindowtext(GetDlgItem(hDlg,txtIconID),StrPtr(b),255)
    Dim As Integer im=VAL(b)
    tInsert.Item.iImage=im             ' image ID
    tInsert.Item.iSelectedImage=im     ' selected image = normal image
    tInsert.Item.cChildren=0            ' new item currently does not have children

  Dim As HTREEITEM  h=TreeView_InsertItem(hTrv,@tInsert) ' insert item
    Return  TreeView_EnsureVisible(hTrv,h) ' open tree - make new item visible
END FUNCTION

 
FUNCTION MainWnd_cmdQuit_BnClicked(BYVAL hDlg AS HWND, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
    endFlag=-1
Return 0
END FUNCTION


'---------------------
'		TreeView
'---------------------

' Ajoute un item à la TreeView
Function AddTreeViewItem(hTree As HWND,hParent As HTREEITEM ,Text As ZString Ptr,iImage As Integer)As HTREEITEM
 
	Dim As TV_INSERTSTRUCT tvinsert 
	tvinsert.hParent=hParent 
	tvinsert.hInsertAfter=TVI_ROOT 
    tvinsert.item.mask=TVIF_TEXT Or TVIF_IMAGE Or TVIF_STATE Or TVIF_SELECTEDIMAGE 
	tvinsert.item.state = TVIS_SELECTED 
	tvinsert.item.pszText=Text 
	tvinsert.item.cchTextMax= Len(*Text) 
	tvinsert.item.iImage=tvinsert.item.iSelectedImage=iImage 
	return cast(HTREEITEM,SendMessage(hTree,TVM_INSERTITEM,0,cast(LPARAM,@tvinsert))) 
End Function

' Récupère le type de l'item hTreeSel en retournant l'id de l'îcone
Function GetTreeViewItemType(hTree As HWND,hTreeSel As HTREEITEM )As Integer
	Dim As TV_ITEM tvi 
	tvi.mask=TVIF_IMAGE 
	tvi.hItem=hTreeSel 
	SendMessage(hTree,TVM_GETITEM,0,cast(LPARAM,@tvi)) 
	return tvi.iImage 
End Function

'/ Récupère le texte de l'item hTreeSel
Sub GetTreeViewItemText(hTree As HWND,hTreeSel As HTREEITEM,lpResult  As ZString Ptr,nMaxCount As integer)
 	Dim As TV_ITEM tvi 
	tvi.mask=TVIF_TEXT 
	tvi.pszText=lpResult 
	tvi.cchTextMax=nMaxCount 
	tvi.hItem=hTreeSel 
	SendMessage(hTree,TVM_GETITEM,0,cast(LPARAM,@tvi))
End Sub

' Modifie le texte de l'item hTreeSel
Sub SetTreeViewItemText(hTree As HWND,hTreeSel As HTREEITEM,Text As ZString Ptr)
 	Dim As TV_ITEM tvi 
	tvi.mask=TVIF_TEXT 
	tvi.pszText=Text 
	tvi.cchTextMax=sizeof(Text) 
	tvi.hItem=hTreeSel 
	SendMessage(hTree,TVM_SETITEM,0,cast(LPARAM,@tvi))
End Sub
tvex.bas

Code: Select all

' Treeview Sample Code by Daniel Modler with powerbasic
' translatted to freebasic by Aloberr


  
 
#INCLUDE once  "WINdows.bi"
#Include Once "win/COMMCTRL.bi"
 
 
#Define dlgMain        101
#Define bmpAPI16       159
#Define bmpTvwOpen16    162
#Define trvMain        1000
#Define txtIconID       1001
#Define spnIconID       1003
#Define txtItemText    1004
#Define cmdAddItem      1005
#Define cmdQuit        1006

'===========  [ Program GLOBAL declarations ]  ========================
Dim Shared ghInst         AS HINSTANCE        ' Module instance handle.
Dim Shared ghCurrentDlg   AS HWND         ' Active modeless dlg handle.
Dim Shared ghImgList As HIMAGELIST
Dim Shared endFlag As Long

 
#Include Once "TVWEXAMP.bi"                ' Event handling functions module.


Declare FUNCTION MainWndProc(BYVAL hDlg    AS HWND,_
                     BYVAL wMsg    AS UINT,_
                     BYVAL wParam  AS WPARAM,_
                     BYVAL lParam  AS LPARAM)   AS LRESULT


 
FUNCTION WinMain(BYVAL hCurrInstance AS HINSTANCE,_
                 BYVAL hPrevInstance AS HINSTANCE,_
                 lpCmdLine           AS ZString PTR,_
                 BYVAL nCmdShow      AS LONG) AS LONG

 
  Dim  Msg            AS MSG
  Dim  wClass         AS WndClass
  Dim  hWnd           AS HWND
  Dim  szClassName    AS ZString * 11

  szClassName = "TrvExample"
  ghInst = hCurrInstance

  ' A REPLACEMENT for program PrevInstance.
  IF 0=(FindWindow(szClassName, BYVAL NULL)) THEN
     ' Fill window structure.
     wClass.style         = CS_HREDRAW OR CS_VREDRAW
     wClass.lpfnWndProc   = Cast(WNDPROC,GetProcAddress(GetModuleHandle("USER32"), "DefDlgProcA"))
     wClass.cbClsExtra    = 0
     wClass.cbWndExtra    = DLGWINDOWEXTRA
     wClass.hInstance     = hCurrInstance
     wClass.hIcon         = LoadIcon(NULL, BYVAL IDI_APPLICATION)
     wClass.hCursor       = LoadCursor(NULL, BYVAL IDC_ARROW)
     wClass.hbrBackground = GetStockObject(LTGRAY_BRUSH)
     wClass.lpszMenuName  = NULL
     wClass.lpszClassName = StrPtr(szClassName)

     ' Register the window-class.
     RegisterClass @wClass
  ELSE
     ' Bring prev instance to front.
     hWnd = FindWindow(szClassName, BYVAL NULL)
     IF IsIconic(hWnd) THEN ShowWindow hWnd ,SW_RESTORE
     SetForegroundWindow hWnd
     ' Exit.
     FUNCTION = 0
     EXIT FUNCTION
  END IF

  ' Create the main window as a MODELESS dialog.
  hWnd = CreateDialogParam(hCurrInstance, cast( LPCTSTR,dlgMain), 0, @MainWndProc, 0)
  ShowWindow hWnd, nCmdShow

  ' Main message loop of program.
  WHILE GetMessage(@Msg, NULL, 0, 0) AND (0=endFlag)
      IF 0=(ghCurrentDlg) OR 0=(IsDialogMessage(ghCurrentDlg,  @Msg)) THEN
         TranslateMessage @Msg
         DispatchMessage @Msg
      END IF
  WEND

  ' Clean up and assign return value.

  FUNCTION = Msg.wParam

END FUNCTION
End WinMain (getmoduleHandle(0),0,Command,SW_show)

'===============  [ MainWnd dialog window procedure ]  =================
FUNCTION MainWndProc(BYVAL hDlg    AS HWND,_
                     BYVAL wMsg    AS UINT,_
                     BYVAL wParam  AS WPARAM,_
                     BYVAL lParam  AS LPARAM)   AS LRESULT

  Dim Action  As LONG

  SELECT CASE wMsg
  	CASE WM_INITDIALOG
       ' Init and create common controls.
      InitCommonControls
      MainWnd_InitDialog(hDlg)
        ' Let dialog-box engine set the focus.
      Action = TRUE

  	CASE WM_ACTIVATE
      If LoWord(wParam) = 0 THEN  ' Dialog becoming inactive.
         ghCurrentDlg = 0
      Else                       ' Dialog becoming active.
         ghCurrentDlg = hDlg
      End IF

  	CASE WM_DESTROY
      ' Send a message to terminate message loop.
      PostQuitMessage 0
      Action = TRUE

  	CASE WM_NOTIFY
      Dim lpNmh   AS NMHDR PTR
      lpNmh = Cast(NMHDR Ptr,lParam)
      ' Examine .Code member.
      Select CASE lpNmh->Code
      	Case NM_CLICK
            Select CASE lpNmh->idFrom
            	Case trvMain
                 Action = MainWnd_trvMain_NmClick(hDlg, wParam, lParam)
            End SELECT

      	Case NM_DBLCLK
            Select CASE lpNmh->idFrom
            	Case trvMain
                 Action = MainWnd_trvMain_NmDblclk(hDlg, wParam, lParam)
            End SELECT

      	Case NM_SETFOCUS
            Select CASE lpNmh->idFrom
            	Case trvMain
                  Action = MainWnd_trvMain_NmSetfocus(hDlg, wParam, lParam)
            End SELECT

         ' Tree view notifications.
      	Case TVN_LAST TO TVN_FIRST
            Select CASE lpNmh->idFrom
            	Case trvMain
                  Select CASE lpNmh->Code
                  	Case TVN_SELCHANGED
                        Action = MainWnd_trvMain_TvnSelchanged(hDlg, wParam, lParam)
                  	Case TVN_SELCHANGING
                        Action = MainWnd_trvMain_TvnSelchanging(hDlg, wParam, lParam)
                  End SELECT
            End SELECT

            ' Up-Down control notifications.
      	Case UDN_DELTAPOS
            Select CASE lpNmh->idFrom
            	Case spnIconID
                  Select CASE lpNmh->Code
                  	Case UDN_DELTAPOS
                       Action = MainWnd_spnIconID_UdnDeltapos(hDlg, wParam, lParam)
                  End SELECT
            End SELECT

         case TVN_BEGINLABELEDIT:  
               Dim As LPNMTVDISPINFO lptvdi = Cast(LPNMTVDISPINFO,lpNmh ) 
       

         Case TVN_ENDLABELEDIT:  
         case TVM_DELETEITEM: 
      	case TVN_BEGINDRAG:
      	Case TVN_BEGINRDRAG: 
      	case TVN_GETDISPINFO: 
                Dim As LPNMTVDISPINFO lptvdi = Cast(LPNMTVDISPINFO,lpNmh ) 
   
      	case TVN_ITEMEXPANDING: 
                Dim As  LPNMTREEVIEW lppnmtv = Cast(LPNMTREEVIEW,lpNmh )
  
      	case TVN_SELCHANGING: 
               Dim As   LPNMTREEVIEW lppnmtv = Cast(LPNMTREEVIEW,lpNmh )
   
      	case TVN_ITEMEXPANDED: 
                Dim As  LPNMTREEVIEW lppnmtv = Cast(LPNMTREEVIEW,lpNmh )
 

      	case TVN_SELCHANGED: 
      End SELECT

  	CASE WM_COMMAND
      Select CASE LoWord(wParam)
      	Case txtIconID
            Select CASE HiWord(wParam)
            	Case EN_CHANGE
                  Action = MainWnd_txtIconID_EnChange(hDlg, wParam, lParam)

            	Case EN_UPDATE
                  Action = MainWnd_txtIconID_EnUpdate(hDlg, wParam, lParam)
            End SELECT

      	Case txtItemText

            Action = TRUE

      	Case cmdAddItem
            If HiWord(wParam) = BN_CLICKED THEN
               Action = MainWnd_cmdAddItem_BnClicked(hDlg, wParam, lParam)
            End IF

      	Case cmdQuit
            If HiWord(wParam) = BN_CLICKED THEN
               Action = MainWnd_cmdQuit_BnClicked(hDlg, wParam, lParam)
            End IF
      End SELECT

  	CASE WM_SYSCOMMAND
      Select CASE wParam
      	Case SC_CLOSE
            DestroyWindow hDlg
            Action = TRUE
      End SELECT
  END SELECT

  ' Assign function return value.
  FUNCTION = Action

END FUNCTION
bcohio2001
Posts: 556
Joined: Mar 10, 2007 15:44
Location: Ohio, USA
Contact:

Re: treeview how to use it . Need help

Post by bcohio2001 »

Use the TreeView_GetSelection(w) macro to get the current selected.
As for the rest of your questions ?????

Here is a basis of what I start with:

Code: Select all

#Include Once "win/commctrl.bi"
/' TreeView macros defined in commctrl.bi as of v0.21.1
#Define TreeView_InsertItem(w,i) cast(HTREEITEM,SNDMSG((w),TVM_INSERTITEM,0,cast(LPARAM,cast(LPTV_INSERTSTRUCT,i))))
#define TreeView_DeleteItem(w,i) cast(BOOL,SNDMSG((w),TVM_DELETEITEM,0,cast(LPARAM,cast(HTREEITEM,i))))
#define TreeView_DeleteAllItems(w) cast(BOOL,SNDMSG((w),TVM_DELETEITEM,0,cast(LPARAM,TVI_ROOT)))
#define TreeView_Expand(w,i,c) cast(BOOL,SNDMSG((w),TVM_EXPAND,c,cast(LPARAM,cast(HTREEITEM,i))))
''''''' #define TreeView_GetItemRect(w,i,p,c) *cast(HTREEITEM ptr,p)=(i) : SNDMSG((w),TVM_GETITEMRECT,c,cast(LPARAM,cast(LPRECT,p)))
#define TreeView_GetCount(w) cuint(SNDMSG((w),TVM_GETCOUNT,0,0))
#define TreeView_GetIndent(w) cuint(SNDMSG((w),TVM_GETINDENT,0,0))
#define TreeView_SetIndent(w,i) cast(BOOL,SNDMSG((w),TVM_SETINDENT,i,0))
#define TreeView_GetImageList(w,i) cast(HIMAGELIST,SNDMSG((w),TVM_GETIMAGELIST,i,0))
#define TreeView_SetImageList(w,h,i) cast(HIMAGELIST,SNDMSG((w),TVM_SETIMAGELIST,i,cast(LPARAM,cast(HIMAGELIST,h))))
#define TreeView_GetNextItem(w,i,c) cast(HTREEITEM,SNDMSG((w),TVM_GETNEXTITEM,c,cast(LPARAM,cast(HTREEITEM,i))))
#define TreeView_GetChild(w,i) TreeView_GetNextItem(w,i,TVGN_CHILD)
#define TreeView_GetNextSibling(w,i) TreeView_GetNextItem(w,i,TVGN_NEXT)
#define TreeView_GetPrevSibling(w,i) TreeView_GetNextItem(w,i,TVGN_PREVIOUS)
#define TreeView_GetParent(w,i)	TreeView_GetNextItem(w,i,TVGN_PARENT)
#define TreeView_GetFirstVisible(w) TreeView_GetNextItem(w,NULL,TVGN_FIRSTVISIBLE)
#define TreeView_GetNextVisible(w,i) TreeView_GetNextItem(w,i,TVGN_NEXTVISIBLE)
#define TreeView_GetPrevVisible(w,i) TreeView_GetNextItem(w,i,TVGN_PREVIOUSVISIBLE)
#define TreeView_GetSelection(w) TreeView_GetNextItem(w,NULL,TVGN_CARET)
#define TreeView_GetDropHilight(w) TreeView_GetNextItem(w,NULL,TVGN_DROPHILITE)
#define TreeView_GetRoot(w) TreeView_GetNextItem(w,NULL,TVGN_ROOT)
#define TreeView_Select(w,i,c) cast(HTREEITEM,SNDMSG((w),TVM_SELECTITEM,c,cast(LPARAM,cast(HTREEITEM,i))))
#define TreeView_SelectItem(w,i) TreeView_Select(w,i,TVGN_CARET)
#define TreeView_SelectDropTarget(w,i) TreeView_Select(w,i,TVGN_DROPHILITE)
#define TreeView_SelectSetFirstVisible(w,i)	TreeView_Select(w,i,TVGN_FIRSTVISIBLE)
#define TreeView_GetItem(w,i) cast(BOOL,SNDMSG((w),TVM_GETITEM,0,cast(LPARAM,cast(TV_ITEM ptr,i))))
#define TreeView_SetItem(w,i) cast(BOOL,SNDMSG((w),TVM_SETITEM,0,cast(LPARAM,cast(TV_ITEM ptr,i))))
#define TreeView_EditLabel(w,i) cast(HWND,SNDMSG((w),TVM_EDITLABEL,0,cast(LPARAM,cast(HTREEITEM,i))))
#define TreeView_GetEditControl(w) cast(HWND,SNDMSG((w),TVM_GETEDITCONTROL,0,0))
#define TreeView_GetVisibleCount(w) cuint(SNDMSG((w),TVM_GETVISIBLECOUNT,0,0))
#define TreeView_HitTest(w,p) cast(HTREEITEM,SNDMSG((w),TVM_HITTEST,0,cast(LPARAM,cast(LPTV_HITTESTINFO,p))))
#define TreeView_CreateDragImage(w,i) cast(HIMAGELIST,SNDMSG((w),TVM_CREATEDRAGIMAGE,0,cast(LPARAM,cast(HTREEITEM,i))))
#define TreeView_SortChildren(w,i,r) cast(BOOL,SNDMSG((w),TVM_SORTCHILDREN,r,cast(LPARAM,cast(HTREEITEM,i))))
#define TreeView_EnsureVisible(w,i) cast(BOOL,SNDMSG((w),TVM_ENSUREVISIBLE,0,cast(LPARAM,cast(HTREEITEM,i))))
#define TreeView_SortChildrenCB(w,s,r) cast(BOOL,SNDMSG((w),TVM_SORTCHILDRENCB,r,cast(LPARAM,cast(LPTVSORTCB,s))))
#Define TreeView_EndEditLabelNow(w,f) cast(BOOL,SNDMSG((w),TVM_ENDEDITLABELNOW,f,0))
#define TreeView_GetISearchString(w,s) cast(BOOL,SNDMSG((w),TVM_GETISEARCHSTRING,0,cast(LPARAM,s)))
#define TreeView_GetToolTips(w) cast(HWND,SNDMSG((w),TVM_GETTOOLTIPS,0,0))
#define TreeView_SetToolTips(w,wt) cast(HWND,SNDMSG((w),TVM_SETTOOLTIPS,cast(WPARAM,wt),0))
#define TreeView_GetBkColor(w) cast(COLORREF,SNDMSG((w),TVM_GETBKCOLOR,0,0))
#define TreeView_GetInsertMarkColor(w) cast(COLORREF,SNDMSG((w),TVM_GETINSERTMARKCOLOR,0,0))
#define TreeView_GetItemHeight(w) cint(SNDMSG((w),TVM_GETITEMHEIGHT,0,0))
#define TreeView_GetScrollTime(w) cuint(SNDMSG((w),TVM_GETSCROLLTIME,0,0))
#define TreeView_GetTextColor(w) cast(COLORREF,SNDMSG((w),TVM_GETTEXTCOLOR,0,0))
#define TreeView_SetBkColor(w,c) cast(COLORREF,SNDMSG((w),TVM_SETBKCOLOR,0,cast(LPARAM,c)))
#define TreeView_SetInsertMarkColor(w,c) cast(COLORREF,SNDMSG((w),TVM_SETINSERTMARKCOLOR,0,cast(LPARAM,c)))
#define TreeView_SetItemHeight(w,h) cint(SNDMSG((w),TVM_SETITEMHEIGHT,cast(WPARAM,h),0))
#define TreeView_SetScrollTime(w,t) cuint(SNDMSG((w),TVM_SETSCROLLTIME,cast(WPARAM,cuint(t)),0))
#define TreeView_SetTextColor(w,c) cast(COLORREF,SNDMSG((w),TVM_SETTEXTCOLOR,0,cast(LPARAM,c)))
#define TreeView_SetInsertMark(w,i,a) cast(BOOL,SNDMSG((w),TVM_SETINSERTMARK,cast(WPARAM,a),cast(LPARAM,i)))
#define TreeView_SetUnicodeFormat(w,u) cast(BOOL,SNDMSG((w),TVM_SETUNICODEFORMAT,cast(WPARAM,u),0))
#define TreeView_GetUnicodeFormat(w) cast(BOOL,SNDMSG((w),TVM_GETUNICODEFORMAT,0,0))
#define TreeView_GetItemState(w,i,m) cuint(SNDMSG((w),TVM_GETITEMSTATE,cast(WPARAM,i),cast(LPARAM,m)))
'/

Function TreeViewInsertItem(BYVAL hTreeView AS HWND, BYVAL hParent AS HTREEITEM,sItem AS STRING) AS LONG
    Dim tTVInsert   AS TV_INSERTSTRUCT
    Dim tTVItem     AS TV_ITEM

    If hParent THEN
        tTVItem.mask        = TVIF_CHILDREN OR TVIF_HANDLE
        tTVItem.hItem       = hParent
        tTVItem.cchildren   = 1
        TreeView_SetItem( hTreeView, @tTVItem)
    END IF

    tTVInsert.hParent              = hParent
    tTVInsert.Item.mask            = TVIF_TEXT Or TVIF_STATE
    tTVInsert.Item.state           = TVIS_EXPANDED
    tTVInsert.Item.pszText         = STRPTR(sItem)
    tTVInsert.Item.cchTextMax      = LEN(sItem)
    FUNCTION = TreeView_InsertItem(hTreeView, @tTVInsert)
END Function

Function TreeView_GetItemText(ByVal hTreeView As HWND, ByVal hItem As HTREEITEM)As String
    Dim ItemText As zstring*32765
    Dim Item As TV_ITEM
    Item.hItem=hItem
    Item.Mask=TVIF_TEXT
    Item.cchTextMax=32765
    Item.pszText=@ItemText
    SendMessage hTreeView,TVM_GETITEM,0,@Item
    Function=ItemText
End Function

Function TreeView_ChangeChildState(ByVal hTreeView As HWND, ByVal hItem As HTREEITEM, ByVal iState As Long) As Long
	Dim hTmpItem As Long
	Dim TVITEM As TV_ITEM
	hTmpItem = TreeView_GetChild(hTreeView,hItem)
	While hTmpItem 
		TVITEM.mask = TVIF_STATE Or TVIF_HANDLE
		TVITEM.hItem = hTmpItem
		TVITEM.stateMask = TVIS_STATEIMAGEMASK
		TVITEM.state = (iState+1) Shl 12
		sendmessage hTreeView,TVM_SetItem,0,@TVITEM
		TreeView_ChangeChildState hTreeView, hTmpItem, iState
		hTmpItem = treeview_getnextitem(htreeview,hTmpItem,TVGN_NEXT)
	wend	
End Function

Function TreeView_ChangeParentState(ByVal hTreeView As HWND, ByVal hItem As HTREEITEM, ByVal iState As Long) As Long
	Dim hTmpItem As Long
	Dim TVITEM As TV_ITEM
	hTmpItem = TreeView_GetParent(hTreeView,hItem)
	If hTmpItem then
		TVITEM.mask = TVIF_STATE Or TVIF_HANDLE
		TVITEM.hItem = hTmpItem
		TVITEM.stateMask = TVIS_STATEIMAGEMASK
		TVITEM.state = (iState+1) Shl 12
		sendmessage hTreeView,TVM_SetItem,0,@TVITEM
		TreeView_ChangeParentState hTreeView, hTmpItem,0
	EndIf	
End Function

Function TreeView_SetCheckState(ByVal hTreeView As HWND, ByVal hItem As HTREEITEM, ByVal iState As Long) As Long
	Dim TVITEM As TV_ITEM
	TVITEM.mask = TVIF_STATE Or TVIF_HANDLE
	TVITEM.hItem = hItem
	TVITEM.stateMask = TVIS_STATEIMAGEMASK
	TVITEM.state = (iState+1) Shl 12
	sendmessage hTreeView,TVM_SetItem,0,@TVITEM
End Function

Function TreeView_GetCheckState( ByVal hTreeView As HWND, ByVal hItem As HTREEITEM ) As Long
    Dim tvItem As TV_ITEM
    tvItem.mask         = TVIF_HANDLE Or TVIF_STATE
    tvItem.hItem        = hItem
    tvItem.stateMask    = TVIS_STATEIMAGEMASK
    sendmessage hTreeView,TVM_GetItem,0,@tvItem
    tvItem.state Shr=12
    tvItem.state-=1
    Function = tvItem.state Xor 1
End Function
aloberoger
Posts: 507
Joined: Jan 13, 2009 19:23

Re: treeview how to use it . Need help

Post by aloberoger »

thanks
but i was thinking that you cound apply this in my example,
put a word in the textbox and it will be copied in the treeview, by selecting an item in the treeview how can somebody modified
this itemtext, how can we swap two items in the treeview, how can we attach an imagelist with transparency background
bcohio2001
Posts: 556
Joined: Mar 10, 2007 15:44
Location: Ohio, USA
Contact:

Re: treeview how to use it . Need help

Post by bcohio2001 »

I got your code and will look at it more later.

In the meantime, all I know about Treeviews can be found in my Double Directory Explorer
The code is quite messy. Been trying to clean it up.
In the DDir.bas, look at my WM_NOTIFY section. (Edit value inside a listview, but about the same for tree.)
"TV_Control.bas" is where most of the actual tree handling is done.
In "DDir.bi" the only thing that might be helpful is:

Code: Select all

'Editing inside listview
Type EditLVItem
	As HWND TheLView 'since using 3 types listviews for top and bottom -- remember which one
	As HWND EditMe 'handle for edit box created
	As String OldValue 'original value in Listview
	As BOOL IsDir 'flag if is a folder
	As String ThePath 'path for renaming
	As Integer ItemNum 'which item editing
End Type
It's for a listview, but could be adapted for Trees.
Post Reply