This is a crib sheet for how to add drag and drop to a dialog. It documents the steps I found necessary. Note, this isn't a tutorial, just a collection in one place of all the information required, you should check your documentation for full details of the methods sketched out here (especially to confirm their argument lists).
I first wrestled with these issues when developing AscToHTM (http://www.jafsoft.com/asctohtm/) the text to HTML converter used to generate this HTML page from this source file .
Introduction
Dragging Files
More general Drag and drop
Things to check
Miscellaneous notes
Dragging Files can be supported relatively easily as follows
Make the following changes
// Generated message map functions //{{AFX_MSG(A2hDialog) ... afx_msg void OnDropFiles(HDROP hDropInfo); ... //}}AFX_MSG DECLARE_MESSAGE_MAP()
BEGIN_MESSAGE_MAP(MyDialog, CDialog) //{{AFX_MSG_MAP(MyDialog) ... ON_WM_DROPFILES() ... //}}AFX_MSG_MAP END_MESSAGE_MAP()
Strangely, after doing (1) and (2) the results become visible in the ClassWizard.
this->DragAcceptFiles(TRUE);
This enables Drag and drop of files on your Dialog's window. You can check this by dragging files over your Window. If the cursor is a "no entry" sign (circle with a line through it) it's not working. If the cursor changes to a file/folder icon with a "+" on it, you're in business.
void MyDialog::OnDropFiles(HDROP hDropInfo) { HDROP m_hDropInfo = hDropInfo; CString Filename; if (m_hDropInfo) { int iFiles = DragQueryFile(m_hDropInfo, (UINT)-1, NULL, 0); for (int i=0; i<ifiles; i++) { char* pFilename = Filename.GetBuffer(_MAX_PATH); DragQueryFile(m_hDropInfo, i, pFilename, _MAX_PATH); // do whatever... } // for each files... } // if DropInfo DragFinish(m_hDropInfo); m_hDropInfo = 0; } // End of OnDropFiles
I've not tried this, but I read around the subject. The more general approach (for objects other than files) seems to be as follows
m_DropTarget.Register (this);
to register the Dialog with the DropTarget
OnDragEnter
OnDragLeave // probably not needed for a target
OnDrop
The OnDragEnter should return a value (a DROPEFFECT) indicating that your window will accept drop events
The OnDrop method handles the actual drop
home - contact us - news - product index - search this site |