Handling tree control check box selection/de-selection in Win32/MFC

by APIJunkie 11. November 2007 00:38

Unfortunately, no direct notifications are sent when a check box gets selected or deselected in a win32 tree control.

 

While this is a gross oversight by Microsoft in the design of the tree control, there are ways to overcome this problem.

 

One simple way to overcome this problem is to catch the following messages that are sent to the parent window of a tree control:

 

TVN_KEYDOWN -> http://msdn2.microsoft.com/en-us/library/bb773540.aspx

 

NM_CLICK -> http://msdn2.microsoft.com/en-us/library/bb773466.aspx

 

When one of the above messages is sent, the state of the check box, next to the tree item, will be changed.

 

Below is an MFC code snippet that handles the above messages:

 

// CTreeCtrl *m_fileTreeCtrl is a member of CDemoTreeDlg dialog.

// CDemoTreeDlg dialog will handle the TVN_KEYDOWN and NM_CLICK messages

 

BEGIN_MESSAGE_MAP(CDemoTreeDlg, CDialog)

      ON_MESSAGE(TREE_VIEW_CHECK_STATE_CHANGE, OnTreeViewCheckStateChange)

      ON_NOTIFY(NM_CLICK, IDC_FILESYS_TREE, &CDemoTreeDlg::OnNMClickFileSysTree)

      ON_NOTIFY(TVN_KEYDOWN, IDC_FILESYS_TREE, &CDemoTreeDlg::OnTvnKeydownFileSysTree)

END_MESSAGE_MAP()

 

// handle mouse click on check box

void CDemoTreeDlg::OnNMClickFileSysTree(NMHDR *pNMHDR, LRESULT *pResult)

{

      // TODO: Add your control notification handler code here

      HTREEITEM item;

      UINT flags;

      // verify that we have a mouse click in the check box area

      DWORD pos = GetMessagePos();

      CPoint point(LOWORD(pos), HIWORD(pos));

      m_fileTreeCtrl->ScreenToClient(&point);

      item = m_fileTreeCtrl->HitTest(point, &flags);

      if(item && (flags & TVHT_ONITEMSTATEICON))

      {

            // handle state change here or post message to another handler

            // Post message state has changed…

            // PostMessage(TREE_VIEW_CHECK_STATE_CHANGE,0,(LPARAM)item);

      }

      *pResult = 0;

}

 

// handle key press on check box

void CDemoTreeDlg::OnTvnKeydownFileSysTree(NMHDR *pNMHDR, LRESULT *pResult)

{

      LPNMTVKEYDOWN pTVKeyDown = reinterpret_cast<LPNMTVKEYDOWN>(pNMHDR);

      // TODO: Add your control notification handler code here

      // we only want to handle the space key press

      if(pTVKeyDown->wVKey==VK_SPACE)

      {

            // Determine the selected tree item.

            HTREEITEM item = m_fileTreeCtrl->GetSelectedItem();

            if(item!=NULL)

            {

                  // handle state change here or post message to another handler

                  // Post message state has changed…

                  // PostMessage(TREE_VIEW_CHECK_STATE_CHANGE,0,(LPARAM)item);

            }

      }

      *pResult = 0;

}

 

In many cases it is better to handle the check box change in a central place after the change. To do this we can define a custom message that will be called when a check box state has changed:

 

// define a custom message

#define TREE_VIEW_CHECK_STATE_CHANGE (WM_USER + 100)

// implementation for TREE_VIEW_CHECK_STATE_CHANGE message handler

LRESULT CDemoTreeDlg::OnTreeViewCheckStateChange(WPARAM wParam, LPARAM lParam)

{

      // Handle message here…

      HTREEITEM   changedItem = (HTREEITEM)lParam;

      int checkState = m_fileTreeCtrl->GetCheck(changedItem);

      // Do something with check state change information…

}

Tags:

MFC | Win32 | GUI

Comments

12/1/2007 10:10:45 AM #

ingo

This tipp was very useful and has solved my problem...
Thank you very much and greetings from germany!

ingo Germany

3/2/2008 1:28:27 AM #

Also Stuck in C++

Thank you. This helped Smile
And this greeting from India.
Nameste!

Also Stuck in C++ India

6/30/2008 6:07:33 PM #

Chris

Hello,

Thank you.
I'm searching for long time, before to find your solution.
Now it's Great !

Chris France

11/24/2008 5:22:11 PM #

mittu

Thanks.... Great tip.. It worked for me...

mittu United States

2/22/2009 8:20:05 AM #

Alex

This gave me an excellent starting point ... In the even less documented area of TreeCtrl's embeded in CTreeView's classes.

To whomever is wondering about the same, here are my findings:

1.TreeCtrl's are not embeded in CTreeView's. CTreeView's are actually CTreeCtrl's nowadays.

2.This may have not always been the case. I came to a hint that this is true with MFC 4.0 onwards. If any still struggle with something older, the code above should work nice with CTreeView instead with CDialog, but only as long as you know the ID of the CTreeCtrl within the CTreeView. Which you generally don't. In this case I found 2 solutions on codeguru:
  - Establish yourself a desired ID and enforce it in CTreeView::OnInitialUpdate or whatever, with the GetTreeCtrl().SetDlgCtrlId(<EstablishedID>)
  - Enlarge the ON_NOTIFY(notifyCode, IDC_FILESYS,...) map macros above to ON_NOTIFY_RANGE(notifyCode,1, UINT(-1),...)
CTreeView may embed an TreeCtrl or not; however it will not embed anything else, hence you will get no ID conflicts with these.

3. With the latest MFC versions where CTReeView==CTreeCtrl the solution can be find in the OleView sample.
Use the APIJunkie constructs above with CTreeView instead of a CDialog, only with the following changes:
- change ON_NOTIFY to ON_NOTIFY_REFLECT
- these later macros do not take the CTreeCtrlID parameter. Drop it.

But again to where it started:
Thanks, APIJunkie

Alex Romania

6/4/2010 10:07:32 AM #

Hassane

great thank you from morocco
this helped me too

Hassane Morocco

Comments are closed

About the author

Name of author

I was first wounded by x86 assembly, recovered and moved on to C. Following a long addiction to C++ and a short stint at rehab I decided to switch to a healthier addiction so I am now happily sniffing .NET and getting hooked on Silverlight.

I am mainly here to ramble about coding, various API’s, Junkies(me especially) and everything else that happens between coders and their significant other.

  James Bacon