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的說明
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; }