有关CTreeCtrl拖拽的问题,请教!

huazaimh 2005-09-28 11:01:21
下面是有关拖拽的源代码,但是拖拽的时候拖拽的图标离鼠标有比较大的距离,不知道是怎么回事
各位大哥大姐能不能指点一下啊??
void CXTreeCtrl::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here

*pResult = 0;

//Èç¹ûÊÇÎÞÒâÍÏÒ·£¬Ôò·ÅÆú²Ù×÷
if( (GetTickCount() - m_dwDragStart) < DRAG_DELAY )
return;

m_hItemDragS = pNMTreeView->itemNew.hItem;
m_hItemDragD = NULL;

//µÃµ½ÓÃÓÚÍ϶¯Ê±ÏÔʾµÄͼÏóÁбí
m_pDragImage = CreateDragImage( m_hItemDragS );
if( !m_pDragImage )
return;

m_bDragging = true;
m_pDragImage->BeginDrag ( 0, CPoint(8,8));
CPoint pt = pNMTreeView->ptDrag;
ClientToScreen( &pt );

m_pDragImage->DragEnter ( NULL,pt); //"this"½«ÍÏÒ·¶¯×÷ÏÞÖÆÔڸô°¿Ú
SetCapture();

m_nScrollTimerID = SetTimer( 2,40,NULL );

}
...全文
239 点赞 收藏 10
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
dnliophsam 2006-03-22
标记一下,我也正好想学习
回复
雪7狼 2006-03-22
m_pDragImage->DragEnter ( NULL,pt);
改为m_pDragImage->DragEnter ( this,pt); //this"将拖曳动作限制在该窗口
回复
zwl327 2006-03-22
修改这两个值就行.

pt.x = pNMTreeView->ptDrag.x - (rect.left-16);
pt.y = pNMTreeView->ptDrag.y - rect.top;
回复
huazaimh 2005-10-12
这个帖子不能沉啊!
没有最后搞定呢!
回复
SanTa 2005-10-12
收藏
回复
luokun 2005-10-12
给你一个函数,你用它替代CreateDragImage试试。

CImageList* CreateDragImageEx(HTREEITEM hItem)
{
if(GetImageList(TVSIL_NORMAL) != NULL)
return CreateDragImage(hItem);

CRect rect;
GetItemRect(hItem, rect, TRUE);
rect.top = rect.left = 0;

// Create bitmap
CClientDC dc (this);
CDC memDC;

if(!memDC.CreateCompatibleDC(&dc))
return NULL;

CBitmap bitmap;
if(!bitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height()))
return NULL;

CBitmap* pOldMemDCBitmap = memDC.SelectObject( &bitmap );
CFont* pOldFont = memDC.SelectObject(GetFont());

memDC.FillSolidRect(&rect, RGB(0, 255, 0)); // Here green is used as mask color
memDC.SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
memDC.TextOut(rect.left, rect.top, GetItemText(hItem));

memDC.SelectObject( pOldFont );
memDC.SelectObject( pOldMemDCBitmap );

// Create imagelist
CImageList* pImageList = new CImageList;
pImageList->Create(rect.Width(), rect.Height(),
ILC_COLOR | ILC_MASK, 0, 1);
pImageList->Add(&bitmap, RGB(0, 255, 0)); // Here green is used as mask color

return pImageList;
}
回复
goodboyws 2005-10-12
这是我以前写的相关代码你参考一下
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
HTREEITEM hItem = pNMTreeView->itemNew.hItem;
int nData = m_cUserTree.GetItemData(hItem);
if (nData == -1)
{
*pResult = 0;
return;
}
m_hTreeItem = hItem;
m_pDragImageList = m_cUserTree.CreateDragImage(m_hTreeItem);
CRect rect;
if (m_pDragImageList)
{
SetCapture();
m_cUserTree.GetItemRect(m_hTreeItem, rect, TRUE);
CPoint pt;
pt.x = pNMTreeView->ptDrag.x - (rect.left-16);
pt.y = pNMTreeView->ptDrag.y - rect.top;
CImageList::DragShowNolock(TRUE);
m_pDragImageList->BeginDrag(0, pt);
CPoint ptAction;

GetCursorPos(&ptAction);
m_cUserTree.ScreenToClient(&ptAction);
CImageList::DragMove(ptAction);
m_cUserTree.SelectDropTarget(m_hTreeItem);
CImageList::DragEnter(&m_cUserTree, ptAction);
m_pWndLocked = &m_cUserTree;
m_bDrag = TRUE;
}
*pResult = 0;
回复
huazaimh 2005-10-02
上面的大哥,ClientToScreen( &pt )我也试过了,但是不行
可能是别的原因
回复
phoenix96_2000 2005-09-28

ClientToScreen( &pt );
去掉试试呢?
回复
cnwolf 2005-09-28
帮你up
回复
发帖
界面
创建于2007-09-28

1.5w+

社区成员

VC/MFC 界面
申请成为版主
帖子事件
创建了帖子
2005-09-28 11:01
社区公告
暂无公告