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

本文介绍如何在Win32 MFC应用程序中处理树控件内的复选框状态改变事件,通过捕获特定消息来实现自定义通知机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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…

}

 

[from http://www.apijunkie.com/APIJunkie/blog/post/2007/11/Handling-tree-control-check-box-selectionde-selection-in-Win32MFC.aspx]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值