Drag &Drop

来源:互联网 发布:将数组赋值给指针 编辑:程序博客网 时间:2024/05/01 22:24

 

Hi!
I try to put a HTREEITEM in the clipborad and read from clipboard with drag and drop and i don't quite understand the techniqe. Is there anyone who could help me out of this problem, please?
Here is my code:

void CTree::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
TRACE("CTree::OnBegindrag/n");
MyFormat= RegisterClipboardFormat("CF_DWORD");
HTREEITEM hTSelItem = pNMTreeView->itemNew.hItem;
// Highlight selected item
m_IdbTreeCtrl->SelectItem(hTSelItem);
if(m_IdbTreeCtrl->Select(hTSelItem, TVGN_DROPHILITE) == 0)
return;
DWORD pDword = m_IdbTreeCtrl->GetItemData(hTSelItem);
COleDataSource* pSource = new COleDataSource();
HGLOBAL hDword = ::GlobalAlloc(GMEM_FIXED,6);
pDword = (DWORD) ::GlobalLock(hDword);
ASSERT(pDword);
::GlobalUnlock(hDword);
pSource->CacheGlobalData(MyFormat,hDword);
m_bDragHere = true;
DROPEFFECT dropEffect = pSource->DoDragDrop(DROPEFFECT_COPY);
TRACE("after DoDragDrop -- dropEffect = %ld/n", dropEffect);
if (dropEffect == DROPEFFECT_MOVE && m_bDragHere) {
::GlobalUnlock(hDword);
::GlobalFree(hDword);
}
m_bDragHere = FALSE;
delete pSource;
*pResult = 0;
}

BOOL CTree::OnDrop(COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point)
{
TRACE("CTree::OnDrop/n");
if (!pDataObject->IsDataAvailable(MyFormat)) {
TRACE("MyFormat is unavailable/n");
return FALSE;
}
pDataObject->GetIDataObject(TRUE);
HGLOBAL hTree = pDataObject->GetGlobalData(MyFormat);
ASSERT(hTree != NULL);
DWORD dWord = (DWORD)::GlobalLock(hTree);
if(dWord == NULL) {
TRACE("lpTree is NULL/n");
return FALSE;
}
// Copy this node to target tree
GetDocument()->SetModifiedFlag();
return TRUE;
}

DROPEFFECT CTree::OnDragOver(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point)
{
// TODO: Add your specialized code here and/or call the base class
TRACE("CTree::OnDragOver/n");
return CTreeView::OnDragOver(pDataObject, dwKeyState, point);
}

void CTree::OnDragLeave()
{
// TODO: Add your specialized code here and/or call the base class
TRACE("CTree::OnDragLeave()/n");
CTreeView::OnDragLeave();
}

DROPEFFECT CTree::OnDragEnter(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point)
{
// TODO: Add your specialized code here and/or call the base class
TRACE("CTree::OnDragEnter/n");
return CTreeView::OnDragEnter(pDataObject, dwKeyState, point);
}

}

For the first i do not know which ClipFormat should i use in CacheGlobalData(..,..) . i have created my own format and i 'm not so sure that it is correct. And why CTree object dosen't get any Drop message? I get OnBeginDrag , OnDragEnter,OnDragOver and OnDragLeave messages and after that nothing happens. :cry:
 
=================================================================
Yes, i have fixed it and i get onDrop message. Here is the new code:
DROPEFFECT CTree::OnDragOver(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point)
{
// TODO: Add your specialized code here and/or call the base class
TRACE("CTree::OnDragOver/n");
// return CTreeView::OnDragOver(pDataObject, dwKeyState, point);
return DROPEFFECT_MOVE;
}

void CTree::OnDragLeave()
{
// TODO: Add your specialized code here and/or call the base class
TRACE("CTree::OnDragLeave()/n");
//CTreeView::OnDragLeave();

}

DROPEFFECT CTree::OnDragEnter(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point)
{
// TODO: Add your specialized code here and/or call the base class
TRACE("CTree::OnDragEnter/n");
//return CTreeView::OnDragEnter(pDataObject, dwKeyState, point);
return OnDragOver(pDataObject, dwKeyState, point);
}

But i still have problem with putting data on the clipborad specially first parameter on CacheGlobalData(), i don't know which format must i use if i want to put pointer to CMyBaseClass (placed in the heap) in the clipbord and reterive it OnDrop() function! Do you have any tip how should i do?
Here is the code for these two functions (little diffeerent now):


void CTree::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
TRACE("CTree::OnBegindrag/n");
HTREEITEM hTSelItem = pNMTreeView->itemNew.hItem;
// Highlight selected item
m_IdbTreeCtrl->SelectItem(hTSelItem);
if(m_IdbTreeCtrl->Select(hTSelItem, TVGN_DROPHILITE) == 0)
return;
DWORD m_dword = m_IdbTreeCtrl->GetItemData(hTSelItem);
COleDataSource* pSource = new COleDataSource();
HGLOBAL hBase = ::GlobalAlloc(GMEM_FIXED,6);
CMyBaseClass* pBase = (CMyBaseClass*) ::GlobalLock(hBase);
ASSERT(pBase);
pBase = (CMyBaseClass*) m_dword;
::GlobalUnlock(hBase);
pSource->CacheGlobalData(CF_DIB,pBase);
m_bDragHere = true;
DROPEFFECT dropEffect = pSource->DoDragDrop();
TRACE("after DoDragDrop -- dropEffect = %ld/n", dropEffect);
if (dropEffect == DROPEFFECT_COPY && m_bDragHere) {
::GlobalUnlock(hBase);
::GlobalFree(hBase);
}
m_bDragHere = FALSE;
delete pSource;
*pResult = 0;
}

BOOL CTree::OnDrop(COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point)
{
TRACE("CTree::OnDrop/n");
if (!pDataObject->IsDataAvailable(CF_DIB)) {
TRACE("MyFormat is unavailable/n");
return FALSE;
}
pDataObject->GetIDataObject(TRUE);
HGLOBAL hGlobal = pDataObject->GetGlobalData(CF_DIB);
ASSERT(hGlobal != NULL);
CMyBaseClass* pBase = (CMyBaseClass*)::GlobalLock(hGlobal);
if(pBase == NULL) {
TRACE("pBase is NULL/n");
return FALSE;
}
// Copy this node to target tree
GetDocument()->SetModifiedFlag();
return TRUE;
}
:confused:
=============================================================
Packing:

FORMATETC fmt = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
CacheGlobalData( CF_HDROP, hGlobal, &fmt );


Unpacking:

FORMATETC fmt = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
STGMEDIUM stg = { TYMED_HGLOBAL };

pDataObject->GetData( CF_HDROP, &stg, &fmt );

HGLOBAL hGlobal = stg.hGlobal;
GlobalLock( hGlobal );
// extract data
GlobalUnlock( hGlobal );
ReleaseStgMedium( &stg );
原创粉丝点击