如何对CTreeCtrl的项进行拖曳
(1) 取消TVS_DISABLEDRAGDROP 样式,该样式会禁止发送TVN_BEGINDRAG消息
(2) 派生一个类于CTreeCtrl,定义几个成员变量:
CImageList *m_pDragImage;//拖曳图像列表指针
BOOL m_bLDragging;//是否拖曳
HTREEITEM m_hitemDrag,m_hitemDrop;//拖曳项和目标项
(3)响应拖曳消息TVN_BEGINDRAG
void CTreeCtrlEx::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
m_hitemDrag=pNMTreeView->itemNew.hItem;//获得拖曳项
m_hitemDrop=NULL;
m_pDragImage=CreateDragImage(m_hitemDrag);//创建拖曳图像
if(!m_pDragImage)
return;
this->m_bLDragging=TRUE;
m_pDragImage->BeginDrag(0,CPoint(-15,-15));//后面的point表示拖曳图像离拖曳项的相对位置
POINT pt=pNMTreeView->ptDrag;
ClientToScreen(&pt);
m_pDragImage->DragEnter(NULL,pt);//开始显示拖曳图像
SetCapture();//锁定鼠标
*pResult = 0;
}
(4)响应鼠标移动消息
void CTreeCtrlEx::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
HTREEITEM hitem;
UINT flags;
if(m_bLDragging)
{
POINT pt=point;
ClientToScreen(&pt);
CImageList::DragMove(pt);//当鼠标移动时也移动拖曳的图像
if((hitem=HitTest(point,&flags))!=NULL)//确定鼠标所在的项
{
CImageList::DragShowNolock(FALSE);//隐藏拖曳图像
SelectDropTarget(hitem);
m_hitemDrop=hitem;//确定目标项
CImageList::DragShowNolock(TRUE);//重新显示
}
}
CTreeCtrl::OnMouseMove(nFlags, point);
}
(5)鼠标弹起消息
void CTreeCtrlEx::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bLDragging)//是否拖动
{
m_bLDragging=FALSE;
CImageList::DragLeave(this);
CImageList::EndDrag();//结束拖曳
ReleaseCapture();
delete m_pDragImage;
SelectDropTarget(NULL);
if(m_hitemDrag==m_hitemDrop)
return;
MessageBox("拖曳成功!");
。。。。现在已经获知了拖曳项和目标项,就看你自己需要什么代码了,略去。
}
CTreeCtrl::OnLButtonUp(nFlags, point);
}
评论