How to check a tree node if I have the node hWnd (API)

Everything else that doesn't fall into one of the other PB categories.
halo
Enthusiast
Enthusiast
Posts: 104
Joined: Mon Jan 26, 2004 2:49 am

How to check a tree node if I have the node hWnd (API)

Post by halo »

Can anyone show me how I would check and uncheck a tree node if I only have the handle of the node? I can get the state like this:

Procedure.l TreeViewNodeChecked(hTree,hNode)
If (4096 & SendMessage_(hTree,#TVM_GETITEMSTATE,hNode,0))=0
ProcedureReturn 1
EndIf
EndProcedure

Here are some docs:
http://msdn.microsoft.com/library/defau ... etitem.asp

Thanks for your help.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Seems easier to use native PB functions but maybe you have a reason for using API. Either way, see if thisl helps you. :)

Code: Select all

#TVM_GETITEMSTATE = #TV_FIRST + 39
If OpenWindow(0, 0, 0, 355, 180, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "TreeGadget") And CreateGadgetList(WindowID(0)) 
  TreeGadget(0, 180, 10, 160, 160, #PB_Tree_AlwaysShowSelection | #PB_Tree_CheckBoxes | #PB_Tree_NoLines)
  ButtonGadget(1, 10, 10, 100, 20, "API Check")
  ButtonGadget(2, 10, 35, 100, 20, "API Un-check")
  ButtonGadget(3, 10, 125, 100, 20, "PB Check")
  ButtonGadget(4, 10, 150, 100, 20, "PB Un-check")
  For i = 0 To 5 
    AddGadgetItem (0, -1, "Item "+ Str(i))
  Next 
  SetGadgetState(0, 0)
  DisableGadget(1, 0) 
  DisableGadget(3, 0) 
  DisableGadget(2, 1) 
  DisableGadget(4, 1) 
  Repeat
    event = WaitWindowEvent()
    Select event
      Case #PB_EventGadget
        Select EventGadgetID()
          Case 0
            If GetGadgetItemState(0, GetGadgetState(0)) & #PB_Tree_Checked
              DisableGadget(1, 1) 
              DisableGadget(3, 1) 
              DisableGadget(2, 0) 
              DisableGadget(4, 0) 
            Else
              DisableGadget(1, 0) 
              DisableGadget(3, 0) 
              DisableGadget(2, 1) 
              DisableGadget(4, 1) 
            EndIf
          Case 1
            ; --> API method to add check
            tvNode = GetGadgetState(0)            ; item index as used in PB
            APIhandle = GadgetItemID(0, tvNode)   ; item handle as used in API
            nodeState = SendMessage_(GadgetID(0), #TVM_GETITEMSTATE, APIhandle, #TVIS_STATEIMAGEMASK)
            ; when nodeState = %1000000000010 (4098) then nodeState = %1 (1) after bit shift >>12 
            If nodeState >> 12 = 1
              tv.TV_ITEM\mask = #TVIF_HANDLE | #TVIF_STATE
              tv.TV_ITEM\hItem = APIhandle
              tv.TV_ITEM\state = 2 << 12 
              tv.TV_ITEM\stateMask = #TVIS_STATEIMAGEMASK 
              SendMessage_(GadgetID(0), #TVM_SETITEM, 0, tv)
              DisableGadget(1, 1) 
              DisableGadget(3, 1) 
              DisableGadget(2, 0) 
              DisableGadget(4, 0) 
            EndIf
          Case 2
            ; --> API method to remove check
            tvNode = GetGadgetState(0)            ; item index as used in PB
            APIhandle = GadgetItemID(0, tvNode)   ; item handle as used in API
            nodeState = SendMessage_(GadgetID(0), #TVM_GETITEMSTATE, APIhandle, #TVIS_STATEIMAGEMASK)
            ; --> when nodeState = %10000000000010 (8194) then nodeState = %10 (2) after bit shift >>12
            If nodeState >> 12 = 2
              tv.TV_ITEM\mask = #TVIF_HANDLE | #TVIF_STATE
              tv.TV_ITEM\hItem = APIhandle
              tv.TV_ITEM\state = 1 << 12
              tv.TV_ITEM\stateMask = #TVIS_STATEIMAGEMASK 
              SendMessage_(GadgetID(0), #TVM_SETITEM, 0, tv)
              DisableGadget(1, 0) 
              DisableGadget(3, 0) 
              DisableGadget(2, 1) 
              DisableGadget(4, 1) 
            EndIf
          Case 3
            ; --> PB method to add check
            tvNode = GetGadgetState(0) 
            If GetGadgetItemState(0, tvNode) & (~#PB_Tree_Checked)
              SetGadgetItemState(0, tvNode, #PB_Tree_Checked | #PB_Tree_Selected)
              DisableGadget(1, 1) 
              DisableGadget(3, 1) 
              DisableGadget(2, 0) 
              DisableGadget(4, 0) 
            EndIf
          Case 4
            ; --> PB method to remove check
            tvNode = GetGadgetState(0) 
            If GetGadgetItemState(0, tvNode) & #PB_Tree_Checked
              SetGadgetItemState(0, tvNode, ~#PB_Tree_Checked)
              DisableGadget(1, 0) 
              DisableGadget(3, 0) 
              DisableGadget(2, 1) 
              DisableGadget(4, 1) 
            EndIf
        EndSelect
    EndSelect
  Until event = #PB_Event_CloseWindow 
EndIf 
End
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Post Reply