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:

  • intercepting OnDragEnter from (my extended) CRichEditView
  • using ON_NOTIFY_REFLECT(EN_DROPFILES, OnEnDropFiles)
  • calling DragAcceptFiles(false) on the CRichEditCtrl from the CRichEditView
  • calling RevokeDragDrop() on both the CRichEditCtrl and CRichEditView handles
  • calling DragAcceptFiles(true) from the CMainFrame/CMDIFrameWndEx

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?

2

There are 2 best solutions below

0
On BEST ANSWER

After a lot of trial and error, this is what worked for me in the end:

  • use CRichEditDoc instead of CDocument as suggested by xMRI, this gets rid of some of the issues
  • Add DragAcceptFiles(false) in the OnCreate of the class extending CRichEditView
  • Implement QueryAcceptData in the class extending CRichEditView and let it return S_FALSE
  • Add an OnDropFiles handler in the class extending CRichEditView (WM_DROPFILES)

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).

2
On

A CRichEditView requires a CRichEditDoc.

If you don't need a CRichEditView use your own CView implementation and implemnt the messages you need.

Here is a good MFC drag&drop sample.

And here another detailed description.