Click here to Skip to main content
15,896,522 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I posted a question yesterday about drag and drop.
I only implemented the drag part (data from my app to other app) and works fine...except one detail.

For exemple. I want to drag text from my app to notepad++.
If notepad++ is visible, every thing works normally.
If notepad++ is minimized, I drag the text from my app, go to the notepad++ icon on explorer taskbar to turn notepad++ visible and I drop the text on it. Works fine, except the time that I will close my application: there is a debug assertion failure in cmdtarg.cpp line:43.

Here is my code:

C++
void CFilesDragDropView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();

	VERIFY( m_DropTarget.Register(this) );
}

DROPEFFECT CFilesDragDropView::OnDragOver(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point) 
{
	if( ! pDataObject->IsDataAvailable( CF_TEXT ) ){
		return DROPEFFECT_NONE;
	}

	DROPEFFECT de = DROPEFFECT_COPY;	
	return de; 
}

void CFilesDragDropView::OnBeginDrag(NMHDR* pNMHDR, LRESULT* pResult) 
{
	HGLOBAL hMem = ::GlobalAlloc(GMEM_ZEROINIT|GMEM_MOVEABLE|GMEM_DDESHARE, 10);
	memcpy( (char*)::GlobalLock(hMem), _T("ola mundo"), 10);
	::GlobalUnlock(hMem);

	DropData.CacheGlobalData( CF_TEXT , hMem );
	DROPEFFECT de = DropData.DoDragDrop(DROPEFFECT_COPY);
	
	*pResult = 0;
}


Can anybody knows why this happens?

Best regards,
Filipe Marques
Posted

1 solution

I don't know exactly what happens. But I have some notes:

The first two functions are for dropping.

Your code will fail when your project is an Unicode application.

Depending on your Unicode project setting, you must use the matching CF_TEXT or CF_UNICODETEXT format. Note also that there is no implicit conversion between Unicode and ANSI when using Drag & Drop. So the final application should cache strings in both formats.

Your code does not show the definition of the DropData object. It must not be a member of your view class. Define it locally.

You may try this to check if it solves your problem:
void CFilesDragDropView::OnBeginDrag(NMHDR* pNMHDR, LRESULT* pResult) 
{
    LPCTSTR s = _T("ola mundo"); // string to be dragged
    size_t nSize = (_tcslen(s) + 1) * sizeof(TCHAR);
    HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE, nSize);
    memcpy((char*)::GlobalLock(hMem), s, nSize);
    ::GlobalUnlock(hMem);
    
    COleDataSource * pDataSrc = new COleDataSource; 
#ifdef _UNICODE
    pDataSrc->CacheGlobalData(CF_UNICODETEXT, hMem);
#else
    pDataSrc->CacheGlobalData(CF_TEXT, hMem);
#endif
    DROPEFFECT de = pDataSrc->DoDragDrop(DROPEFFECT_COPY);
    // Must call InternalRelease() when object allocated on the heap.
    pDataSrc->InternalRelease();
    	
    *pResult = 0;
}

The above example allocates the COleDataSource object on the heap. This is the preferred way. When doing so, InternalRelease() must be called afterwards when not calling SetClipboard().
 
Share this answer
 
Comments
Filipe Marques 27-Sep-13 17:38pm    
Hi Jochen again :) This was just a test, to understand the process. So I did not check the if the project is a UNICODE or not because I knew the it was a non UNICODE. But yes, I must take in consideration. About the first two methods, you are right. Last night my head was over heat and I did not read properly the documentation and I thought that the drop target was my window. So, finally, the problem was the COleDataSource variable. It was a member of my class. I did not know that detail. Again, thanks for your help Jochen. Best regards, Filipe Marques

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900