Link to home
Start Free TrialLog in
Avatar of yingchunli
yingchunliFlag for Canada

asked on

Window Message

I have a MFC MDI application (Myapp). I select CEditView as the view's base class and add DragAcceptFiles to the InitInstance() in myapp.cpp, the application can accept dropping of multi-files. But as I add OnDropFiles()  Window-message function to my view class by ClassWizzard, it is not called at all as I drop files (although the files are opened). What might be the problem(s)?  
Avatar of jhance
jhance

CEditView doesn't normally receive the WM_DROPFILES message.  It's sent to the containing frame window.  If you want to receive and process WM_DROPFILES in a CEditView, you need to derive your own class from CEditView and then subclass it.  Then you will be able to use your own message handler.  Usually, it's just easier to process WM_DROPFILES in the frame window and send the files to the appropriate CEditView.
Avatar of yingchunli

ASKER

Hi jhance:

Pleased to talk to you another time.

It is ok for the MainFram class. But I am more interested in having OnDropFiles() in CMyappView class.

My CMyappView class is derived from CEditView, and the message handler WS_DROPFILES is there (I suppose it is my "own" message handler for the subclass). I still do not know how to make the function OnDropFiles in my CMyappView work.

Each window can be a drop target. You cannot do what you're trying to do in the app's InitInstance(). You need to call DragAcceptFiles() in your view's InitialUpdate() or OnCreate() methods.


I think it is OK to call  DragAcceptFiles() in the app's InitInstance(). It turns out that every window can be a drop target. My problem is that the WS_DROPFILES message does not cause the processing of OnDropFile() in my view class (derived from CEditView). If I really want to do it this way, what I need to do?
There's no reason to derive your own class from CEditView, as you _must've_ already done that because CEditView is an abstract base class. AppWizard and ClassWizard do the derivation for if you've used them to get the class into your app.

You don't mention which window you're calling DragAcceptFiles() upon in your InitInstance().  I guess it must be your frame window.

If you're calling it on your frame window, MFC itself is handling the drag-and-drop operation for you because MFC's own CFrameWnd class supplies an OnDropFiles() handler that simply tries to open the dragged files by calling CYourApp::OpenDocumentFile() on each of the passed file names.

Can add a handler to OnDropFiles() in your your own CFrameWnd-derived window and handle the drop operation any way you'd like to.

It's also wrong to think that about the view is not offered the dropped files; just call DragAcceptFiles() on whichever window you want to be the drop target. You're right, it is OK to call DragAcceptFiles() in InitInstance(), but InitInstance() only runs once in your application--and you may have several instances of your view, or you might destroy and recreate your view.

So, if you must handle the drop message in your view, you must call DragAcceptFiles() on your view class. Putting that code in InitInstance() of the view is a fine place to do that. Then, write an OnDropFiles() handler for the WM_DROPFILES message and process the dropped files in whatever way you'd like.

If you're making your view the drop target, only your view is the drop target--dropping on your frame, on the MDI Client Area, or on the child frame will not result in WM_DROPFILES being sent to your window.

.B keiM

Hi   Mikeblas:

Thanks for you helpful points. But there is one thing on which I did not get your idea.

You said: "if you must handle the drop message in your view, you must call DragAcceptFiles() on your view class."

I just have no idea on how to do this. please show me where and how you can call this function and then make the OnDropFiles() work.


ASKER CERTIFIED SOLUTION
Avatar of mikeblas
mikeblas

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial