拖放 - 断言失败 [英] Drag and Drop - assertion failure
问题描述
我昨天发布了一个关于拖放的问题。
我只实现了拖动部分(来自我应用的数据)到其他应用程序)并且工作正常...除了一个细节。
例如。我想将文本从我的应用程序拖到记事本++。
如果notepad ++可见,那么每件事都能正常工作。
如果记事本++被最小化,我会从我的应用程序中拖动文本,转到资源管理器任务栏上的记事本++图标,将notepad ++显示为可见,然后将文本放在其上。工作正常,除了我将关闭我的应用程序的时间:cmdtarg.cpp行中有一个调试断言失败:43。
这是我的代码:
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:
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;
}
有谁知道为什么会这样?
祝你好运,
Filipe Marques
Can anybody knows why this happens?
Best regards,
Filipe Marques
推荐答案
我不确切知道会发生什么。但我有一些注意事项:
前两个函数用于删除。
你的代码会失败当您的项目是Unicode应用程序时。
根据您的Unicode项目设置,您必须使用匹配的CF_TEXT
或CF_UNICODETEXT
格式。另请注意,使用Drag& amp;时,Unicode和ANSI之间没有隐式转换。下降。所以最终的应用程序应该缓存两种格式的字符串。
你的代码没有显示DropData
的定义宾语。它不能是您的视图类的成员。在本地定义。
你可以尝试这个来检查它是否解决了你的问题:
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 matchingCF_TEXT
orCF_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 theDropData
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;
}
上面的示例在堆上分配 COleDataSource
对象。这是首选方式。这样做时,必须在不调用 SetClipboard()
时调用 InternalRelease()
。
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()
.
这篇关于拖放 - 断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!