轉載請說明出處,謝謝~~:http://blog.csdn.net/zhuhongshu/article/details/42265209函數
CTreeViewUI裏面自帶了複選的功能,可是複選功能存在bug:ui
1)當一個分組下面存在子項時,子項被手動全選後,分組的複選框沒有自動變爲選中狀態spa
2)當一個分組下面存在子項時,當全部子項都取消選中狀態時,分組的複選框仍是選中狀態.net
分組的CTreeNodeUI控件應該自動判斷是否爲選中狀態,bug被修復後的效果以下:code
修復過程:blog
當某個複選框被單擊後會觸發CTreeViewUI控件的OnCheckBoxChanged函數,這時應該在這裏作出判斷,來決定分組的選中狀態,爲此我給CTreeNodeUI控件增長了IsAllChildChecked函數。get
修改後的OnCheckBoxChanged函數以下:it
bool CTreeViewUI::OnCheckBoxChanged( void* param ) { TNotifyUI* pMsg = (TNotifyUI*)param; if(pMsg->sType == _T("selectchanged")) { CCheckBoxUI* pCheckBox = (CCheckBoxUI*)pMsg->pSender; CTreeNodeUI* pItem = (CTreeNodeUI*)pCheckBox->GetParent()->GetParent(); SetItemCheckBox(pCheckBox->GetCheck(),pItem); if(pItem->GetParentNode() != NULL) //edit by:Redrain 2014.12.11 pItem->GetParentNode()->IsAllChildChecked(); return true; } return true; }
當某個複選框被單擊後,去通知他的父控件,讓父控件判斷是否應該是選中狀態。爲CTreeNodeUI控件增長的IsAllChildChecked函數以下:class
void CTreeNodeUI::IsAllChildChecked() { bool bIsAllChildChecked = true; bool bIsAllChildUncheck = true; int nCount = GetCountChild(); if(nCount > 0) { for(int nIndex = 0;nIndex < nCount;nIndex++) { CTreeNodeUI* pItem = GetChildNode(nIndex); if(!pItem->GetCheckBox()->IsSelected()) { bIsAllChildChecked = false; } else { bIsAllChildUncheck = false; } } if (bIsAllChildChecked && !GetCheckBox()->IsSelected()) { GetCheckBox()->Selected(true); return; } else if (bIsAllChildUncheck && GetCheckBox()->IsSelected()) { GetCheckBox()->Selected(false); return; } } }
總結:select
bug的修復代碼已經提交到我本身的Duilib庫。
個人Duilib庫代碼下載地址:點擊打開連接
Redrain 2014.12.30