步驟如下:
1.使用GetCursorPos()來取得目前滑鼠座標。
2.使用CTreeCtrl::ScreenToClient()轉換滑鼠座標位置到CTreeCtrl的相對位置。
3.使用CTreeCtrl::HitTest()來取得此座標位置是否有Item存在。
4.判斷HitTest()回傳的Flag狀態是否為TVHT_ONITEM。
5.如上述步驟都成立,則HitTest()回傳之HTREEITEM則為所要之Item。如此值為NULL,則代表此座標位置並無Tree Item存在。
程式碼如下:
CPoint pt;
UINT unFlag = 0;
::GetCursorPos(&pt);
pTree->ScreenToClient(&pt);
//When this function is called, the pt parameter specifies the coordinates of the point to test.
//The function returns the handle of the item at the specified point or NULL if no item occupies the point.
//In addition, the pFlags parameter contains a value that indicates the location of the specified point.
HTREEITEM htreeHitItem = pTree->HitTest(pt, &unFlag);
if ( htreeHitItem == NULL || !(unFlag&TVHT_ONITEM) ) //No hit item, and not on item
{
//Just show Create
return false;
}else if ( htreeHitItem && (unFlag & TVHT_ONITEM) ) {
//Just show edit and delete
pTree->Select(htreeHitItem, TVGN_CARET); //Set this item be selected
//pTree->SelectItem(htreeHitItem); //用這個也可以
}else{ //Not allow
return false;
} //End of if ( htreeHitItem == NULL || !(unFlag&TVHT_ONITEM) )
如需再指定此Item被選擇,僅需使用CTreeCtrl::Select()或是CTreeCtrl::SelectItem()即可。
No comments:
Post a Comment