This is the handler function Nm_click when the CTreeCtrl control is clicked
voidCdriverselctrl::onnmclick (NMHDR *pnmhdr, LRESULT *PResult) {CPoint curpoint; UINT nflags; Htreeitem HItem; GetCursorPos (&curpoint);//Current Point coordinatesScreenToClient (&curpoint);//screen coordinates converted to tree inner coordinatesHItem = HitTest (Curpoint, &nflags);//whether the coordinates have an item if(HItem && (Tvht_onitem & nflags))//Judging whether there is Htreeitem { This-SelectItem (HItem);//Click here to process the results after clicking}
//TODO: Add control notification handler code here*presult =0;}
It is important to point out here that the HitTest function can detect not only where the item is located on item, but also the description of MSDN.
Value |
meaning |
-
Tvht_above
|
Above the client area. |
-
Tvht_below
|
Below the client area. |
-
Tvht_nowhere
|
In the client area, but below the last item. |
-
Tvht_onitem
|
The bitmap or label associated with an item. |
-
Tvht_onitembutton
|
The button associated with an item. |
-
Tvht_onitemicon
|
The bitmap associated with an item. |
-
Tvht_onitemindent
|
In the indentation associated with an item. |
-
Tvht_onitemlabel
|
The label (string) associated with an item. |
-
Tvht_onitemright
|
The area to the right of a item. |
-
Tvht_onitemstateicon
|
On the State, icon for a Tree-view item, is in a user-defined state. |
-
Tvht_toleft
|
The left of the client area. |
-
Tvht_toright
|
The right of the client area. |
This allows for more flexible use of the Ctreectl control.
For example, the following example:
voidCdriverselctrl::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: Add control notification handler code here*presult =0;}
Here is the function when the option changes
// This is the handler function when the control's selection has changed void Cdriverselctrl::ontvnselchanged (NMHDR *pnmhdr, LRESULT *pResult) { = Reinterpret_cast<lpnmtreeview >(PNMHDR);p nmtreeview// check htreeitem// last selected Htreeitem// TODO: Add control notification handler code 0 here ;}
VC + + CTreeCtrl use Nm_click and tvn_selchanged