I have written a MDI app in MFC, where a document window contains a CRichEditView among other things. When I drag a file into the document window, onto the CRichEditView, it crashes here (from afxrich.inl):
_AFXRICH_INLINE CRichEditDoc* CRichEditView::GetDocument() const
{
AFXASSUME(m_pDocument != NULL);
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CRichEditDoc))); <<< crash!
return (CRichEditDoc*)m_pDocument;
}
The crash happens because m_pDocument is not a CRichEditDoc (it is a class of mine derived by CDocument).
However, I don't care about the RichEditView/Ctrl, what I want is that when I drag and drop a file into the application, it is simply opened as a document. This already happens if my MDI app is empty: I drag and drop a file into the app and it gets opened. If I try the same drag and drop when a document window is already opened, it crashes as described above instead of opening it. I tried multiple solutions to override this behavior, but none works:
none of these is able to prevent the RichEdit from taking the drop operation (and crashing), they seem to have no effect at all. All I want is for the CRichEditView/CRichEditCtrl not to intercept the dropped file, and to let it intercept from the CMainFrame/CMDIFrameWndEx instead.
Does anyone know how to solve this?
After a lot of trial and error, this is what worked for me in the end:
With all these in place now whenever I drop files in the application, even if they are on the RichEdit, they are still intercepted by the main frame and opened as new documents in the MDI. This also suppresses drag and drop where you are not dropping files (e.g. you drop some text you drag from notepad).