Greetings,
Msgbox "Check state = " + Str$(Treeview_Getcheckstate(htrv,hitem) '---Returns 4096 or 8192
htrv is handle to a treeview control with style that uses %TVS_CHECKBOXES
hitem is a tree node
Treeview_Getcheckstate() does not return Checked = 1, Unchecked = 0, or No Check Box Image = -1 as the documentation states. I keep getting 4096 and 8192. I can't seem to get Treeview_Setcheckstate( htrv, hitem, 1) or Treeview_Setcheckstate( htrv, hitem, %TRUE) to set the tree node check box. I fail to understand what's going on with it. Any help would be appreciated.
Thanks
Change in TreeViewCtrl.inc
' ========================================================================================
FUNCTION TreeView_GetCheckState (BYVAL hwndTV AS DWORD, BYVAL hItem AS DWORD) AS LONG
LOCAL dTmp AS LONG
dTmp = %TVIS_STATEIMAGEMASK
SHIFT RIGHT dTmp, 12
DECR dTmp
FUNCTION = SendMessage(hwndTV, %TVM_GETITEMSTATE, hItem, dTmp)
END FUNCTION
' ========================================================================================
to:
' ========================================================================================
FUNCTION TreeView_GetCheckState (BYVAL hwndTV AS DWORD, BYVAL hItem AS DWORD) AS LONG
LOCAL nState AS LONG
nState = SendMessage(hwndTV, %TVM_GETITEMSTATE, hItem, %TVIS_STATEIMAGEMASK)
SHIFT RIGHT nState, 12
nState -=1
FUNCTION = nState
END FUNCTION
' ========================================================================================
Odd. You must be the first one to use this function, because it has been wrongly implemented in the PB includes since a lot of years ago.
No, you aren't the first. In 2003, Kev Peel had the same problem: http://www.powerbasic.com/support/pbforums/showthread.php?t=8952&highlight=TVM_GETITEMSTATE
The problem was a bad translation of this C++ macro:
#define ListView_GetCheckState(hwndLV, i) \
((((UINT)(SNDMSG((hwndLV), LVM_GETITEMSTATE, (WPARAM)(i), LVIS_STATEIMAGEMASK))) >> 12) -1)
I have used LONG as the return type to return -1, as stated in the MSDN documentation. If a DWORD will be used, it wil return &HFFFFFFFF???
Hi Jose
I appreciate your efforts and time. I just started using PBasic. Your name is well known in this community.
Thanks for the prompt reply to this inquiry.
Hi Jose,
Tried the fix and it returns a 1 for item checked and 0 for item unchecked consistently. However Treeview_SETcheckstat()
doesn't seem to be setting the check box. Here's the code in Win32 includes that come with PB10. It works as expected.
'---Get the root node and expand it
TREEVIEW GET ROOT hDlg, %IDC_trvNavigator TO hRoot :TREEVIEW SET EXPANDED hDlg, %IDC_trvNavigator, hRoot, -1
'---Check to see if root node has been check marked
IF BookChecks(0) = -1 THEN TREEVIEW SET CHECK hDlg, %IDC_trvNavigator, hRoot, -1
'---Get 1st child parent under root node
TREEVIEW GET CHILD hDlg, %IDC_trvNavigator, hRoot TO hParent
cnt = 1 '---Initial value of chapter counter
FOR i = 1 TO %NUMBOOKS
'---Check to see if it's been checked marked
IF BookChecks(i) = -1 THEN TREEVIEW SET CHECK hDlg, %IDC_trvNavigator, hParent, -1
'---Get the 1st child node under this parent
TREEVIEW GET CHILD hDlg, %IDC_trvNavigator, hParent TO hChild
FOR j = 1 TO ChaptsPerBook(i) '---Array that stores number of chapters in each book
'---Check to see if child node has been checked
IF ChaptChecks(cnt) = -1 THEN TREEVIEW SET CHECK hDlg, %IDC_trvNavigator, hChild, -1
'---Get next sibling node
TREEVIEW GET NEXT hDlg, %IDC_trvNavigator, hChild TO hNextChild
'---Transfer this nextchild to previous node handle and iterate loop
hChild = hNextChild
INCR cnt '---Increment chapter counter
NEXT
'---Get the next child parent node
TREEVIEW GET NEXT hDlg, %IDC_trvNavigator, hParent TO hNextParent
'---Transfer to previous parent node holder
hParent = hNextParent
'---Iterate loop
NEXT
Here's the code using Firefly 3.5 with your Win32 includes...
'---Get the root node
hRoot = Treeview_Getroot(trv)
'---Check to see if root node has been check marked
If BookChecks(0) = %TRUE Then Treeview_Setcheckstate( trv, hRoot, %TRUE)
'---Get 1st child parent under root node
hParent = Treeview_Getchild( trv, hRoot)
cnt = 1 '---Initial value of chapter counter
For i = 1 To %NUMBOOKS
'---Check to see if it's been checked marked
If BookChecks(i) = %TRUE Then Treeview_Setcheckstate( trv, hParent, %TRUE)
'---Get the 1st child node under this parent
hChild = Treeview_Getchild( trv, hParent)
For j = 1 To ChaptsPerBook(i) '---Array that stores number of chapters in each book
'---Check to see if child node has been checked
If ChaptChecks(cnt) = %TRUE Then Treeview_Setcheckstate( trv, hChild, %TRUE)
'---Get next sibling node
hNextChild = Treeview_Getnextsibling(trv, hChild)
'---Transfer this nextchild to previous node handle and iterate loop
hChild = hNextChild
Incr cnt '---Increment chapter counter
Next
'---Get the next child parent node
hNextParent = Treeview_Getnextsibling( trv, hParent)
'---Transfer to previous parent node holder
hParent = hNextParent
'---Iterate loop
Next
This code doesn't work. So I'm puzzled.
Kind regards,
G.C.
I have tried the TreeView_SetCheckState function and it works. Your problem seems to be that in your DDT code you use -1 for comparisons, e.g. IF BookChecks(0) = -1, whereas in the SDK code you use %TRUE, e.g. BookChecks(i) = %TRUE. %TRUE is defined as 1, not -1; therefore, your comparison fails.
Hi Jose,
As I stated the top code example works as expected with PowerBasic 10, Win32 includes. %TRUE is defined as non-zero. That's not the problem.
It's the bottom example of code that doesn't work. As you said the Treeview_SetCheckstate() in your Win32 includes does in fact change the state internally. However the check mark in the treeview control is not being displayed when set to %TRUE. Treeview_Getcheckstate() does report the change. It's just not showing a check in the treeview control. Do I need to repaint the control, update the control, set the image index or am I just way off base? Still puzzled.
Thanks
G.C.
You can invalidate redraw before the loop
SendMessage trv, %WM_SETREDRAW, %FALSE, 0 ' Disables redraw
and enable it and repaint the control after it
SendMessage trv, %WM_SETREDRAW, %TRUE, 0 ' Enables redraw
InvalidateRect trv, BYVAL %NULL, %TRUE ' Tells Windows to repaint the control
Thanks Jose, I think the problem is within Firefly 3.5... Not sure. Still Doesn't work.
For Treeviews don't you need to set the Imagelist for the Treeview containing actual checkmark images. Just setting the state selected/unselected just sets the state...it doesn't automatically set the image. Maybe PB's DDT does set the image as part of also setting the state(?), but winapi requires you to set the image separately. I am not at my development computer so I can't test it. Maybe I am wrong.
Search the FireFly forum for the tri-state treeview example project by David Kenny. That simple project uses an imagelist and displays/undisplays a checkmark when clicked.