EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

Drag and Drop

Win32 subsystem facilitates files and object transfer between two applications using drag and drop. Drag and drop is a mouse operation. User clicks on some file(s) in file explorer or some OLE object in application like Word and moves mouse cursor to other application window with out realising left mouse button. This operation is called drag. Now releaseing left mouse button in the other application window is called drop operation. When drag and drop operations are made in conjunction, a drag-n-drop operation happens. This creates a visual effect and object seems moves between two windows and mouse cursor changes. An application should prepare its window to accept drop operations.

There are Win32 APIs to enble this operation and Windows sends event when drop operation is performed. drag-n-drop is mainly with two types -

  1. file/folder drag and drom from windows exploler or other file manager applications.
  2. OLE objects like simple text, rich edit text, html, bitmaps, pictures etc drag and drop from other aplications.

We are discussing file drag and drop events here. Before going the the demo aplication we should know the Win32 APIs and Windows attributes related to drag and drop.


WS_EX_ACCEPTFILES is the extended windows style to pass the parameter in CreateWindowEx() to give the capability of accepting dropped files from windows explorer or file manager applications. An active and visible window receives the file drop event (WM_DROPFILES) when this extended style is applied.

Accept files

Extended Window/Dialog style Accept files

While create a dialog in resource editor, WS_EX_ACCEPTFILES can be applied by selecting "Accept files" checkbox.


Registers whether a window accepts dropped files nor not. DragAcceptFiles function call changes windows style/capability of WS_EX_ACCEPTFILES at runtime.

VOID DragAcceptFiles(
   HWND hWnd,
   BOOL fAccept
Parameters hWnd - Handle to the window fAccept- TRUE to accept dropped files or FALSE to discontinue accepting dropped files. Return value No return value. Remarks DragAcceptFiles() can be called with FALSE to disable file drop operation.


Window receives this event during drag-n-drop operation. Windows with WS_EX_ACCEPTFILES can only receives this event. Windows explorer and file manager applications are sender and our demo application is the receiver part of this message. File manager application detects the mouse drag even and calls PostMessage() with message id WM_DROPFILES and one or more file path information as a handle in wParam papameter.

    (HWND) hWndControl,   // handle to destination control
    (UINT) WM_DROPFILES,  // message ID
    (WPARAM) wParam,      // = (WPARAM) (HDROP) hDrop;
    (LPARAM) lParam       // = 0; not used, must be zero 


hWndControl - Handle to the target control msgID - WM_DROPFILES hDrop - A handle to an internal structure describing the dropped files. Pass this handle DragFinish, DragQueryFile, or DragQueryPoint to retrieve information about the dropped files. lParam - ust be zero.

Return value

An application should return zero if it processes this message.


The HDROP handle is declared in Shellapi.h. You must include this header in your build to use WM_DROPFILES. For further discussion of how to use drag-and-drop to transfer Shell data, see Transferring Shell Dat

DragQueryFile function

Retrieves the names of dropped files that result from a successful drag-and-drop operation.

UINT DragQueryFile(
  HDROP  hDrop,
  UINT   iFile,
  LPTSTR lpszFile,
  UINT   cch


  • hDrop - Handle of the structure that contains the file names of the dropped files.
  • iFile - Index of the file to query. If the value of this parameter is 0xFFFFFFFF, DragQueryFile returns a count of the files dropped. If the value of this parameter is between zero and the total number of files dropped, DragQueryFile copies the file name with the corresponding value to the buffer pointed to by the lpszFile parameter.
  • lpszFile- The address of a buffer that receives the file name of a dropped file when the function returns. This file name is a null-terminated string.
  • If this parameter is NULL, DragQueryFile returns the required size, in characters, of this buffer.
  • cch - The size of the lpszFile buffer.

Return value

A nonzero value indicates a successful call.

  • Returns number of dropped files - Pass iFile as 0xFFFFFFFF to know how many files are dropped to this window.
  • Returns length of the file path - Pass lpszFile as NULL to index as iFile to know the size of the buffer needed.
  • Returns file path string in lpszFile - Pass iFile index between 0 to count -1 and lpszFile as a pointer to a buffer and cch as the length of the buffer.

DragFinish function

Releases memory that the system allocated for use in transferring file names to the application.

VOID DragFinish(
   HDROP hDrop


  • hDrop- Handle that describes dropped files. This handle is retrieved from the wParam parameter of the WM_DROPFILES message.
class CDragNDropDlg : public CDialog
  CDragNDropDlg(CWnd* pParent = NULL);  /* standard constructor */
  CListBox  m_List;
10    protected:
11    virtual void DoDataExchange(CDataExchange* pDX);
13  protected:
15    virtual BOOL OnInitDialog();
17    /* Event handler for ON_WM_DROPFILES */
18    afx_msg void OnDropFiles(HDROP hDropInfo);
21  };
22  BEGIN_MESSAGE_MAP(CDragNDropDlg, CDialog)
27  BOOL CDragNDropDlg::OnInitDialog()
28  {
29    CDialog::OnInitDialog();
30    /* Enable Window to accept drop files */
31    DragAcceptFiles(TRUE);
33    return TRUE;
34  }
36  void CDragNDropDlg::DoDataExchange(CDataExchange* pDX)
37  {
38    CDialog::DoDataExchange(pDX);
39    DDX_Control(pDX, IDC_LIST1, m_List);
40  }
41  /* MFC Event handler for ON_WM_DROPFILES */
42  void CDialogsDlg::OnDropFiles(HDROP hDropInfo)
43  {
44    TCHAR buffer[MAX_PATH + 1];
45    /* Get the number of files dropped */
46    UINT nCount = ::DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0);
47    m_List.ResetContent();
48    for (UINT index = 0; index < nCount; index++)
49    {
50      /* Get the path of file #n */
51      ::DragQueryFile(hDropInfo, index, buffer, MAX_PATH + 1);
52      /* Add the string in list box */
53      m_List.AddString(buffer);
54    }
55    CDialog::OnDropFiles(hDropInfo);
56    /* Free buffer and close handle */
57    ::DragFinish(hDropInfo);
58  }

Program output

During drag from windows explorer

Files dragged from Windows explorer

After files are dropped to dialog

List of files after drop

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Fri 02 May/2014 02:15:34(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.


Your message goes here:

My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.