这里以CTreeCtrl为例
1.首先鼠标OnLButtonDown选中节点
void CTreeX::OnLButtonDown(UINT nFlags, CPoint point)
{
UINT uFlags;
HTREEITEM hItem = HitTest(point, &uFlags);
if ((hItem != NULL) && (TVHT_ONITEM & uFlags))
SelectItem(hItem);
CTreeCtrl::OnLButtonDown(nFlags, point);
}
2.按着左键开始拖拽
void CTreeX::OnTvnBegindrag(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
UINT uFlags;
HTREEITEM hItem = HitTest(pNMTreeView->ptDrag, &uFlags);
if ((hItem != NULL) && (TVHT_ONITEM & uFlags))
SelectItem(hItem);
SetCursor(AfxGetApp()->LoadCursor(IDC_FO_DROP_CURSOR)); // 设置拖拽时的光标
m_bDrag = TRUE; // 自己写个变量,标记正在拖拽
*pResult = 0;
}
3.光标的设置,要使用自己设置的光标必须重载此函数
BOOL CTreeX::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
if (m_bDrag)
return TRUE;
return CTreeCtrl::OnSetCursor(pWnd, nHitTest, message);
}