1.在對話框中添加CTreeCtrl控件並勾選Check Boxes選項,爲CTreeCtrl控件添加CTreeCtrl變量m_tree; 函數
2.爲CTreeCtrl控件添加NM_CLICK消息響應函數OnClickTree();代碼以下: 測試
OnClickTree(NMHDR* pNMHDR, LRESULT* pResult) 遞歸
{
CPoint point;
UINT uFlag; //接收有關點擊測試的信息的整數
HTREEITEM hTree;
BOOL bCheck;
GetCursorPos(&point); //獲取屏幕鼠標座標
m_tree.ScreenToClient(&point); //轉化成客戶座標
hTree = m_tree.HitTest(point,&uFlag); //返回與CtreeCtrl關聯的光標的當前位置和句柄 it
if (hTree && (TVHT_ONITEMSTATEICON & uFlag)) //點中複選框
{ 變量
bCheck = m_treeSsid.GetCheck(hTree); //獲取當前複選狀態
SetChildCheck(hTree,!bCheck); //設置子項複選狀態
}
*pResult = 0;
} 總結
3.SetChildCheck()函數爲自定義的處理函數(響應點擊複選框後的具體處理函數): poi
下面的函數功能爲:父項選中或取消選中,子項跟住選中或取消選中; while
SetChildCheck(HTREEITEM hTree,BOOL bCheck)
{
hTree = m_tree.GetChildItem(hTree); //獲取子項句柄
while (hTree)
{ 消息
m_tree.SetCheck(hTree, bCheck);
SetChildCheck(hTree,bCheck); //遞歸調用
hTree = m_tree.GetNextSiblingItem(hTree); //獲取兄弟的句柄
}
} 遞歸調用
4.總結:關鍵是TVHT_ONITEMSTATEICON,它標誌鼠標點擊的是樹形控件的複選框;