上位机MFC控件Treeview节点位置任意拖放源代码
如上图,我们可以通过鼠标将CTreeview的节点任意拖放。
以调节其位置。
主要是在鼠标操作函数内实现。
比如鼠标放开函数
- void CDragView::OnLButtonUp(UINT nFlags, CPoint point)
- {
- CTreeView::OnLButtonUp(nFlags, point);
- if( m_bLDragging )
- {
- CTreeCtrl& theTree = GetTreeCtrl();
- m_bLDragging = false;
- CImageList::DragLeave(this);
- CImageList::EndDrag();
- ReleaseCapture();
- delete m_pDragImage;
- theTree.SelectDropTarget(NULL);
- m_htiOldDrop = NULL;
- if( m_htiDrag == m_htiDrop )
- return;
- HTREEITEM htiParent = m_htiDrop;
- while( (htiParent = theTree.GetParentItem(htiParent)) != NULL )
- {
- if( htiParent == m_htiDrag ) return;
- }
- theTree.Expand( m_htiDrop, TVE_EXPAND );
- HTREEITEM htiNew = CopyBranch( m_htiDrag, m_htiDrop, TVI_LAST );
- theTree.DeleteItem(m_htiDrag);
- theTree.SelectItem( htiNew );
- if( m_idTimer )
- {
- KillTimer( m_idTimer );
- m_idTimer = 0;
- }
- }
- }
复制代码 比如鼠标移动函数
- void CDragView::OnMouseMove(UINT nFlags, CPoint point)
- {
- CTreeView::OnMouseMove(nFlags, point);
- HTREEITEM hti;
- UINT flags;
- if( m_bLDragging )
- {
- CTreeCtrl& theTree = GetTreeCtrl();
- POINT pt = point;
- ClientToScreen( &pt );
- CImageList::DragMove(pt);
-
- hti = theTree.HitTest(point,&flags);
- if( hti != NULL )
- {
- CImageList::DragShowNolock(FALSE);
- if( m_htiOldDrop == NULL )
- m_htiOldDrop = theTree.GetDropHilightItem();
- theTree.SelectDropTarget(hti);
-
- m_htiDrop = hti;
-
- if( m_idTimer && hti == m_htiOldDrop )
- {
- KillTimer( m_idTimer );
- m_idTimer = 0;
- }
-
- if( !m_idTimer )
- m_idTimer = SetTimer( 1000, 2000, NULL );
- CImageList::DragShowNolock(TRUE);
- }
- }
- }
复制代码 更多函数可以下载例程查看。
|