//这是当CTREECTRL控件点击时NM_CLICK的处理函数
void CDriverSelCtrl::OnNMClick(NMHDR *pNMHDR, LRESULT *pResult) { CPoint curPoint; UINT nFlags; HTREEITEM hItem; GetCursorPos(&curPoint); //当前点坐标 ScreenToClient(&curPoint); //屏幕坐标转换为TREE内坐标 hItem = HitTest(curPoint, &nFlags); //坐标是否有ITEM if (hItem && (TVHT_ONITEM & nFlags)) //判断是否有HTREEITEM { this->SelectItem(hItem);//在这里处理点击后的结果 }
// TODO: 在此添加控件通知处理程序代码 *pResult = 0; }
在这里需要指出 HitTest 函数不但可以检测出是否在item上 也可以检测出 位于 item项的 哪个位置请看 MSDN的说明
Value | Meaning |
---|---|
|
Above the client area. |
|
Below the client area. |
|
In the client area, but below the last item. |
|
On the bitmap or label associated with an item. |
|
On the button associated with an item. |
|
On the bitmap associated with an item. |
|
In the indentation associated with an item. |
|
On the label (string) associated with an item. |
|
In the area to the right of an item. |
|
On the state icon for a tree-view item that is in a user-defined state. |
|
To the left of the client area. |
|
To the right of the client area. |
由此可以更加灵活的使用的CTREECTL控件了
例如下面 例子:
void CDriverSelCtrl::OnNMClick(NMHDR *pNMHDR, LRESULT *pResult) { CPoint point; UINT uFlag; BOOL bCheck; GetCursorPos(&point); CTreeCtrl::ScreenToClient(&point); HTREEITEM hTree = CTreeCtrl::HitTest(point, &uFlag); if (hTree && (TVHT_ONITEMSTATEICON & uFlag)) { CTreeCtrl::SelectItem(hTree); bCheck = CTreeCtrl::GetCheck(hTree); } // TODO: 在此添加控件通知处理程序代码 *pResult = 0; }
下面是关于 选项改变时的函数
//这是当控件的选择发生变化时的处理函数 void CDriverSelCtrl::OnTvnSelchanged(NMHDR *pNMHDR, LRESULT *pResult) { LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR); pNMTreeView->itemNew //选中HTREEITEM pNMTreeView->itemOld //上次选中HTREEITEM // TODO: 在此添加控件通知处理程序代码 *pResult = 0; }