@@ -28,8 +28,8 @@ void TreeView::init(HINSTANCE hInst, HWND parent, int treeViewID)
2828 _hSelf = CreateWindowEx (0 ,
2929 WC_TREEVIEW,
3030 TEXT (" Tree View" ),
31- TVS_HASBUTTONS | WS_CHILD | WS_BORDER | WS_HSCROLL |
32- TVS_HASLINES | TVS_HASBUTTONS | TVS_SHOWSELALWAYS | TVS_EDITLABELS | TVS_INFOTIP | WS_TABSTOP ,
31+ WS_CHILD | WS_BORDER | WS_HSCROLL | WS_TABSTOP | TVS_LINESATROOT | TVS_HASLINES |
32+ TVS_HASBUTTONS | TVS_HASBUTTONS | TVS_SHOWSELALWAYS | TVS_EDITLABELS | TVS_INFOTIP,
3333 0 , 0 , 0 , 0 ,
3434 _hParent,
3535 NULL ,
@@ -188,29 +188,13 @@ void TreeView::dragItem(HWND parentHandle, int x, int y)
188188 HTREEITEM targetItem = (HTREEITEM)::SendMessage (_hSelf, TVM_HITTEST, (WPARAM)0 , (LPARAM)&hitTestInfo);
189189 if (targetItem)
190190 {
191- // highlight the target of drag-and-drop operation
192- ::SendMessage (_hSelf, TVM_SELECTITEM, (WPARAM)(TVGN_DROPHILITE), (LPARAM)targetItem);
191+ if (canBeDropped (_draggedItem, targetItem))
192+ // highlight the target of drag-and-drop operation
193+ ::SendMessage (_hSelf, TVM_SELECTITEM, (WPARAM)(TVGN_DROPHILITE), (LPARAM)targetItem);
193194 }
194195
195196 // show the dragged image
196197 ::ImageList_DragShowNolock (true );
197-
198- /*
199- ImageList_DragMove(x-32, y-25); // where to draw the drag from
200-
201- ImageList_DragShowNolock(FALSE);
202- // the highlight items should be as
203-
204- // the same points as the drag
205- TVHITTESTINFO tvht;
206- tvht.pt.x = x-20;
207- tvht.pt.y = y-20; //
208- HTREEITEM hitTarget=(HTREEITEM)SendMessage(parentHandle,TVM_HITTEST,NULL,(LPARAM)&tvht);
209- if (hitTarget) // if there is a hit
210- SendMessage(parentHandle,TVM_SELECTITEM,TVGN_DROPHILITE,(LPARAM)hitTarget); // highlight it
211-
212- ImageList_DragShowNolock(TRUE);
213- */
214198}
215199
216200void TreeView::dropItem ()
@@ -237,17 +221,29 @@ void TreeView::dropItem()
237221 _draggedItem = 0 ;
238222 _draggedImageList = 0 ;
239223 _isItemDragged = false ;
224+ }
240225
241- /*
242- ImageList_DragLeave(_hSelf);
243- ImageList_EndDrag();
244- HTREEITEM Selected=(HTREEITEM)SendMessage(_hSelf,TVM_GETNEXTITEM,TVGN_DROPHILITE,0);
245- SendMessage(_hSelf,TVM_SELECTITEM,TVGN_CARET,(LPARAM)Selected);
246- SendMessage(_hSelf,TVM_SELECTITEM,TVGN_DROPHILITE,0);
247- ReleaseCapture();
248- ShowCursor(TRUE);
249- _isItemDragged = FALSE;
250- */
226+ bool TreeView::canBeDropped (HTREEITEM draggedItem, HTREEITEM targetItem)
227+ {
228+ if (draggedItem == targetItem)
229+ return false ;
230+ if (targetItem == TreeView_GetRoot (_hSelf))
231+ return false ;
232+ if (isDescendant (targetItem, draggedItem))
233+ return false ;
234+ return true ;
235+ }
236+
237+ bool TreeView::isDescendant (HTREEITEM targetItem, HTREEITEM draggedItem)
238+ {
239+ if (TreeView_GetRoot (_hSelf) == targetItem)
240+ return false ;
241+
242+ HTREEITEM parent = TreeView_GetParent (_hSelf, targetItem);
243+ if (parent == draggedItem)
244+ return true ;
245+
246+ return isDescendant (parent, draggedItem);
251247}
252248
253249void TreeView::moveTreeViewItem (HTREEITEM draggedItem, HTREEITEM targetItem)
0 commit comments