开发需要,借用了一个哥们的blog里的代码,但是它里面有些错误,做了下修改,但还是尊重他原创吧
void CSearechDLG::OnNMClickCommercialTree(NMHDR *pNMHDR, LRESULT *pResult)
{// TODO: Add your control notification handler code here
CPoint point;
GetCursorPos(&point); // 当前鼠标坐标
CPoint PointInTree = point;
m_CtrlCommercial.ScreenToClient( &PointInTree);
HTREEITEM Item;
UINT nFlag = TVHT_ONITEM; // 当在 Item 上左击时
Item = m_CtrlCommercial.HitTest(PointInTree, &nFlag);
if ( Item != NULL)
{
PostMessage(UM_CHECKSTATECHANGE, 0, (LPARAM)Item);
//获取所选节点的文字信息
//CString sText = m_CtrlCommercial.GetItemText(hItem);
}
LRESULT CSearechDLG::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
HTREEITEM hItem = (HTREEITEM)lParam;
DWORD data = m_CtrlCommercial.GetItemData(hItem);
BOOL bCheck = m_CtrlCommercial.GetCheck(hItem);
CString cStrTemp = m_CtrlCommercial.GetItemText(hItem);
if(data==1 && !bCheck)
{
SetParentChildNodeCheck(hItem, 0); //按下CheckBox后触发的动作
m_CtrlCommercial.SetItemData(hItem, 0);
}
if(data==0 && bCheck)
{
SetParentChildNodeCheck(hItem, 1); //按下CheckBox后触发的动作
m_CtrlCommercial.SetItemData(hItem, 1);
}
return 0;
}
BOOL CSearechDLG::SetParentChildNodeCheck(HTREEITEM hItem, BOOL bCheck)
{
if (hItem==NULL)
{
return FALSE;
}
HTREEITEM hChild = m_CtrlCommercial.GetChildItem(hItem);
SetChildCheck(hChild, bCheck);
m_CtrlCommercial.SetCheck(hItem, bCheck);
return TRUE;
}
//递归函数
void CSearechDLG::SetChildCheck(HTREEITEM hItem, BOOL bCheck)
{
if (hItem == NULL) return;
m_CtrlCommercial.SetCheck(hItem, bCheck);
HTREEITEM hSibling = m_CtrlCommercial.GetNextItem(hItem, TVGN_NEXT);
while (hSibling)
{
m_CtrlCommercial.SetCheck(hSibling, bCheck);
HTREEITEM hSibChild = m_CtrlCommercial.GetChildItem(hSibling);
if (hSibChild)
{
SetChildCheck(hSibChild, bCheck);
}
hSibling = m_CtrlCommercial.GetNextItem(hSibling, TVGN_NEXT);
}
HTREEITEM hChild = m_CtrlCommercial.GetChildItem(hItem);
if (hChild!=NULL)
{
SetChildCheck(hChild, bCheck);
}
}